Search in sources :

Example 1 with SamzaSqlRelMessageSerde

use of org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde in project samza by apache.

the class TestSamzaSqlRelMessageSerde method testNestedRecordConversion.

@Test
public void testNestedRecordConversion() {
    Map<String, String> props = new HashMap<>();
    SystemStream ss1 = new SystemStream("test", "nestedRecord");
    props.put(String.format(ConfigBasedAvroRelSchemaProviderFactory.CFG_SOURCE_SCHEMA, ss1.getSystem(), ss1.getStream()), Profile.SCHEMA$.toString());
    ConfigBasedAvroRelSchemaProviderFactory factory = new ConfigBasedAvroRelSchemaProviderFactory();
    AvroRelSchemaProvider nestedRecordSchemaProvider = (AvroRelSchemaProvider) factory.create(ss1, new MapConfig(props));
    AvroRelConverter nestedRecordAvroRelConverter = new AvroRelConverter(ss1, nestedRecordSchemaProvider, new MapConfig());
    Pair<SamzaSqlRelMessage, GenericData.Record> messageRecordPair = createNestedSamzaSqlRelMessage(nestedRecordAvroRelConverter);
    SamzaSqlRelMessageSerde serde = (SamzaSqlRelMessageSerde) new SamzaSqlRelMessageSerdeFactory().getSerde(null, null);
    SamzaSqlRelMessage resultMsg = serde.fromBytes(serde.toBytes(messageRecordPair.getKey()));
    KV<Object, Object> samzaMessage = nestedRecordAvroRelConverter.convertToSamzaMessage(resultMsg);
    GenericRecord recordPostConversion = (GenericRecord) samzaMessage.getValue();
    for (Schema.Field field : Profile.SCHEMA$.getFields()) {
        // equals() on GenericRecord does the nested record equality check as well.
        Assert.assertEquals(messageRecordPair.getValue().get(field.name()), recordPostConversion.get(field.name()));
    }
}
Also used : HashMap(java.util.HashMap) SystemStream(org.apache.samza.system.SystemStream) Schema(org.apache.avro.Schema) SamzaSqlRelMessageSerde(org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde) AvroRelConverter(org.apache.samza.sql.avro.AvroRelConverter) AvroRelSchemaProvider(org.apache.samza.sql.avro.AvroRelSchemaProvider) ConfigBasedAvroRelSchemaProviderFactory(org.apache.samza.sql.avro.ConfigBasedAvroRelSchemaProviderFactory) GenericRecord(org.apache.avro.generic.GenericRecord) AddressRecord(org.apache.samza.sql.avro.schemas.AddressRecord) StreetNumRecord(org.apache.samza.sql.avro.schemas.StreetNumRecord) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) Test(org.junit.Test)

Example 2 with SamzaSqlRelMessageSerde

use of org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde in project samza by apache.

the class TestSamzaSqlRelMessageSerde method testWithDifferentFields.

@Test
public void testWithDifferentFields() {
    SamzaSqlRelMessage message = new SamzaSqlRelMessage(names, values, new SamzaSqlRelMsgMetadata(0L, 0L));
    SamzaSqlRelMessageSerde serde = (SamzaSqlRelMessageSerde) new SamzaSqlRelMessageSerdeFactory().getSerde(null, null);
    SamzaSqlRelMessage resultMsg = serde.fromBytes(serde.toBytes(message));
    Assert.assertEquals(names, resultMsg.getSamzaSqlRelRecord().getFieldNames());
    Assert.assertEquals(values, resultMsg.getSamzaSqlRelRecord().getFieldValues());
}
Also used : SamzaSqlRelMsgMetadata(org.apache.samza.sql.data.SamzaSqlRelMsgMetadata) SamzaSqlRelMessageSerde(org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) Test(org.junit.Test)

Aggregations

SamzaSqlRelMessage (org.apache.samza.sql.data.SamzaSqlRelMessage)2 SamzaSqlRelMessageSerde (org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Schema (org.apache.avro.Schema)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 MapConfig (org.apache.samza.config.MapConfig)1 AvroRelConverter (org.apache.samza.sql.avro.AvroRelConverter)1 AvroRelSchemaProvider (org.apache.samza.sql.avro.AvroRelSchemaProvider)1 ConfigBasedAvroRelSchemaProviderFactory (org.apache.samza.sql.avro.ConfigBasedAvroRelSchemaProviderFactory)1 AddressRecord (org.apache.samza.sql.avro.schemas.AddressRecord)1 StreetNumRecord (org.apache.samza.sql.avro.schemas.StreetNumRecord)1 SamzaSqlRelMsgMetadata (org.apache.samza.sql.data.SamzaSqlRelMsgMetadata)1 SystemStream (org.apache.samza.system.SystemStream)1