
io.pravega.schemaregistry.serializer.avro.impl.AvroSerializerFactory Maven / Gradle / Ivy
/**
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed 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
*/
package io.pravega.schemaregistry.serializer.avro.impl;
import com.google.common.base.Preconditions;
import io.pravega.client.stream.Serializer;
import io.pravega.schemaregistry.serializer.avro.schemas.AvroSchema;
import io.pravega.schemaregistry.client.SchemaRegistryClient;
import io.pravega.schemaregistry.common.Either;
import io.pravega.schemaregistry.contract.data.EncodingInfo;
import io.pravega.schemaregistry.serializer.shared.impl.AbstractDeserializer;
import io.pravega.schemaregistry.serializer.shared.impl.AbstractSerializer;
import io.pravega.schemaregistry.serializer.shared.impl.EncodingCache;
import io.pravega.schemaregistry.serializer.shared.impl.MultiplexedAndGenericDeserializer;
import io.pravega.schemaregistry.serializer.shared.impl.MultiplexedDeserializer;
import io.pravega.schemaregistry.serializer.shared.impl.MultiplexedSerializer;
import io.pravega.schemaregistry.serializer.shared.impl.SerializerConfig;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.generic.GenericRecord;
import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.stream.Collectors;
import static io.pravega.schemaregistry.serializer.shared.impl.SerializerFactoryHelper.initForDeserializer;
import static io.pravega.schemaregistry.serializer.shared.impl.SerializerFactoryHelper.initForSerializer;
/**
* Internal Factory class for Avro serializers and deserializers.
*/
@Slf4j
public class AvroSerializerFactory {
/**
* Creates a typed Avro serializer for the schema. The serializer implementation returned from this method is
* responsible for interacting with schema registry service and ensures that only valid registered schema can be used.
*
* Note: the returned serializer only implements {@link Serializer#serialize(Object)}.
* It does not implement {@link Serializer#deserialize(ByteBuffer)}.
*
* @param config Serializer Config used for instantiating a new serializer.
* @param schema Schema container that encapsulates an AvroSchema
* @param Type of event. It accepts either POJO or Avro generated classes and serializes them.
* @return A Serializer Implementation that can be used in {@link io.pravega.client.stream.EventStreamWriter} or
* {@link io.pravega.client.stream.TransactionalEventStreamWriter}.
*/
public static Serializer serializer(@NonNull SerializerConfig config, @NonNull AvroSchema schema) {
Preconditions.checkArgument(config.isWriteEncodingHeader(), "Events should be tagged with encoding ids.");
SchemaRegistryClient schemaRegistryClient = initForSerializer(config);
String groupId = config.getGroupId();
return new AvroSerializer<>(groupId, schemaRegistryClient, schema, config.getEncoder(), config.isRegisterSchema());
}
/**
* Creates a typed avro deserializer for the Schema. The deserializer implementation returned from this method is
* responsible for interacting with schema registry service and validate the writer schema before using it.
*
* Note: the returned serializer only implements {@link Serializer#deserialize(ByteBuffer)}.
* It does not implement {@link Serializer#serialize(Object)}.
*
* @param config Serializer Config used for instantiating a new serializer.
* @param schema Schema container that encapsulates an AvroSchema
* @param Type of event. The typed event should be an avro generated class. For generic type use
* {@link #genericDeserializer(SerializerConfig, AvroSchema)}
* @return A deserializer Implementation that can be used in {@link io.pravega.client.stream.EventStreamReader}.
*/
public static Serializer deserializer(@NonNull SerializerConfig config, @NonNull AvroSchema schema) {
Preconditions.checkArgument(config.isWriteEncodingHeader(), "Events should be tagged with encoding ids.");
SchemaRegistryClient schemaRegistryClient = initForDeserializer(config);
String groupId = config.getGroupId();
EncodingCache encodingCache = new EncodingCache(groupId, schemaRegistryClient);
return new AvroDeserializer<>(groupId, schemaRegistryClient, schema, config.getDecoders(), encodingCache);
}
/**
* Creates a generic avro deserializer. It has the optional parameter for schema.
* If the schema is not supplied, the writer schema is used for deserialization into {@link GenericRecord}.
*
* Note: the returned serializer only implements {@link Serializer#deserialize(ByteBuffer)}.
* It does not implement {@link Serializer#serialize(Object)}.
*
* @param config Serializer Config used for instantiating a new serializer.
* @param schema Schema container that encapsulates an AvroSchema. It can be null to indicate that writer schema should
* be used for deserialization.
* @return A deserializer Implementation that can be used in {@link io.pravega.client.stream.EventStreamReader}.
*/
public static Serializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy