You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/02/17 08:06:29 UTC

[GitHub] [incubator-inlong] ywh19911024 commented on a change in pull request #2525: [INLONG-2524][Feature][Sort] Support deserialization of json, canal and avro formatted data

ywh19911024 commented on a change in pull request #2525:
URL: https://github.com/apache/incubator-inlong/pull/2525#discussion_r808766698



##########
File path: inlong-sort/sort-single-tenant/src/main/java/org/apache/inlong/sort/singletenant/flink/serialization/CustomDateFormatSerializationSchemaWrapper.java
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.inlong.sort.singletenant.flink.serialization;
+
+import org.apache.commons.lang3.time.FastDateFormat;
+import org.apache.flink.api.common.serialization.SerializationSchema;
+import org.apache.flink.shaded.guava18.com.google.common.annotations.VisibleForTesting;
+import org.apache.flink.types.Row;
+import org.apache.inlong.sort.formats.common.DateFormatInfo;
+import org.apache.inlong.sort.formats.common.FormatInfo;
+import org.apache.inlong.sort.formats.common.TimeFormatInfo;
+import org.apache.inlong.sort.formats.common.TimestampFormatInfo;
+
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+
+import static org.apache.flink.shaded.guava18.com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.flink.shaded.guava18.com.google.common.base.Preconditions.checkState;
+import static org.apache.inlong.sort.singletenant.flink.utils.CommonUtils.isStandardTimestampFormat;
+
+public class CustomDateFormatSerializationSchemaWrapper implements SerializationSchema<Row> {
+
+    private static final long serialVersionUID = 7342088340154604198L;
+
+    private final SerializationSchema<Row> innerSchema;
+
+    private final FormatInfo[] formatInfos;
+
+    public CustomDateFormatSerializationSchemaWrapper(SerializationSchema<Row> innerSchema, FormatInfo[] formatInfos) {
+        this.innerSchema = checkNotNull(innerSchema);
+        this.formatInfos = checkNotNull(formatInfos);
+    }
+
+    @Override
+    public void open(InitializationContext context) throws Exception {
+        innerSchema.open(context);
+    }
+
+    @Override
+    public byte[] serialize(Row element) {
+        Row outputRow = fromDateAndTimeToString(element);
+        return innerSchema.serialize(outputRow);
+    }
+
+    @VisibleForTesting
+    Row fromDateAndTimeToString(Row inputRow) {
+        int arity = inputRow.getArity();
+        Row outputRow = new Row(arity);
+        for (int i = 0; i < arity; i++) {
+            outputRow.setField(i, convert(inputRow.getField(i), formatInfos[i]));
+        }
+        outputRow.setKind(inputRow.getKind());
+        return outputRow;
+    }
+
+    private Object convert(Object input, FormatInfo formatInfo) {

Review comment:
       这个是不是可以移动到CommonUtils类中




-- 
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@inlong.apache.org

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