|
@@ -0,0 +1,55 @@
|
|
|
+package com.winhc.bigdata.udf;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.aliyun.odps.udf.UDF;
|
|
|
+import com.aliyun.odps.utils.StringUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: π
|
|
|
+ * @desc:提取json中k,v
|
|
|
+ */
|
|
|
+public class get_json_info extends UDF {
|
|
|
+
|
|
|
+ public List<String> evaluate(String json, String... keys) {
|
|
|
+ if (StringUtils.isBlank(json) || "[]".equalsIgnoreCase(json) || keys == null) return null;
|
|
|
+ try {
|
|
|
+ List<Map> list1 = JSON.parseArray(json, Map.class);
|
|
|
+ List<String> m1 = list1.stream().map(m -> {
|
|
|
+ ArrayList<String> list = new ArrayList<>();
|
|
|
+ for (String k : keys) {
|
|
|
+ if (StringUtils.isNotBlank(k)) {
|
|
|
+ String v = toStringV2(m.getOrDefault(k, ""));
|
|
|
+ list.add(v);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return String.join("@@", list);
|
|
|
+ }).filter(StringUtils::isNotBlank).distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (m1.isEmpty()) return null;
|
|
|
+ return m1;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String toStringV2(Object o) {
|
|
|
+ if (null == o) return null;
|
|
|
+ return o.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ get_json_info j = new get_json_info();
|
|
|
+ String json = "[{\"name\": \"周一凡\", \"litigant_id\": \"\"}, {\"name\": \"深圳市中装建设集团股份有限公司\", \"litigant_id\": \"627a851aeb97a3311b7f43a20d3a0df3\"}, {\"name\": \"杭州创意投资发展有限公司\", \"litigant_id\": \"2772b07553e36f501654c193c2728cd0\"}]";
|
|
|
+ List<String> evaluate = j.evaluate(json, "name", "litigant_id");
|
|
|
+ System.out.println(evaluate);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|