You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/05/30 15:20:24 UTC

[GitHub] [incubator-seatunnel] ruanwenjun commented on a diff in pull request #1971: [Improve]Use Jackson replace Fastjson

ruanwenjun commented on code in PR #1971:
URL: https://github.com/apache/incubator-seatunnel/pull/1971#discussion_r884921613


##########
seatunnel-apis/seatunnel-api-flink/pom.xml:
##########
@@ -87,6 +93,11 @@
             <artifactId>flink-avro</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>

Review Comment:
   We have defined the jackson-databind in `seatunnel-common`, so we don't need to define in this pom again.



##########
seatunnel-apis/seatunnel-api-flink/src/main/java/org/apache/seatunnel/flink/util/SchemaUtil.java:
##########
@@ -123,12 +124,12 @@ private static void getJsonSchema(Schema schema, JSONObject json) {
                 schema.field(key, Types.LONG());
             } else if (value instanceof BigDecimal) {
                 schema.field(key, Types.JAVA_BIG_DEC());
-            } else if (value instanceof JSONObject) {
-                schema.field(key, getTypeInformation((JSONObject) value));
-            } else if (value instanceof JSONArray) {
-                Object obj = ((JSONArray) value).get(0);
-                if (obj instanceof JSONObject) {
-                    schema.field(key, ObjectArrayTypeInfo.getInfoFor(Row[].class, getTypeInformation((JSONObject) obj)));
+            } else if (value instanceof ObjectNode) {
+                schema.field(key, getTypeInformation((ObjectNode) value));
+            } else if (value instanceof ObjectNode) {

Review Comment:
   ```suggestion
               } else if (value instanceof ObjectNode) {
                   schema.field(key, getTypeInformation((ObjectNode) value));
               } else if (value instanceof ArrayNode) {
   ```



##########
seatunnel-apis/seatunnel-api-flink/src/main/java/org/apache/seatunnel/flink/util/TableUtil.java:
##########
@@ -39,7 +39,11 @@ public static DataStream<Row> tableToDataStream(StreamTableEnvironment tableEnvi
         if (isAppend) {
             return tableEnvironment.toAppendStream(table, typeInfo);
         }
-        return tableEnvironment.toChangelogStream(table);
+        return tableEnvironment
+                .toRetractStream(table, typeInfo)
+                .filter(row -> row.f0)
+                .map(row -> row.f1)
+                .returns(typeInfo);

Review Comment:
   We need to revert this file?



##########
seatunnel-apis/seatunnel-api-flink/src/main/java/org/apache/seatunnel/flink/stream/FlinkStreamExecution.java:
##########
@@ -99,7 +99,7 @@ private Optional<DataStream<Row>> fromSourceTable(Config pluginConfig) {
         if (pluginConfig.hasPath(SOURCE_TABLE_NAME)) {
             StreamTableEnvironment tableEnvironment = flinkEnvironment.getStreamTableEnvironment();
             Table table = tableEnvironment.scan(pluginConfig.getString(SOURCE_TABLE_NAME));
-            return Optional.ofNullable(TableUtil.tableToDataStream(tableEnvironment, table, false));
+            return Optional.ofNullable(TableUtil.tableToDataStream(tableEnvironment, table, true));

Review Comment:
   This file need to revert?



##########
seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/JsonUtils.java:
##########
@@ -0,0 +1,377 @@
+/*
+ * 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.
+ */
+
+package org.apache.seatunnel.common.utils;
+
+import static com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT;
+import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
+import static com.fasterxml.jackson.databind.DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL;
+import static com.fasterxml.jackson.databind.MapperFeature.REQUIRE_SETTERS_FOR_GETTERS;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.fasterxml.jackson.databind.type.CollectionType;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.TimeZone;
+
+public class JsonUtils {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);
+
+    /**
+     * can use static singleton, inject: just make sure to reuse!
+     */
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
+            .configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
+            .configure(ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)
+            .configure(READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
+            .configure(REQUIRE_SETTERS_FOR_GETTERS, true)
+            .setTimeZone(TimeZone.getDefault());
+
+    private JsonUtils() {
+        throw new UnsupportedOperationException("Construct JSONUtils");
+    }
+
+    public static ArrayNode createArrayNode() {
+        return OBJECT_MAPPER.createArrayNode();
+    }
+
+    public static ObjectNode createObjectNode() {
+        return OBJECT_MAPPER.createObjectNode();
+    }
+
+    public static JsonNode toJsonNode(Object obj) {
+        return OBJECT_MAPPER.valueToTree(obj);
+    }
+
+    /**
+     * json representation of object
+     *
+     * @param object  object
+     * @param feature feature
+     * @return object to json string
+     */
+    public static String toJsonString(Object object, SerializationFeature feature) {
+        try {
+            ObjectWriter writer = OBJECT_MAPPER.writer(feature);
+            return writer.writeValueAsString(object);
+        } catch (Exception e) {
+            LOGGER.error("object to json exception!", e);

Review Comment:
   It's better to keep the same logic to deal with exception. In this file, there are some methods will catch the exception and some methods will re throw the exception.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org