|
@@ -0,0 +1,35 @@
|
|
|
|
+package com.winhc.bigdata.udf;
|
|
|
|
+
|
|
|
|
+import com.aliyun.odps.udf.UDF;
|
|
|
|
+import com.aliyun.odps.utils.StringUtils;
|
|
|
|
+
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Author: Yan Yongnian
|
|
|
|
+ * @Date: 2020/8/5
|
|
|
|
+ * @Description: 包容null的字符串连接函数,类似spark sql里的对应函数
|
|
|
|
+ */
|
|
|
|
+public class Concat_ws2 extends UDF {
|
|
|
|
+ public String evaluate(String seperator,String...args) {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for(String arg : args) {//当作数组用foreach遍历
|
|
|
|
+ if(arg!=null){
|
|
|
|
+ sb.append(arg);
|
|
|
|
+ sb.append(seperator);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ int sepLen = seperator.length();
|
|
|
|
+ int sbLen = sb.length();
|
|
|
|
+ if(seperator==null || sepLen==0){
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return sbLen > 0 ? sb.delete(sbLen - sepLen, sbLen).toString() : null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String res = new Concat_ws2().evaluate("_","100336476","94126f32c17495ad45ef1e8bb51d2ac4");
|
|
|
|
+ System.out.println(res);
|
|
|
|
+ }
|
|
|
|
+}
|