All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.atleon.schemaregistry.confluent.AvroRegistrySerializer Maven / Gradle / Ivy

There is a newer version: 0.28.3
Show newest version
package io.atleon.schemaregistry.confluent;

import io.atleon.avro.AtleonReflectData;
import io.atleon.avro.AvroSerializer;
import io.atleon.avro.GenericDatas;
import io.confluent.kafka.schemaregistry.ParsedSchema;
import io.confluent.kafka.schemaregistry.SchemaProvider;
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
import io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;

import java.util.Map;

/**
 * A {@link RegistrySerializer} that uses Avro and delegates to {@link AvroSerializer}
 *
 * @param  The type of data serialized by this serializer
 */
public final class AvroRegistrySerializer extends RegistrySerializer {

    private AvroSerializer serializer;

    @Override
    public void configure(Map properties) {
        configure(new AvroRegistrySerializerConfig(properties));
    }

    public void configure(AvroRegistrySerializerConfig config) {
        super.configure(config);
        this.serializer = createSerializer(config);
    }

    @Override
    protected SchemaProvider createSchemaProvider() {
        return new AvroSchemaProvider();
    }

    @Override
    protected AvroSerializer serializer() {
        return serializer;
    }

    @Override
    protected ParsedSchema toParsedSchema(Schema schema) {
        return new AvroSchema(schema);
    }

    private AvroSerializer createSerializer(AvroRegistrySerializerConfig config) {
        return AvroSerializer.create(createGenericData(config))
            .withSchemaCachingEnabled(config.schemaCachingEnabled())
            .withSchemaGenerationEnabled(config.schemaGenerationEnabled())
            .withRemoveJavaProperties(config.removeJavaProperties());
    }

    private GenericData createGenericData(AvroRegistrySerializerConfig config) {
        GenericData genericData = instantiateGenericData(config);
        if (config.useLogicalTypeConverters()) {
            GenericDatas.addLogicalTypeConversion(genericData);
        }
        return genericData;
    }

    private GenericData instantiateGenericData(AvroRegistrySerializerConfig config) {
        if (useSchemaReflection) {
            return config.reflectionAllowNull() ? new AtleonReflectData.AllowNull() : new AtleonReflectData();
        } else {
            return new GenericData();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy