Selaa lähdekoodia

feat(udf): 计算缺失的年份

JimZhang 2 vuotta sitten
vanhempi
commit
0cf287187f
1 muutettua tiedostoa jossa 43 lisäystä ja 0 poistoa
  1. 43 0
      src/main/java/com/winhc/bigdata/udf/ComputeLostNum.java

+ 43 - 0
src/main/java/com/winhc/bigdata/udf/ComputeLostNum.java

@@ -0,0 +1,43 @@
+package com.winhc.bigdata.udf;
+
+import com.aliyun.odps.udf.UDF;
+import com.google.common.collect.Lists;
+
+import java.time.LocalDate;
+import java.util.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * @author ZhangJi
+ * @since 2022-06-30 13:26
+ */
+public class ComputeLostNum extends UDF {
+    private static final Pattern NUM_4 = Pattern.compile("^\\d{4}$");
+    private static final int CURRENT_MONTH = LocalDate.now().getMonthValue();
+
+    public List<String> evaluate(List<String> arr, Integer max) {
+        max -= 1;
+        if (arr == null || arr.size() == 0) {
+            return Collections.singletonList(String.valueOf(max));
+        }
+        int min = arr
+                .stream()
+                .filter(s -> NUM_4.matcher(s).matches())
+                .mapToInt(Integer::parseInt)
+                .min().orElse(max);
+        if (min >= max) return null;
+        Set<String> sets = new HashSet<>(arr);
+        List<String> result = IntStream.range(min, max + 1)
+                .mapToObj(String::valueOf)
+                .filter(s -> !sets.contains(s))
+                .collect(Collectors.toList());
+        if (result.isEmpty()) return null;
+        return result;
+    }
+
+    public static void main(String[] args) {
+        System.out.println(new ComputeLostNum().evaluate(Lists.asList("2012", new String[]{"2018", "2019", "2020"}), 2022));
+    }
+}