[ 
https://issues.apache.org/jira/browse/FLINK-9679?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16571399#comment-16571399
 ] 

ASF GitHub Bot commented on FLINK-9679:
---------------------------------------

dawidwys commented on a change in pull request #6259: [FLINK-9679] Implement 
AvroSerializationSchema
URL: https://github.com/apache/flink/pull/6259#discussion_r208166496
 
 

 ##########
 File path: 
flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/AvroSerializationSchema.java
 ##########
 @@ -0,0 +1,134 @@
+/*
+ * 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;
+
+import org.apache.flink.api.common.serialization.SerializationSchema;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.specific.SpecificData;
+import org.apache.avro.specific.SpecificDatumWriter;
+import org.apache.avro.specific.SpecificRecord;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * Serialization schema that serializes from Avro binary format.
+ *
+ * @param <T> type of record it produces
+ */
+public class AvroSerializationSchema<T> implements SerializationSchema<T> {
+
+       /**
+        * Creates {@link AvroSerializationSchema} that produces {@link 
SpecificRecord} using provided schema.
+        *
+        * @param tClass class of record to be produced
+        * @return topic record in form of {@link SpecificRecord}
+        */
+       public static <T extends SpecificRecord> AvroSerializationSchema<T> 
forSpecific(Class<T> tClass) {
+               return new AvroSerializationSchema<>(tClass);
+       }
+
+       private static final long serialVersionUID = -8766681879020862312L;
+
+       /**
+        * Class to serialize from.
+        */
+       private Class<T> recordClazz;
+
+       /**
+        * Write the serializes byte array into a record.
+        */
+       private transient GenericDatumWriter<T> datumWriter;
+
+       /**
+        * Output stream to write message to.
+        */
+       private transient ByteArrayOutputStream arrayOutputStream;
+
+       /**
+        * Avro encoder that encodes binary data.
+        */
+       private transient BinaryEncoder encoder;
+
+       /**
+        * Creates Avro Serialization schema.
+        *
+        * @param recordClazz         class to which serialize which is
+        *                            {@link SpecificRecord}.
+        */
+
+       AvroSerializationSchema(Class<T> recordClazz) {
+               Preconditions.checkNotNull(recordClazz, "Avro record class must 
not be null.");
+               this.recordClazz = recordClazz;
+
+       }
+
+       BinaryEncoder getEncoder() {
+               return encoder;
+       }
+
+       GenericDatumWriter<T> getDatumWriter() {
+               return datumWriter;
+       }
+
+       ByteArrayOutputStream getOutputStream() {
+               return arrayOutputStream;
+       }
+
+       @Override
+       public byte[] serialize(T object) {
+               checkAvroInitialized();
+
+               if (object == null) {
+                       return null;
+               } else {
+                       try {
+                               arrayOutputStream.write(0);
+                               datumWriter.write(object, encoder);
+                               encoder.flush();
+                               byte[] bytes = arrayOutputStream.toByteArray();
+                               arrayOutputStream.close();
+                               return bytes;
+                       } catch (RuntimeException | IOException e) {
 
 Review comment:
   See no point in catching the `RuntimeException`. Use 
`WrappingRuntimeException` for `IOException`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


> Implement AvroSerializationSchema
> ---------------------------------
>
>                 Key: FLINK-9679
>                 URL: https://issues.apache.org/jira/browse/FLINK-9679
>             Project: Flink
>          Issue Type: Improvement
>    Affects Versions: 1.6.0
>            Reporter: Yazdan Shirvany
>            Assignee: Yazdan Shirvany
>            Priority: Major
>              Labels: pull-request-available
>
> Implement AvroSerializationSchema using Confluent Schema Registry



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to