dannycranmer commented on a change in pull request #14737:
URL: https://github.com/apache/flink/pull/14737#discussion_r574342605



##########
File path: 
flink-formats/flink-avro-glue-schema-registry/src/main/java/org/apache/flink/formats/avro/glue/schema/registry/GlueSchemaRegistryInputStreamDeserializer.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.flink.formats.avro.glue.schema.registry;
+
+import org.apache.flink.formats.avro.utils.MutableByteArrayInputStream;
+
+import com.amazonaws.services.schemaregistry.deserializers.AWSDeserializer;
+import 
com.amazonaws.services.schemaregistry.exception.AWSSchemaRegistryException;
+import org.apache.avro.Schema;
+import org.apache.avro.SchemaParseException;
+import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+/**
+ * AWS Glue Schema Registry input stream de-serializer to accept input stream 
and extract schema
+ * from it and remove schema registry information in the input stream.
+ */
+public class GlueSchemaRegistryInputStreamDeserializer {
+    private final AWSDeserializer awsDeserializer;
+
+    /**
+     * Constructor accepts configuration map for AWS Deserializer.
+     *
+     * @param configs configuration map
+     */
+    public GlueSchemaRegistryInputStreamDeserializer(Map<String, Object> 
configs) {
+        awsDeserializer =
+                AWSDeserializer.builder()
+                        
.credentialProvider(DefaultCredentialsProvider.builder().build())
+                        .configs(configs)
+                        .build();
+    }
+
+    public GlueSchemaRegistryInputStreamDeserializer(AWSDeserializer 
awsDeserializer) {
+        this.awsDeserializer = awsDeserializer;
+    }
+
+    /**
+     * Get schema and remove extra Schema Registry information within input 
stream.
+     *
+     * @param in input stream
+     * @return schema of object within input stream
+     * @throws IOException Exception during decompression
+     */
+    public Schema getSchemaAndDeserializedStream(InputStream in) throws 
IOException {
+        byte[] inputBytes = new byte[in.available()];
+        in.read(inputBytes);
+        in.reset();
+
+        MutableByteArrayInputStream mutableByteArrayInputStream = 
(MutableByteArrayInputStream) in;
+        String schemaDefinition = 
awsDeserializer.getSchema(inputBytes).getSchemaDefinition();
+        byte[] deserializedBytes = awsDeserializer.getActualData(inputBytes);
+        mutableByteArrayInputStream.setBuffer(deserializedBytes);
+
+        Schema schema;
+        try {
+            schema = (new Schema.Parser()).parse(schemaDefinition);

Review comment:
       The question was focusing on the frequency of deserialisation. To 
improve performance can we deserialise schema for specific info once, and cache 
it? Or are we expecting the schema definition to change over time? What happens 
if the schema changes for a `SpecificRecord`? Is the idea that the Flink job 
would fail if the upstream data format changes in a non compatible way?




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

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


Reply via email to