|
@@ -0,0 +1,52 @@
|
|
|
+package com.winhc.bigdata.udf;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONPath;
|
|
|
+import com.aliyun.odps.udf.UDF;
|
|
|
+import com.aliyun.odps.utils.StringUtils;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: π
|
|
|
+ * @Description: 获取json 里字段
|
|
|
+ */
|
|
|
+public class split_names extends UDF {
|
|
|
+
|
|
|
+ public List<String> evaluate(String jsons, String json_path) {
|
|
|
+ if (StringUtils.isBlank(jsons)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String[] jsonArr = jsons.split("&");
|
|
|
+ HashSet<String> set = new HashSet();
|
|
|
+ for (String json : jsonArr) {
|
|
|
+ parse(json, json_path, set);
|
|
|
+ }
|
|
|
+ return new ArrayList<>(set);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void parse(String json, String json_path, Set set) {
|
|
|
+ JSONArray jsonArray = ((JSONArray) JSONPath.eval(JSON.parse(json), json_path));
|
|
|
+ for (Object o : jsonArray) {
|
|
|
+ set.add(o);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String jsons = "[{\"name\":\"张海林\",\"litigant_id\":\"\"},{\"name\":\"招商银行股份有限公司信用卡中心\",\"litigant_id\":\"cc45eeb0634f73531ba54ad55ba152a6\"}]&[{\"name\":\"上海浦东发展银行静安支行\",\"litigant_id\":\"c2c57fcc6398cfd49393c1a2d3c35a4c\"}]";
|
|
|
+ String json_path = "$.name";
|
|
|
+ split_names n = new split_names();
|
|
|
+ System.out.println(n.evaluate(jsons, json_path));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|