当前位置:   article > 正文

hive解析jsonobject jsonarray常用的自定义函数_hive json 转 array

hive json 转 array

  1. # 加载 + 使用
  2. ```hiveql
  3. add jar /Users/thomas990p/IdeaProjects/hiveThomasUDF/target/hiveThomasUDF-1.0-SNAPSHOT-jar-with-dependencies.jar;
  4. -- udf
  5. create temporary function size_json_array as "org.thomas.hive.udf.JsonArraySize";
  6. -- select size_json_array('[{"k1":"v1"},{"k2":"v2"}]') ;
  7. -- 2
  8. create temporary function get_json_object_via_path as 'org.thomas.hive.udf.GetJsonObjectViaPath';
  9. -- select get_json_object_via_path('[{"k1":11,"k2":22},{"k1":44,"k2":55}]','$[*].k1') ;
  10. -- [11,44] 这里不是数组 是string
  11. create temporary function get_json_array_via_path as 'org.thomas.hive.udf.GetJsonArrayViaPath';
  12. -- select get_json_array_via_path('{"k1":100,"k2":[11,22,33]}','$.k2') ;
  13. -- ["11","22","33"] Array<String>
  14. -- select get_json_array_via_path('[{"k1":11,"k2":22},{"k1":44,"k2":55}]','$[*].k1') ;
  15. -- [11,44] 这里是数组
  16. create temporary function castArrayType2Int as 'org.thomas.hive.udf.ArrayCastType.CastArrayT1Cast2Int';
  17. -- select castArrayType2Int(array('1','2')) ;
  18. -- [1,2]
  19. create temporary function castArrayType2Double as 'org.thomas.hive.udf.ArrayCastType.CastArrayT1Cast2Double';
  20. -- select castArrayType2Double(array('1.11','2.22')) ;
  21. -- [1.11,2.23]
  22. create temporary function castArrayType2Long as 'org.thomas.hive.udf.ArrayCastType.CastArrayT1Cast2Long';
  23. -- select castArrayType2Long(array(123456789012,1234567890123)) ;
  24. -- [123456789012,1234567890123]
  25. create temporary function get_json_array as "org.thomas.hive.udf.GetJsonArray";
  26. -- select name,field_name2 from tb1 lateral view explode(get_json_array('[{"k1":"v1"},{"k2":"v2"}]')) tmp_table_name AS field_name2 limit 2;
  27. -- +------+-----------+
  28. -- |name |field_name2|
  29. -- +------+-----------+
  30. -- |thomas|{"k1":"v1"}|
  31. -- |thomas|{"k2":"v2"}|
  32. -- +------+-----------+
  33. -- udtf
  34. create temporary function explode_json_array as "org.thomas.hive.udtf.ExplodeJsonArray";
  35. -- select name,ainfo from tb1 lateral view explode_json_array('[{"k1":"v1"},{"k2":"v2"}]') tmp_table_name AS ainfo limit 2;
  36. -- +------+-----------+
  37. -- |name |ainfo |
  38. -- +------+-----------+
  39. -- |thomas|{"k1":"v1"}|
  40. -- |thomas|{"k2":"v2"}|
  41. -- +------+-----------+
  42. -- udaf
  43. ```

持续更新

GitHub - yangyongyongyong/hiveThomasUDF at dev

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/911190
推荐阅读
相关标签
  

闽ICP备14008679号