|
@@ -0,0 +1,57 @@
|
|
|
+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.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: π
|
|
|
+ * 2021/8/30 16:57
|
|
|
+ */
|
|
|
+public class legal_merge extends UDF {
|
|
|
+
|
|
|
+ public String evaluate(String old_legal, String new_legal) {
|
|
|
+ if (
|
|
|
+ StringUtils.isBlank(old_legal) || "[]".equalsIgnoreCase(old_legal)
|
|
|
+ || StringUtils.isBlank(new_legal) || "[]".equalsIgnoreCase(new_legal)
|
|
|
+ ) return old_legal;
|
|
|
+ List<Map> list1 = JSON.parseArray(old_legal, Map.class);
|
|
|
+ List<Map> list2 = JSON.parseArray(new_legal, Map.class);
|
|
|
+ Map<String, String> merge_map = list2.stream().map(m -> {
|
|
|
+ String name = toStringV2(m.getOrDefault("name", ""));
|
|
|
+ String id = toStringV2(m.getOrDefault("id", ""));
|
|
|
+ if (StringUtils.isNotBlank(name)) {
|
|
|
+ return Arrays.asList(name, id);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }).filter(Objects::nonNull).collect(Collectors.toMap(t -> t.get(0), t -> t.get(1), (o, n) -> o));
|
|
|
+
|
|
|
+ List<Map> res = list1.stream().map(m0 -> {
|
|
|
+ String name = toStringV2(m0.getOrDefault("name", ""));
|
|
|
+ if (merge_map.containsKey(name)) {
|
|
|
+ m0.put("id", merge_map.get(name));
|
|
|
+ }
|
|
|
+ return m0;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return JSON.toJSONString(res);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String toStringV2(Object o) {
|
|
|
+ if (null == o) return null;
|
|
|
+ return o.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ legal_merge j = new legal_merge();
|
|
|
+ //String json = "[{\"name\":\"冯金元\",\"id\":\"pbc6c4f933e85850d1c401509877671b0\",\"type\":1,\"deleted\":0}]";
|
|
|
+ //String json = "[{\"name\":\"冯金元\",\"id\":\"pbc6c4f933e85850d1c401509877671b0\",\"type\":1,\"deleted\":1}]";
|
|
|
+ String json1 = "[{\"name\":\"冯金元111\",\"id\":\"pbc6c4f933e85850d1c401509877671b0\",\"type\":1,\"deleted\":9},{\"name\":\"冯金元\",\"id\":\"pbc6c4f933e85850d1c401509877671b0\",\"type\":1,\"deleted\":1},{\"name\":\"冯金元2\",\"id\":\"pbc6c4f933e85850d1c401509877671b0\",\"type\":1,\"deleted\":0}]";
|
|
|
+ String json2 = "[{\"name\":\"冯金元\",\"id\":\"xxxxc4f933e85850d1c401509877671b0\"},{\"name\":\"冯金元111\",\"id\":\"000004f933e85850d1c401509877671b0\"}]";
|
|
|
+ String evaluate = j.evaluate(json1, json2);
|
|
|
+ System.out.println(evaluate);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|