Search in sources :

Example 1 with SpecificRecordBase

use of org.apache.avro.specific.SpecificRecordBase in project schema-registry by pravega.

the class SerializerTest method testAvroSerializers.

@Test
public void testAvroSerializers() {
    SchemaRegistryClient client = mock(SchemaRegistryClient.class);
    SerializerConfig config = SerializerConfig.builder().registryClient(client).groupId("groupId").build();
    AvroSchema<Test1> schema1 = AvroSchema.of(Test1.class);
    AvroSchema<Test2> schema2 = AvroSchema.of(Test2.class);
    VersionInfo versionInfo1 = new VersionInfo("name", SerializationFormat.Avro.getFullTypeName(), 0, 0);
    VersionInfo versionInfo2 = new VersionInfo("name", SerializationFormat.Avro.getFullTypeName(), 1, 1);
    doAnswer(x -> GroupProperties.builder().serializationFormat(SerializationFormat.Any).build()).when(client).getGroupProperties(anyString());
    doAnswer(x -> versionInfo1).when(client).getVersionForSchema(anyString(), eq(schema1.getSchemaInfo()));
    doAnswer(x -> versionInfo2).when(client).getVersionForSchema(anyString(), eq(schema2.getSchemaInfo()));
    doAnswer(x -> new EncodingId(0)).when(client).getEncodingId(anyString(), eq(versionInfo1), any());
    doAnswer(x -> new EncodingId(1)).when(client).getEncodingId(anyString(), eq(versionInfo2), any());
    doAnswer(x -> new EncodingInfo(versionInfo1, schema1.getSchemaInfo(), Codecs.None.getCodec().getCodecType())).when(client).getEncodingInfo(anyString(), eq(new EncodingId(0)));
    doAnswer(x -> new EncodingInfo(versionInfo2, schema2.getSchemaInfo(), Codecs.None.getCodec().getCodecType())).when(client).getEncodingInfo(anyString(), eq(new EncodingId(1)));
    doAnswer(x -> true).when(client).canReadUsing(anyString(), any());
    AvroSchema<Object> of = AvroSchema.of(SchemaDefinitions.ENUM);
    VersionInfo versionInfo3 = new VersionInfo(of.getSchema().getFullName(), SerializationFormat.Avro.getFullTypeName(), 0, 2);
    doAnswer(x -> versionInfo3).when(client).getVersionForSchema(anyString(), eq(of.getSchemaInfo()));
    doAnswer(x -> new EncodingId(2)).when(client).getEncodingId(anyString(), eq(versionInfo3), any());
    doAnswer(x -> new EncodingInfo(versionInfo3, of.getSchemaInfo(), Codecs.None.getCodec().getCodecType())).when(client).getEncodingInfo(anyString(), eq(new EncodingId(2)));
    Serializer<Object> serializerStr = AvroSerializerFactory.serializer(config, of);
    GenericData.EnumSymbol enumSymbol = new GenericData.EnumSymbol(of.getSchema(), "a");
    ByteBuffer serialized1 = serializerStr.serialize(enumSymbol);
    Serializer<Object> deserializer1 = AvroSerializerFactory.deserializer(config, of);
    Object deserializedEnum = deserializer1.deserialize(serialized1);
    assertEquals(deserializedEnum, enumSymbol);
    Serializer<Test1> serializer = AvroSerializerFactory.serializer(config, schema1);
    Test1 test1 = new Test1("name", 1);
    ByteBuffer serialized = serializer.serialize(test1);
    Serializer<Test1> deserializer = AvroSerializerFactory.deserializer(config, schema1);
    Test1 deserialized = deserializer.deserialize(serialized);
    assertEquals(deserialized, test1);
    serialized = serializer.serialize(test1);
    Serializer<Object> genericDeserializer = AvroSerializerFactory.genericDeserializer(config, null);
    Object genericDeserialized = genericDeserializer.deserialize(serialized);
    assertTrue(genericDeserialized instanceof GenericRecord);
    assertEquals(((GenericRecord) genericDeserialized).get("name").toString(), "name");
    assertEquals(((GenericRecord) genericDeserialized).get("field1"), 1);
    // multi type
    Test2 test2 = new Test2("name", 1, "2");
    AvroSchema<SpecificRecordBase> schema1Base = AvroSchema.ofSpecificRecord(Test1.class);
    AvroSchema<SpecificRecordBase> schema2Base = AvroSchema.ofSpecificRecord(Test2.class);
    Map<Class<? extends SpecificRecordBase>, AvroSchema<SpecificRecordBase>> map = new HashMap<>();
    map.put(Test1.class, schema1Base);
    map.put(Test2.class, schema2Base);
    Serializer<SpecificRecordBase> multiSerializer = AvroSerializerFactory.multiTypeSerializer(config, map);
    serialized = multiSerializer.serialize(test1);
    Serializer<SpecificRecordBase> multiDeserializer = AvroSerializerFactory.multiTypeDeserializer(config, map);
    SpecificRecordBase deserialized2 = multiDeserializer.deserialize(serialized);
    assertEquals(deserialized2, test1);
    serialized = multiSerializer.serialize(test2);
    deserialized2 = multiDeserializer.deserialize(serialized);
    assertEquals(deserialized2, test2);
    Map<Class<? extends SpecificRecordBase>, AvroSchema<SpecificRecordBase>> map2 = new HashMap<>();
    map2.put(Test1.class, schema1Base);
    Serializer<Either<SpecificRecordBase, Object>> fallbackDeserializer = AvroSerializerFactory.typedOrGenericDeserializer(config, map2);
    serialized = multiSerializer.serialize(test1);
    Either<SpecificRecordBase, Object> fallback = fallbackDeserializer.deserialize(serialized);
    assertTrue(fallback.isLeft());
    assertEquals(fallback.getLeft(), test1);
    serialized = multiSerializer.serialize(test2);
    fallback = fallbackDeserializer.deserialize(serialized);
    assertTrue(fallback.isRight());
}
Also used : AvroSchema(io.pravega.schemaregistry.serializer.avro.schemas.AvroSchema) HashMap(java.util.HashMap) Test2(io.pravega.schemaregistry.serializer.avro.testobjs.generated.Test2) EncodingId(io.pravega.schemaregistry.contract.data.EncodingId) Either(io.pravega.schemaregistry.common.Either) GenericRecord(org.apache.avro.generic.GenericRecord) EncodingInfo(io.pravega.schemaregistry.contract.data.EncodingInfo) SpecificRecordBase(org.apache.avro.specific.SpecificRecordBase) GenericData(org.apache.avro.generic.GenericData) ByteBuffer(java.nio.ByteBuffer) Test1(io.pravega.schemaregistry.serializer.avro.testobjs.generated.Test1) VersionInfo(io.pravega.schemaregistry.contract.data.VersionInfo) SerializerConfig(io.pravega.schemaregistry.serializer.shared.impl.SerializerConfig) SchemaRegistryClient(io.pravega.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test)

