当前位置:   article > 正文

Hive将Json字符串数组转为Json对象数组_hive jsonstring 转array

hive jsonstring 转array

一. maven pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.hive</groupId>
  4. <artifactId>hive-exec</artifactId>
  5. <version>2.1.1</version>
  6. </dependency>
  7. </dependencies>

二.  UDF代码

  1. package com.cn.bigdata.hive.func;
  2. import com.google.common.collect.Lists;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.hadoop.hive.ql.exec.Description;
  5. import org.apache.hadoop.hive.ql.exec.UDF;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import java.util.List;
  9. /**
  10. * @description Convert a string of a JSON-encoded array to a Hive array of strings
  11. */
  12. @Description(name = "json_array", value = "_FUNC_(array_string) - Convert a string of a JSON-encoded array to a Hive array of strings.")
  13. public class JsonArray extends UDF {
  14. public List<String> evaluate(String jsonString) {
  15. if (StringUtils.isBlank(jsonString)) {
  16. return null;
  17. }
  18. try {
  19. JSONArray jsonArray = new JSONArray(jsonString);
  20. List<String> result = Lists.newArrayList();
  21. for (int i = 0; i < jsonArray.length(); i++) {
  22. result.add(jsonArray.get(i).toString());
  23. }
  24. return result;
  25. } catch(JSONException | NumberFormatException e) {
  26. return null;
  27. }
  28. }
  29. }

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

闽ICP备14008679号