xufei преди 2 години
родител
ревизия
08f52b6a9d
променени са 2 файла, в които са добавени 108 реда и са изтрити 0 реда
  1. 55 0
      src/main/java/com/winhc/bigdata/udf/get_json_info.java
  2. 53 0
      src/main/java/com/winhc/bigdata/udf/replace_pid.java

+ 55 - 0
src/main/java/com/winhc/bigdata/udf/get_json_info.java

@@ -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);
+    }
+
+}

+ 53 - 0
src/main/java/com/winhc/bigdata/udf/replace_pid.java

@@ -0,0 +1,53 @@
+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.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * @author: π
+ * @desc:替换pid为空
+ */
+public class replace_pid extends UDF {
+
+    public 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<Map> m1 = list1.stream().map(m -> {
+                for (String k : keys) {
+                    if (StringUtils.isNotBlank(k)) {
+                        String v = toStringV2(m.getOrDefault(k, ""));
+                        if (v.length() == 33) {
+                            m.put(k, "");
+                        }
+                    }
+                }
+                return m;
+            }).collect(Collectors.toList());
+            return JSON.toJSONString(m1);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "[]";
+        }
+
+    }
+
+    public static String toStringV2(Object o) {
+        if (null == o) return null;
+        return o.toString();
+    }
+
+    public static void main(String[] args) {
+        replace_pid j = new replace_pid();
+        String json = "[{\"name\": \"周一凡\", \"litigant_id\": \"627a851aeb97a3311b7f43a20d3a0df3\"}, {\"name\": \"深圳市中装建设集团股份有限公司\", \"litigant_id\": \"627a851aeb97a3311b7f43a20d3a0df3\"}, {\"name\": \"杭州创意投资发展有限公司\", \"litigant_id\": \"p2772b07553e36f501654c193c2728cd0\"}]";
+        String evaluate = j.evaluate(json, "litigant_id");
+        System.out.println(evaluate);
+    }
+
+}