Example 2 with SpecificRecordBase

use of org.apache.avro.specific.SpecificRecordBase in project pipelines by gbif.

the class GbifJsonConverter method getMultimediaConverter.

/**
 * String converter for {@link MultimediaRecord}, convert an object to specific string view * *
 *
 * <pre>{@code
 * Result example:
 *
 * {
 *  "id": "777",
 *  "multimediaItems": [
 *    {
 *      "type": "StillImage",
 *      "format": "image/jpeg",
 *      "identifier": "http://arctos.database.museum/media/10436011?open"
 *     },
 *     {
 *      "type": "MovingImage",
 *      "format": "video/mp4",
 *      "identifier": "http://arctos.database.museum/media/10436025?open"
 *      }
 *  ],
 *  "mediaTypes": [
 *     "StillImage",
 *     "MovingImage"
 *  ]
 *
 * }</pre>
 */
private BiConsumer<JsonConverter, SpecificRecordBase> getMultimediaConverter() {
    return (jc, record) -> {
        MultimediaRecord mr = (MultimediaRecord) record;
        if (!skipId) {
            jc.addJsonTextFieldNoCheck(Indexing.ID, mr.getId());
        }
        // multimedia items
        if (mr.getMultimediaItems() != null && !mr.getMultimediaItems().isEmpty()) {
            List<ObjectNode> items = mr.getMultimediaItems().stream().map(item -> {
                ObjectNode node = JsonConverter.createObjectNode();
                BiConsumer<String, String> addField = (k, v) -> Optional.ofNullable(v).filter(f -> !f.isEmpty()).ifPresent(r -> node.put(k, r));
                addField.accept("type", item.getType());
                addField.accept("format", item.getFormat());
                addField.accept("identifier", item.getIdentifier());
                addField.accept("audience", item.getAudience());
                addField.accept("contributor", item.getContributor());
                addField.accept("created", item.getCreated());
                addField.accept("creator", item.getCreator());
                addField.accept("description", item.getDescription());
                addField.accept("license", item.getLicense());
                addField.accept("publisher", item.getPublisher());
                addField.accept("references", item.getReferences());
                addField.accept("rightsHolder", item.getRightsHolder());
                addField.accept("source", item.getSource());
                addField.accept("title", item.getTitle());
                return node;
            }).collect(Collectors.toList());
            jc.addJsonArray("multimediaItems", items);
            // media types
            Set<TextNode> mediaTypes = mr.getMultimediaItems().stream().filter(i -> !Strings.isNullOrEmpty(i.getType())).map(Multimedia::getType).map(TextNode::valueOf).collect(Collectors.toSet());
            jc.addJsonArray("mediaTypes", mediaTypes);
            // media licenses
            Set<TextNode> mediaLicenses = mr.getMultimediaItems().stream().filter(i -> !Strings.isNullOrEmpty(i.getLicense())).map(Multimedia::getLicense).map(TextNode::valueOf).collect(Collectors.toSet());
            jc.addJsonArray("mediaLicenses", mediaLicenses);
        }
    };
}
Also used : AmplificationRecord(org.gbif.pipelines.io.avro.AmplificationRecord) Arrays(java.util.Arrays) Singular(lombok.Singular) StringToDateFunctions(org.gbif.pipelines.core.parsers.temporal.StringToDateFunctions) BasicRecord(org.gbif.pipelines.io.avro.BasicRecord) Multimedia(org.gbif.pipelines.io.avro.Multimedia) TemporalConverter(org.gbif.pipelines.core.utils.TemporalConverter) TemporalAccessorUtils(org.gbif.common.parsers.date.TemporalAccessorUtils) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) BlastResult(org.gbif.pipelines.io.avro.BlastResult) License(org.gbif.api.vocabulary.License) DwcTerm(org.gbif.dwc.terms.DwcTerm) Indexing(org.gbif.pipelines.common.PipelinesVariables.Pipeline.Indexing) Extension(org.gbif.api.vocabulary.Extension) Set(java.util.Set) GbifInternalTerm(org.gbif.dwc.terms.GbifInternalTerm) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) TextNode(com.fasterxml.jackson.databind.node.TextNode) ZoneId(java.time.ZoneId) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) GbifTerm(org.gbif.dwc.terms.GbifTerm) Builder(lombok.Builder) GadmFeatures(org.gbif.pipelines.io.avro.GadmFeatures) Entry(java.util.Map.Entry) Optional(java.util.Optional) TemporalRecord(org.gbif.pipelines.io.avro.TemporalRecord) TaxonRecord(org.gbif.pipelines.io.avro.TaxonRecord) MultimediaRecord(org.gbif.pipelines.io.avro.MultimediaRecord) LocationRecord(org.gbif.pipelines.io.avro.LocationRecord) IntNode(com.fasterxml.jackson.databind.node.IntNode) LocalDateTime(java.time.LocalDateTime) TermFactory(org.gbif.dwc.terms.TermFactory) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExtendedRecord(org.gbif.pipelines.io.avro.ExtendedRecord) Strings(com.google.common.base.Strings) Issues(org.gbif.pipelines.io.avro.Issues) BiConsumer(java.util.function.BiConsumer) RankedName(org.gbif.pipelines.io.avro.RankedName) GrscicollRecord(org.gbif.pipelines.io.avro.grscicoll.GrscicollRecord) ModelUtils(org.gbif.pipelines.core.utils.ModelUtils) LocationFeatureRecord(org.gbif.pipelines.io.avro.LocationFeatureRecord) Term(org.gbif.dwc.terms.Term) LongFunction(java.util.function.LongFunction) SpecificRecordBase(org.apache.avro.specific.SpecificRecordBase) OccurrenceIssue(org.gbif.api.vocabulary.OccurrenceIssue) StringJoiner(java.util.StringJoiner) Collections(java.util.Collections) MultimediaRecord(org.gbif.pipelines.io.avro.MultimediaRecord) Set(java.util.Set) HashSet(java.util.HashSet) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) List(java.util.List) ArrayList(java.util.ArrayList) TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 3 with SpecificRecordBase

