Bladeren bron

feat: 添加一些工具类、依赖

许家凯 8 maanden geleden
bovenliggende
commit
a272ec518d

+ 8 - 0
pom.xml

@@ -87,6 +87,14 @@
             <version>3.7</version>
         </dependency>
 
+        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.20</version>
+            <scope>provided</scope>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 136 - 0
src/main/java/com/winhc/bigdata/bean/Tuple2.java

@@ -0,0 +1,136 @@
+package com.winhc.bigdata.bean;
+
+/**
+ * @Author: XuJiakai
+ * 2024/6/5 10:36
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// --------------------------------------------------------------
+//  THIS IS A GENERATED SOURCE FILE. DO NOT EDIT!
+//  GENERATED FROM org.apache.flink.api.java.tuple.TupleGenerator.
+// --------------------------------------------------------------
+
+
+import com.winhc.bigdata.utils.BaseUtils;
+
+/**
+ * A tuple with 2 fields. Tuples are strongly typed; each field may be of a separate type. The
+ * fields of the tuple can be accessed directly as public fields (f0, f1, ...) or via their position
+ * through the {@link #getField(int)} method. The tuple field positions start at zero.
+ *
+ * <p>Tuples are mutable types, meaning that their fields can be re-assigned. This allows functions
+ * that work with Tuples to reuse objects in order to reduce pressure on the garbage collector.
+ *
+ * <p>Warning: If you subclass Tuple2, then be sure to either
+ *
+ * <ul>
+ *   <li>not add any new fields, or
+ *   <li>make it a POJO, and always declare the element type of your DataStreams/DataSets to your
+ *       descendant type. (That is, if you have a "class Foo extends Tuple2", then don't use
+ *       instances of Foo in a DataStream&lt;Tuple2&gt; / DataSet&lt;Tuple2&gt;, but declare it as
+ *       DataStream&lt;Foo&gt; / DataSet&lt;Foo&gt;.)
+ * </ul>
+ *
+ * @see Tuple
+ * @param <T0> The type of field 0
+ * @param <T1> The type of field 1
+ */
+public class Tuple2<T0, T1> {
+
+    private static final long serialVersionUID = 1L;
+
+    /** Field 0 of the tuple. */
+    public T0 f0;
+    /** Field 1 of the tuple. */
+    public T1 f1;
+
+    /** Creates a new tuple where all fields are null. */
+    public Tuple2() {}
+
+    /**
+     * Creates a new tuple and assigns the given values to the tuple's fields.
+     *
+     * @param f0 The value for field 0
+     * @param f1 The value for field 1
+     */
+    public Tuple2(T0 f0, T1 f1) {
+        this.f0 = f0;
+        this.f1 = f1;
+    }
+
+
+    // -------------------------------------------------------------------------------------------------
+    // standard utilities
+    // -------------------------------------------------------------------------------------------------
+
+    /**
+     * Creates a string representation of the tuple in the form (f0, f1), where the individual
+     * fields are the value returned by calling {@link Object#toString} on that field.
+     *
+     * @return The string representation of the tuple.
+     */
+    @Override
+    public String toString() {
+        return BaseUtils.toString(this);
+    }
+
+    /**
+     * Deep equality for tuples by calling equals() on the tuple members.
+     *
+     * @param o the object checked for equality
+     * @return true if this is equal to o.
+     */
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof Tuple2)) {
+            return false;
+        }
+        @SuppressWarnings("rawtypes")
+        Tuple2 tuple = (Tuple2) o;
+        if (f0 != null ? !f0.equals(tuple.f0) : tuple.f0 != null) {
+            return false;
+        }
+        if (f1 != null ? !f1.equals(tuple.f1) : tuple.f1 != null) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = f0 != null ? f0.hashCode() : 0;
+        result = 31 * result + (f1 != null ? f1.hashCode() : 0);
+        return result;
+    }
+
+
+    /**
+     * Creates a new tuple and assigns the given values to the tuple's fields. This is more
+     * convenient than using the constructor, because the compiler can infer the generic type
+     * arguments implicitly. For example: {@code Tuple3.of(n, x, s)} instead of {@code new
+     * Tuple3<Integer, Double, String>(n, x, s)}
+     */
+    public static <T0, T1> Tuple2<T0, T1> of(T0 f0, T1 f1) {
+        return new Tuple2<>(f0, f1);
+    }
+}

+ 97 - 0
src/main/java/com/winhc/bigdata/bean/Tuple3.java

