You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/07/31 06:25:02 UTC

[GitHub] [rocketmq-schema-registry] ferrirW commented on a diff in pull request #16: add avro schema serializer and deserializer

ferrirW commented on code in PR #16:
URL: https://github.com/apache/rocketmq-schema-registry/pull/16#discussion_r933932704


##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serializer/AbstractAvroDeserializer.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.rocketmq.schema.registry.client.serializer;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.BinaryDecoder;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.specific.SpecificDatumReader;
+import org.apache.rocketmq.schema.registry.client.SchemaRegistryClient;
+import org.apache.rocketmq.schema.registry.client.exceptions.RestClientException;
+import org.apache.rocketmq.schema.registry.client.exceptions.SerializationException;
+import org.apache.rocketmq.schema.registry.common.dto.SchemaRecordDto;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public class AbstractAvroDeserializer<T> {
+
+    Logger log = LoggerFactory.getLogger(AvroDeserializer.class);
+
+    private final DecoderFactory decoderFactory = DecoderFactory.get();
+    protected SchemaRegistryClient schemaRegistry;
+
+    protected T deserializeImpl(String subject, byte[] payload)
+            throws SerializationException {
+        if (schemaRegistry == null) {
+            throw new SerializationException("please initialize the schema registry client first");
+        }
+        if (payload == null) {
+            return null;
+        }
+
+        try {
+            SchemaRecordDto schemaRecordDto = schemaRegistry.getSchemaBySubject(subject);
+            Schema schema = new Schema.Parser().parse(schemaRecordDto.getIdl());
+            return avroDecode(payload, schema);
+        } catch (RestClientException | IOException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+    public T avroDecode(byte[] input, Schema schema) throws IOException {
+        ByteArrayInputStream bais = new ByteArrayInputStream(input);
+        BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(bais, null);
+
+        ByteBuffer buffer = ByteBuffer.allocate(16);
+        try {
+            decoder.readBytes(buffer);
+        } catch (Exception e) {
+            log.error("read bytes error: ", e);
+        }
+
+        long id = buffer.getLong();

Review Comment:
   id + version



##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serializer/AbstractAvroSerializer.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.rocketmq.schema.registry.client.serializer;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.DatumWriter;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.specific.SpecificDatumWriter;
+import org.apache.rocketmq.schema.registry.client.SchemaRegistryClient;
+import org.apache.rocketmq.schema.registry.client.exceptions.RestClientException;
+import org.apache.rocketmq.schema.registry.client.exceptions.SerializationException;
+import org.apache.rocketmq.schema.registry.common.dto.SchemaRecordDto;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.ExecutionException;
+
+public class AbstractAvroSerializer<T> {
+
+    private static final int SCHEMA_ID_LENGTH = 16;
+    protected SchemaRegistryClient schemaRegistry;
+    private final EncoderFactory encoderFactory = EncoderFactory.get();
+
+    protected byte[] serializeImpl(
+            String subject, T originMessage)
+            throws SerializationException {
+        if (schemaRegistry == null) {
+            throw new SerializationException("please initialize the schema registry client first");
+        }
+
+        if (originMessage == null) {
+            return null;
+        }
+
+        try {
+
+        } catch (Exception e) {
+            throw new SerializationException("get schema by subject failed", e);
+        }
+
+        try {
+            SchemaRecordDto schemaRecordDto = getSchemaBySubject(subject);
+            long schemaId = schemaRecordDto.getSchemaId();

Review Comment:
   in our design, we use id + version to place a schema record, so you should write version in header too



-- 
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: dev-unsubscribe@rocketmq.apache.org

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