use of org.apache.avro.specific.SpecificRecordBase in project hudi by apache.

the class HoodieAvroUtils method rewriteRecord.

public static GenericRecord rewriteRecord(GenericRecord genericRecord, Schema newSchema, boolean copyOverMetaFields, GenericRecord fallbackRecord) {
    GenericRecord newRecord = new GenericData.Record(newSchema);
    boolean isSpecificRecord = genericRecord instanceof SpecificRecordBase;
    for (Schema.Field f : newSchema.getFields()) {
        if (!(isSpecificRecord && isMetadataField(f.name()))) {
            copyOldValueOrSetDefault(genericRecord, newRecord, f);
        }
        if (isMetadataField(f.name()) && copyOverMetaFields) {
            // if meta field exists in primary generic record, copy over.
            if (genericRecord.getSchema().getField(f.name()) != null) {
                copyOldValueOrSetDefault(genericRecord, newRecord, f);
            } else if (fallbackRecord != null && fallbackRecord.getSchema().getField(f.name()) != null) {
                // if not, try to copy from the fallback record.
                copyOldValueOrSetDefault(fallbackRecord, newRecord, f);
            }
        }
    }
    if (!GenericData.get().validate(newSchema, newRecord)) {
        throw new SchemaCompatibilityException("Unable to validate the rewritten record " + genericRecord + " against schema " + newSchema);
    }
    return newRecord;
}
Also used : SpecificRecordBase(org.apache.avro.specific.SpecificRecordBase) SerializableSchema(org.apache.hudi.common.config.SerializableSchema) Schema(org.apache.avro.Schema) SchemaCompatibilityException(org.apache.hudi.exception.SchemaCompatibilityException) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) HoodieRecord(org.apache.hudi.common.model.HoodieRecord) GenericRecord(org.apache.avro.generic.GenericRecord) Field(org.apache.avro.Schema.Field) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 4 with SpecificRecordBase