@@ -0,0 +1,97 @@
+package com.winhc.bigdata.bean;
+
+import com.winhc.bigdata.utils.BaseUtils;
+
+/**
+ * @Author: XuJiakai
+ * 2024/6/5 10:37
+ */
+public class Tuple3<T0, T1, T2> {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Field 0 of the tuple.
+     */
+    public T0 f0;
+    /**
+     * Field 1 of the tuple.
+     */
+    public T1 f1;
+    /**
+     * Field 2 of the tuple.
+     */
+    public T2 f2;
+
+    /**
+     * Creates a new tuple where all fields are null.
+     */
+    public Tuple3() {
+    }
+
+    /**
+     * Creates a new tuple and assigns the given values to the tuple's fields.
+     *
+     * @param f0 The value for field 0
+     * @param f1 The value for field 1
+     * @param f2 The value for field 2
+     */
+    public Tuple3(T0 f0, T1 f1, T2 f2) {
+        this.f0 = f0;
+        this.f1 = f1;
+        this.f2 = f2;
+    }
+
+
+    @Override
+    public String toString() {
+        return BaseUtils.toString(this);
+    }
+
+    /**
+     * Deep equality for tuples by calling equals() on the tuple members.
+     *
+     * @param o the object checked for equality
+     * @return true if this is equal to o.
+     */
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof Tuple3)) {
+            return false;
+        }
+        @SuppressWarnings("rawtypes")
+        Tuple3 tuple = (Tuple3) o;
+        if (f0 != null ? !f0.equals(tuple.f0) : tuple.f0 != null) {
+            return false;
+        }
+        if (f1 != null ? !f1.equals(tuple.f1) : tuple.f1 != null) {
+            return false;
+        }
+        if (f2 != null ? !f2.equals(tuple.f2) : tuple.f2 != null) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = f0 != null ? f0.hashCode() : 0;
+        result = 31 * result + (f1 != null ? f1.hashCode() : 0);
+        result = 31 * result + (f2 != null ? f2.hashCode() : 0);
+        return result;
+    }
+
+
+    /**
+     * Creates a new tuple and assigns the given values to the tuple's fields. This is more
+     * convenient than using the constructor, because the compiler can infer the generic type
+     * arguments implicitly. For example: {@code Tuple3.of(n, x, s)} instead of {@code new
+     * Tuple3<Integer, Double, String>(n, x, s)}
+     */
+    public static <T0, T1, T2> Tuple3<T0, T1, T2> of(T0 f0, T1 f1, T2 f2) {
+        return new Tuple3<>(f0, f1, f2);
+    }
+}

+ 16 - 0
src/main/java/com/winhc/bigdata/utils/BaseUtils.java

@@ -4,6 +4,7 @@ import cn.hutool.core.io.file.FileReader;
 import cn.hutool.core.io.resource.ResourceUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.serializer.SerializerFeature;
+import org.apache.commons.lang3.StringUtils;
 
 /**
  * @Author: XuJiakai
@@ -23,6 +24,21 @@ public class BaseUtils {
 
     }
 
+    public static String toString(Object o, SerializerFeature... features) {
+        SerializerFeature[] args = new SerializerFeature[features.length + 1];
+        args[0] = SerializerFeature.WriteMapNullValue;
+        System.arraycopy(features, 0, args, 1, features.length);
+        return JSONObject.toJSONString(o, args);
+    }
+    public static String dateTime2DateStr(String datetime){
+        if (StringUtils.isEmpty(datetime)) {
+            return null;
+        }
+        if(datetime.length()==19){
+            return datetime.substring(0, 10);
+        }
+        return datetime;
+    }
     public static void main(String[] args) {
         System.out.println(readeResourceFile("case_info.json"));
     }

+ 39 - 0
src/main/java/com/winhc/bigdata/utils/JsonObjectUtils.java

@@ -0,0 +1,39 @@
+package com.winhc.bigdata.utils;
+
+import com.alibaba.fastjson.JSONObject;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @Author: XuJiakai
+ * 2024/5/22 9:38
+ */
+public class JsonObjectUtils {
+    public static String getStringOrNull(JSONObject jsonObject, String key) {
+        if (jsonObject.containsKey(key)) {
+            return jsonObject.getString(key);
+        }
+        return null;
+    }
+
+    public static <T> List<T> getListOrNull(JSONObject jsonObject, String key, Class<T> type) {
+        if (jsonObject.containsKey(key) && jsonObject.getJSONArray(key) != null) {
+            List<T> javaList = jsonObject.getJSONArray(key).toJavaList(type);
+            if (javaList == null || javaList.isEmpty()) {
+                return null;
+            }
+            return javaList;
+        }
+        return null;
+    }
+
+    public static <T> List<T> getListOrEmpty(JSONObject jsonObject, String key, Class<T> type) {
+        if (jsonObject.containsKey(key) && jsonObject.getJSONArray(key) != null) {
+            return jsonObject.getJSONArray(key).toJavaList(type);
+        }
+        return Collections.emptyList();
+    }
+}