gong commented on code in PR #5276:
URL: https://github.com/apache/inlong/pull/5276#discussion_r935161114


##########
inlong-sort/sort-connectors/pulsar/src/main/java/org/apache/inlong/sort/pulsar/table/DynamicPulsarDeserializationSchema.java:
##########
@@ -0,0 +1,347 @@
+/*
+ *  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.pulsar.table;
+
+import static org.apache.inlong.sort.base.Constants.DELIMITER;
+import static org.apache.inlong.sort.base.Constants.NUM_BYTES_IN;
+import static org.apache.inlong.sort.base.Constants.NUM_BYTES_IN_PER_SECOND;
+import static org.apache.inlong.sort.base.Constants.NUM_RECORDS_IN;
+import static org.apache.inlong.sort.base.Constants.NUM_RECORDS_IN_PER_SECOND;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import 
org.apache.flink.streaming.connectors.pulsar.table.PulsarDynamicTableSource;
+import org.apache.flink.streaming.util.serialization.FlinkSchema;
+import 
org.apache.flink.streaming.util.serialization.PulsarDeserializationSchema;
+import 
org.apache.flink.streaming.util.serialization.ThreadSafeDeserializationSchema;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.DeserializationException;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.Preconditions;
+import org.apache.inlong.sort.base.metric.SourceMetricData;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.Schema;
+
+/**
+ * A specific {@link PulsarDeserializationSchema} for {@link 
PulsarDynamicTableSource}.
+ */
+class DynamicPulsarDeserializationSchema implements 
PulsarDeserializationSchema<RowData> {
+
+    private static final long serialVersionUID = 1L;
+
+    @Nullable
+    private final DeserializationSchema<RowData> keyDeserialization;
+
+    private final DeserializationSchema<RowData> valueDeserialization;
+
+    private final boolean hasMetadata;
+
+    private final OutputProjectionCollector outputCollector;
+
+    private final TypeInformation<RowData> producedTypeInfo;
+
+    private final boolean upsertMode;
+
+    private SourceMetricData sourceMetricData;
+
+    private String inlongMetric;
+
+    private static final ThreadLocal<SimpleCollector<RowData>> tlsCollector =
+            new ThreadLocal<SimpleCollector<RowData>>() {
+                @Override
+                public SimpleCollector initialValue() {
+                    return new SimpleCollector();
+                }
+            };
+
+    DynamicPulsarDeserializationSchema(
+            int physicalArity,
+            @Nullable DeserializationSchema<RowData> keyDeserialization,
+            int[] keyProjection,
+            DeserializationSchema<RowData> valueDeserialization,
+            int[] valueProjection,
+            boolean hasMetadata,
+            MetadataConverter[] metadataConverters,
+            TypeInformation<RowData> producedTypeInfo,
+            boolean upsertMode,
+    String inlongMetric) {
+        if (upsertMode) {
+            Preconditions.checkArgument(
+                    keyDeserialization != null && keyProjection.length > 0,
+                    "Key must be set in upsert mode for deserialization 
schema.");
+        }
+        this.keyDeserialization = 
ThreadSafeDeserializationSchema.of(keyDeserialization);
+        this.valueDeserialization = 
ThreadSafeDeserializationSchema.of(valueDeserialization);
+        this.hasMetadata = hasMetadata;
+        this.outputCollector = new OutputProjectionCollector(
+                physicalArity,
+                keyProjection,
+                valueProjection,
+                metadataConverters,
+                upsertMode);
+        this.producedTypeInfo = producedTypeInfo;
+        this.upsertMode = upsertMode;
+        this.inlongMetric = inlongMetric;
+    }
+
+    @Override
+    public void open(DeserializationSchema.InitializationContext context) 
throws Exception {
+        if (keyDeserialization != null) {
+            keyDeserialization.open(context);
+        }
+        valueDeserialization.open(context);
+
+        sourceMetricData = new SourceMetricData(context.getMetricGroup());
+        if (inlongMetric != null && !inlongMetric.isEmpty()) {
+            String[] inLongMetricArray = inlongMetric.split(DELIMITER);
+            String groupId = inLongMetricArray[0];
+            String streamId = inLongMetricArray[1];
+            String nodeId = inLongMetricArray[2];
+            sourceMetricData.registerMetricsForNumBytesIn(groupId,
+                streamId, nodeId, NUM_BYTES_IN);
+            sourceMetricData.registerMetricsForNumBytesInPerSecond(groupId,
+                streamId, nodeId, NUM_BYTES_IN_PER_SECOND);
+            sourceMetricData.registerMetricsForNumRecordsIn(groupId, streamId,
+                nodeId, NUM_RECORDS_IN);
+            sourceMetricData.registerMetricsForNumBytesInPerSecond(groupId, 
streamId,
+                nodeId, NUM_RECORDS_IN_PER_SECOND);
+        }
+
+    }
+
+    @Override
+    public boolean isEndOfStream(RowData nextElement) {
+        return false;
+    }
+
+    @Override
+    public RowData deserialize(Message<RowData> message) throws IOException {
+        final SimpleCollector<RowData> collector = tlsCollector.get();
+        deserialize(message, collector);
+        return collector.takeRecord();
+    }
+
+    @Override
+    public void deserialize(Message<RowData> message, Collector<RowData> 
collector) throws IOException {
+        // shortcut in case no output projection is required,
+        // also not for a cartesian product with the keys
+        if (keyDeserialization == null && !hasMetadata) {
+            valueDeserialization.deserialize(message.getData(), collector);
+            return;
+        }

Review Comment:
   here need add metric computing



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to