use of org.apache.avro.specific.SpecificRecordBase in project kafka-tutorials by confluentinc.

the class MultiEventAvroProduceConsumeAppTest method testProduceAvroMultipleEvents.

@Test
@SuppressWarnings("unchecked")
public void testProduceAvroMultipleEvents() {
    KafkaAvroSerializer avroSerializer = new KafkaAvroSerializer();
    avroSerializer.configure(commonConfigs, false);
    MockProducer<String, SpecificRecordBase> mockAvroProducer = new MockProducer<String, SpecificRecordBase>(true, stringSerializer, (Serializer) avroSerializer);
    produceConsumeApp.produceAvroEvents(() -> mockAvroProducer, (String) commonConfigs.get("proto.topic"), produceConsumeApp.avroEvents());
    List<KeyValue<String, SpecificRecordBase>> expectedKeyValues = produceConsumeApp.avroEvents().stream().map((e -> KeyValue.pair((String) e.get("customer_id"), e))).collect(Collectors.toList());
    List<KeyValue<String, SpecificRecordBase>> actualKeyValues = mockAvroProducer.history().stream().map(this::toKeyValue).collect(Collectors.toList());
    assertThat(actualKeyValues, equalTo(expectedKeyValues));
}
Also used : SpecificRecordBase(org.apache.avro.specific.SpecificRecordBase) MockProducer(org.apache.kafka.clients.producer.MockProducer) KeyValue(org.apache.kafka.streams.KeyValue) KafkaAvroSerializer(io.confluent.kafka.serializers.KafkaAvroSerializer) Test(org.junit.Test)

Example 5 with SpecificRecordBase

use of org.apache.avro.specific.SpecificRecordBase in project kafka-tutorials by confluentinc.

the class MultiEventAvroProduceConsumeApp method consumeAvroEvents.

public void consumeAvroEvents(final Supplier<Consumer<String, SpecificRecordBase>> consumerSupplier, final String topic, final List<String> eventTracker) {
    try (Consumer<String, SpecificRecordBase> eventConsumer = consumerSupplier.get()) {
        eventConsumer.subscribe(Collections.singletonList(topic));
        while (keepConsumingAvro) {
            ConsumerRecords<String, SpecificRecordBase> consumerRecords = eventConsumer.poll(Duration.ofSeconds(1));
            consumerRecords.forEach(consumerRec -> {
                SpecificRecord avroRecord = consumerRec.value();
                if (avroRecord instanceof Purchase) {
                    Purchase purchase = (Purchase) avroRecord;
                    eventTracker.add(String.format("Avro Purchase event -> %s", purchase.getItem()));
                } else if (avroRecord instanceof PageView) {
                    PageView pageView = (PageView) avroRecord;
                    eventTracker.add(String.format("Avro Pageview event -> %s", pageView.getUrl()));
                } else {
                    LOG.error("Unexpected type - this shouldn't happen");
                }
            });
        }
    }
}
Also used : SpecificRecordBase(org.apache.avro.specific.SpecificRecordBase) PageView(io.confluent.developer.avro.PageView) Purchase(io.confluent.developer.avro.Purchase) SpecificRecord(org.apache.avro.specific.SpecificRecord)

Aggregations

SpecificRecordBase (org.apache.avro.specific.SpecificRecordBase)20 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Message (co.airy.avro.communication.Message)5 Metadata (co.airy.avro.communication.Metadata)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Schema (org.apache.avro.Schema)5 GenericRecord (org.apache.avro.generic.GenericRecord)5 Test (org.junit.Test)5 Channel (co.airy.avro.communication.Channel)4 MetadataRepository.newMessageMetadata (co.airy.model.metadata.MetadataRepository.newMessageMetadata)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Instant (java.time.Instant)4 ChannelConnectionState (co.airy.avro.communication.ChannelConnectionState)3 DeliveryState (co.airy.avro.communication.DeliveryState)3 ApplicationCommunicationMetadata (co.airy.kafka.schema.application.ApplicationCommunicationMetadata)3 AiryLoggerFactory (co.airy.log.AiryLoggerFactory)3 MetadataKeys (co.airy.model.metadata.MetadataKeys)3 Map (java.util.Map)3 Objects (java.util.Objects)3