You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/07/20 23:04:55 UTC

[GitHub] [beam] TheNeuralBit commented on a diff in pull request #22348: Initial Commit for AvroPayloadSerializer

TheNeuralBit commented on code in PR #22348:
URL: https://github.com/apache/beam/pull/22348#discussion_r926118009


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/avro/utils/SerDesUtils.java:
##########
@@ -0,0 +1,218 @@
+/*
+ * 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.beam.sdk.schemas.utils.avro.utils;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.CodeSource;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.avro.Schema;
+import org.apache.avro.SchemaNormalization;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SerDesUtils {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(SerDesUtils.class);
+
+  private static final Map<Schema, Long> SCHEMA_IDS_CACHE = new HashMap<>();
+  private static final String SEP = "_";
+
+  /**
+   * This function will produce a fingerprint for the provided schema.
+   *
+   * @param schema a schema
+   * @return fingerprint for the given schema
+   */
+  public static Long getSchemaFingerprint(Schema schema) {
+    Long schemaId = SCHEMA_IDS_CACHE.get(schema);
+    if (schemaId == null) {
+      schemaId = SchemaNormalization.parsingFingerprint64(schema);
+      SCHEMA_IDS_CACHE.put(schema, schemaId);
+    }
+
+    return schemaId;
+  }
+
+  public static String getSchemaKey(Schema writerSchema, Schema readerSchema) {

Review Comment:
   nit: is this used?



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/AvroUtils.java:
##########
@@ -439,25 +443,29 @@ public static <T> SerializableFunction<Row, T> getFromRowFunction(Class<T> clazz
 
   /** Returns a function mapping encoded AVRO {@link GenericRecord}s to Beam {@link Row}s. */
   public static SimpleFunction<byte[], Row> getAvroBytesToRowFunction(Schema beamSchema) {
-    return new AvroBytesToRowFn(beamSchema);
+    return new FastAvroBytesToRowFn(beamSchema);
   }
 
-  private static class AvroBytesToRowFn extends SimpleFunction<byte[], Row> {
-    private final AvroCoder<GenericRecord> coder;
-    private final Schema beamSchema;
+  private static class FastAvroBytesToRowFn extends SimpleFunction<byte[], Row> {
+    private final SerDesRegistry registry;
+    private final org.apache.avro.Schema avroSchema;
 
-    AvroBytesToRowFn(Schema beamSchema) {
-      org.apache.avro.Schema avroSchema = toAvroSchema(beamSchema);
-      coder = AvroCoder.of(avroSchema);
-      this.beamSchema = beamSchema;
+    FastAvroBytesToRowFn(Schema beamSchema) {
+      avroSchema = toAvroSchema(beamSchema);
+      //Create Registry in Setup
+      registry = SerDesRegistry.getDefaultInstance();
     }
 
     @Override
     public Row apply(byte[] bytes) {
       try {
-        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
-        GenericRecord record = coder.decode(inputStream);
-        return AvroUtils.toBeamRowStrict(record, beamSchema);
+        //Create Avro decoder
+        Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null);
+        //Get Deserializer
+        RowDeserializer<Row> deserializer = registry.buildRowDeserializer(avroSchema, avroSchema);

Review Comment:
   Do we need the registry? Couldn't we just do the code generation once when creating the instance of FastAvroBytesToRowFn?



-- 
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: github-unsubscribe@beam.apache.org

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