You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/11/09 15:32:58 UTC

[GitHub] [camel-kafka-connector] valdar commented on a change in pull request #686: Added PojoToSchemaAndStructTransform to infer POJO kafka connec schem…

valdar commented on a change in pull request #686:
URL: https://github.com/apache/camel-kafka-connector/pull/686#discussion_r519902255



##########
File path: core/src/main/java/org/apache/camel/kafkaconnector/transforms/PojoToSchemaAndStructTransform.java
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.camel.kafkaconnector.transforms;
+
+import java.io.IOException;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.avro.AvroFactory;
+import com.fasterxml.jackson.dataformat.avro.AvroSchema;
+import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;
+import io.apicurio.registry.utils.converter.avro.AvroData;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.kafka.common.config.ConfigDef;
+import org.apache.kafka.connect.connector.ConnectRecord;
+import org.apache.kafka.connect.data.SchemaAndValue;
+import org.apache.kafka.connect.transforms.Transformation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PojoToSchemaAndStructTransform <R extends ConnectRecord<R>> implements Transformation<R> {
+    private static final Logger LOG = LoggerFactory.getLogger(PojoToSchemaAndStructTransform.class);
+
+    @Override
+    public R apply(R r) {
+        LOG.debug("Incoming record: " + r);
+
+        ObjectMapper mapper = new ObjectMapper(new AvroFactory());
+        AvroSchemaGenerator gen = new AvroSchemaGenerator();
+
+        try {
+            mapper.acceptJsonFormatVisitor(r.value().getClass(), gen);
+        } catch (JsonMappingException e) {
+            e.printStackTrace();
+        }
+
+        AvroSchema schemaWrapper = gen.getGeneratedSchema();
+        org.apache.avro.Schema avroSchema = schemaWrapper.getAvroSchema();
+        LOG.debug("Generated avro schema: " + avroSchema.toString(true));
+
+        SchemaAndValue connectSchemaAndData = null;
+        try {
+            byte[] avroDataByte = mapper.writer(schemaWrapper).writeValueAsBytes(r.value());
+            Decoder decoder = DecoderFactory.get().binaryDecoder(avroDataByte, null);
+            DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(avroSchema);
+            GenericRecord genericAvroData = datumReader.read(null, decoder);
+
+            AvroData ad = new AvroData(1000);
+            connectSchemaAndData = ad.toConnectData(avroSchema, genericAvroData);

Review comment:
       Probably in the context of using it for one connector, not sure how is it handled for multiple connectors... in theory the class should be loaded once per connector classpath...




----------------------------------------------------------------
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:
users@infra.apache.org