|
@@ -0,0 +1,63 @@
|
|
|
|
+package com.winhc.bigdata.udf;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
|
+import com.aliyun.odps.udf.UDF;
|
|
|
|
+import com.aliyun.odps.utils.StringUtils;
|
|
|
|
+
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author: π
|
|
|
|
+ * 2021/8/30 16:57
|
|
|
|
+ */
|
|
|
|
+public class capital_update extends UDF {
|
|
|
|
+ private static List<String> cols = Arrays.asList("amomon", "paymet", "time");
|
|
|
|
+
|
|
|
|
+ public List<String> evaluate(String json) {
|
|
|
|
+ if (StringUtils.isBlank(json) || "[]".equalsIgnoreCase(json)) return Arrays.asList("1", json);
|
|
|
|
+ try {
|
|
|
|
+ List<Map> list1 = JSON.parseArray(json, Map.class);
|
|
|
|
+ AtomicReference<String> flag = new AtomicReference<>("0");
|
|
|
|
+ List<HashMap<String, String>> list2 = list1.stream().map(m -> {
|
|
|
|
+ HashMap<String, String> tm = new HashMap<>();
|
|
|
|
+ cols.forEach(k -> {
|
|
|
|
+ if (!m.containsKey("amomon")) {
|
|
|
|
+ tm.put(k, toStringV2(m.getOrDefault(k, "0")));
|
|
|
|
+ flag.set("2");
|
|
|
|
+ } else if (!m.containsKey(k)) {
|
|
|
|
+ tm.put(k, toStringV2(m.getOrDefault(k, "")));
|
|
|
|
+ flag.set("3");
|
|
|
|
+ } else {
|
|
|
|
+ tm.put(k, toStringV2(m.getOrDefault(k, "")));
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ return tm;
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ return Arrays.asList(flag.get(), JSON.toJSONString(list2, SerializerFeature.WriteMapNullValue));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return Arrays.asList("4", null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String toStringV2(Object o) {
|
|
|
|
+ if (null == o) return null;
|
|
|
|
+ return o.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ capital_update j = new capital_update();
|
|
|
|
+ //String json ="[{\"amomon\": \"13608万人民币\", \"paymet\": \"货币\", \"time\": \"2024-07-31\"}]";
|
|
|
|
+ //String json = "[{\"amomon\": \"13608万人民币\", \"time\": \"2024-07-31\"}]";
|
|
|
|
+ //String json = "[{\"time\": \"2024-07-31\"}]";
|
|
|
|
+ //String json = "[]";
|
|
|
|
+ String json = "[{\"amomon\": \"6.55万人民币\", \"paymet\": null, \"time\": \"2009-01-15\"}]";
|
|
|
|
+ List<String> evaluate = j.evaluate(json);
|
|
|
|
+ System.out.println(evaluate);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|