xufei 2 лет назад
Родитель
Сommit
c614fad9d8

+ 30 - 1
src/main/java/com/winhc/bigdata/bean/Category.java

@@ -1,7 +1,9 @@
 package com.winhc.bigdata.bean;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author: XuJiakai
@@ -464,6 +466,31 @@ public class Category {
         }
     };
 
+    public static final Map<String, String> CATEGORY_CODE_FIRST_MAP = CATEGORY_MAP.entrySet().stream()
+            .map(x -> Arrays.asList(x.getValue().cateFirstCode, x.getValue().cateFirst))
+            .distinct().collect(Collectors.toMap(t -> t.get(0), t -> t.get(1), (n, o) -> n));
+
+    public static final Map<String, String> CATEGORY_CODE_SECOND_MAP = CATEGORY_MAP.entrySet().stream()
+            .map(x -> Arrays.asList(x.getValue().cateSecondCode, x.getValue().cateSecond))
+            .distinct().collect(Collectors.toMap(t -> t.get(0), t -> t.get(1), (n, o) -> n));
+
+    public static final Map<String, String> CATEGORY_CODE_THIRD_MAP = CATEGORY_MAP.entrySet().stream()
+            .map(x -> Arrays.asList(x.getValue().cateThirdCode, x.getValue().cateThird))
+            .distinct().collect(Collectors.toMap(t -> t.get(0), t -> t.get(1), (n, o) -> n));
+
+    public static String getCategoryCode2Name(String code, String index) {
+        switch (index) {
+            case "1":
+                return CATEGORY_CODE_FIRST_MAP.getOrDefault(code,"");
+            case "2":
+                return CATEGORY_CODE_SECOND_MAP.getOrDefault(code,"");
+            case "3":
+                return CATEGORY_CODE_THIRD_MAP.getOrDefault(code,"");
+            default:
+                throw new RuntimeException("不支持的索引,[1,2,3]");
+        }
+    }
+
     public static String getCategoryCode(String code, String index) {
         Category category = Category.CATEGORY_MAP.getOrDefault(code, null);
         if (category == null) {
@@ -522,6 +549,8 @@ public class Category {
 
 
     public static void main(String[] args) {
-        System.out.println(getCategoryName("28","1"));
+        System.out.println(getCategoryCode2Name("28", "1"));
+        System.out.println(getCategoryCode2Name("63", "2"));
+        System.out.println(getCategoryCode2Name("454", "3"));
     }
 }

+ 15 - 0
src/main/java/com/winhc/bigdata/udf/CategoryCode2Name.java

@@ -0,0 +1,15 @@
+package com.winhc.bigdata.udf;
+
+import com.aliyun.odps.udf.UDF;
+import com.winhc.bigdata.bean.Category;
+
+/**
+ * @author: π
+ * 2021/12/30 10:42
+ */
+public class CategoryCode2Name extends UDF {
+
+    public String evaluate(String code, String index) {
+        return Category.getCategoryCode2Name(code, index);
+    }
+}