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/07/18 11:45:08 UTC

[GitHub] [inlong] thesumery commented on a diff in pull request #5061: [INLONG-4806][Sort] Sort support InlongMsg pb format

thesumery commented on code in PR #5061:
URL: https://github.com/apache/inlong/pull/5061#discussion_r923273967


##########
inlong-sort/sort-formats/format-inlongmsg-pb/src/main/java/org/apache/inlong/sort/formats/inlongmsgpb/InLongMsgPbDecodingFormat.java:
##########
@@ -0,0 +1,237 @@
+/*
+ * 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.formats.inlongmsgpb;
+
+import org.apache.commons.lang3.time.DateFormatUtils;
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.formats.csv.CsvRowDataDeserializationSchema;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.connector.format.DecodingFormat;
+import org.apache.flink.table.connector.source.DynamicTableSource.Context;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.utils.DataTypeUtils;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk;
+import org.apache.inlong.sdk.commons.utils.GzipUtils;
+import org.apache.inlong.sort.formats.inlongmsgpb.InLongMsgPbDeserializationSchema.MetadataConverter;
+import org.apache.inlong.sort.formats.inlongmsgpb.InLongMsgPbDeserializationSchema.InLongPbMsgDecompressor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xerial.snappy.Snappy;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * InLongMsg pb format decoding format.
+ */
+public class InLongMsgPbDecodingFormat implements DecodingFormat<DeserializationSchema<RowData>> {
+
+    private static final Logger log = LoggerFactory.getLogger(InLongMsgPbDecodingFormat.class);
+
+    private final String innerFormatMetaPrefix;
+
+    private final DecodingFormat<DeserializationSchema<RowData>> innerDecodingFormat;
+
+    private List<String> metadataKeys;
+
+    private final boolean ignoreErrors;
+
+    private final String decompressType;
+
+    private final boolean ignoreTailingUnmappable;
+
+    public InLongMsgPbDecodingFormat(
+            DecodingFormat<DeserializationSchema<RowData>> innerDecodingFormat,
+            String innerFormatMetaPrefix,
+            boolean ignoreErrors,
+            boolean ignoreTailingUnmappable,
+            String decompressType) {
+        this.innerDecodingFormat = innerDecodingFormat;
+        this.innerFormatMetaPrefix =  innerFormatMetaPrefix;
+        this.metadataKeys = Collections.emptyList();
+        this.ignoreErrors = ignoreErrors;
+        this.ignoreTailingUnmappable = ignoreTailingUnmappable;
+        this.decompressType = decompressType;
+    }
+
+    @Override
+    public DeserializationSchema<RowData> createRuntimeDecoder(Context context, DataType physicalDataType) {
+        final MetadataConverter[] metadataConverters = Arrays.stream(ReadableMetadata.values())
+                .filter(metadata -> metadataKeys.contains(metadata.key))
+                .map(metadata -> metadata.converter)
+                .toArray(MetadataConverter[]::new);
+        final List<ReadableMetadata> readableMetadata =
+                metadataKeys.stream()
+                        .map(
+                                k ->
+                                        Stream.of(ReadableMetadata.values())
+                                                .filter(rm -> rm.key.equals(k))
+                                                .findFirst()
+                                                .orElseThrow(IllegalStateException::new))
+                        .collect(Collectors.toList());
+        final List<DataTypes.Field> metadataFields =
+                readableMetadata.stream()
+                        .map(m -> DataTypes.FIELD(m.key, m.dataType))
+                        .collect(Collectors.toList());
+        final DataType producedDataType =
+                DataTypeUtils.appendRowFields(physicalDataType, metadataFields);
+        final TypeInformation<RowData> producedTypeInfo =
+                context.createTypeInformation(producedDataType);
+        final InLongPbMsgDecompressor decompressor = getDecompressor(decompressType);
+
+        DeserializationSchema<RowData> innerSchema =
+                innerDecodingFormat.createRuntimeDecoder(context, physicalDataType);
+        if (innerSchema instanceof CsvRowDataDeserializationSchema && ignoreTailingUnmappable) {
+            this.makeCsvInnerFormatIgnoreTailingUnmappable(innerSchema);

Review Comment:
   why here a special treatment?could you describe it more clearly in this [4807](https://github.com/apache/inlong/issues/4807)



-- 
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