Search in sources :

Example 26 with SamzaSqlRelMessage

use of org.apache.samza.sql.data.SamzaSqlRelMessage in project samza by apache.

the class TestAvroRelConversion method testNestedRecordConversion.

@Test
public void testNestedRecordConversion() throws IOException {
    GenericData.Record record = new GenericData.Record(Profile.SCHEMA$);
    record.put("id", 1);
    record.put("name", "name1");
    record.put("companyId", 0);
    GenericData.Record addressRecord = new GenericData.Record(AddressRecord.SCHEMA$);
    addressRecord.put("zip", 90000);
    GenericData.Record streetNumRecord = new GenericData.Record(StreetNumRecord.SCHEMA$);
    streetNumRecord.put("number", 1200);
    addressRecord.put("streetnum", streetNumRecord);
    record.put("address", addressRecord);
    record.put("selfEmployed", "True");
    GenericData.Record phoneNumberRecordH = new GenericData.Record(PhoneNumber.SCHEMA$);
    phoneNumberRecordH.put("kind", Kind.Home);
    phoneNumberRecordH.put("number", "111-111-1111");
    GenericData.Record phoneNumberRecordC = new GenericData.Record(PhoneNumber.SCHEMA$);
    phoneNumberRecordC.put("kind", Kind.Cell);
    phoneNumberRecordC.put("number", "111-111-1112");
    List<GenericData.Record> phoneNumbers = new ArrayList<>();
    phoneNumbers.add(phoneNumberRecordH);
    phoneNumbers.add(phoneNumberRecordC);
    record.put("phoneNumbers", phoneNumbers);
    GenericData.Record simpleRecord1 = new GenericData.Record(SimpleRecord.SCHEMA$);
    simpleRecord1.put("id", 1);
    simpleRecord1.put("name", "name1");
    GenericData.Record simpleRecord2 = new GenericData.Record(SimpleRecord.SCHEMA$);
    simpleRecord2.put("id", 2);
    simpleRecord2.put("name", "name2");
    HashMap<String, IndexedRecord> mapValues = new HashMap<>();
    mapValues.put("key1", simpleRecord1);
    mapValues.put("key2", simpleRecord2);
    record.put("mapValues", mapValues);
    SamzaSqlRelMessage relMessage = nestedRecordAvroRelConverter.convertToRelMessage(new KV<>("key", record));
    LOG.info(relMessage.toString());
    KV<Object, Object> samzaMessage = nestedRecordAvroRelConverter.convertToSamzaMessage(relMessage);
    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(record.get(field.name()), recordPostConversion.get(field.name()));
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) SqlSchema(org.apache.samza.sql.schema.SqlSchema) ArrayList(java.util.ArrayList) ByteString(org.apache.calcite.avatica.util.ByteString) GenericData(org.apache.avro.generic.GenericData) ComplexRecord(org.apache.samza.sql.avro.schemas.ComplexRecord) StreetNumRecord(org.apache.samza.sql.avro.schemas.StreetNumRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) GenericRecord(org.apache.avro.generic.GenericRecord) SimpleRecord(org.apache.samza.sql.avro.schemas.SimpleRecord) AddressRecord(org.apache.samza.sql.avro.schemas.AddressRecord) GenericRecord(org.apache.avro.generic.GenericRecord) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) Test(org.junit.Test)

Example 27 with SamzaSqlRelMessage

use of org.apache.samza.sql.data.SamzaSqlRelMessage in project samza by apache.

the class TestAvroRelConversion method validateAvroSerializedData.

private void validateAvroSerializedData(byte[] serializedData, Object unionValue) throws IOException {
    GenericRecord complexRecordValue = genericRecordFromBytes(serializedData, ComplexRecord.SCHEMA$);
    SamzaSqlRelMessage message = complexRecordAvroRelConverter.convertToRelMessage(new KV<>("key", complexRecordValue));
    Assert.assertEquals(message.getSamzaSqlRelRecord().getFieldNames().size(), ComplexRecord.SCHEMA$.getFields().size() + 1);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("id").get(), id);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("bool_value").get(), boolValue);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("double_value").get(), doubleValue);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("string_value").get(), new Utf8(testStrValue));
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("float_value0").get(), floatValue);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("long_value").get(), longValue);
    if (unionValue instanceof String) {
        Assert.assertEquals(message.getSamzaSqlRelRecord().getField("union_value").get(), new Utf8((String) unionValue));
    } else {
        Assert.assertEquals(message.getSamzaSqlRelRecord().getField("union_value").get(), unionValue);
    }
    Assert.assertTrue(arrayValue.stream().map(Utf8::new).collect(Collectors.toList()).equals(message.getSamzaSqlRelRecord().getField("array_values").get()));
    Assert.assertTrue(mapValue.entrySet().stream().collect(Collectors.toMap(x -> new Utf8(x.getKey()), y -> new Utf8(y.getValue()))).equals(message.getSamzaSqlRelRecord().getField("map_values").get()));
    Assert.assertTrue(Arrays.equals(((ByteString) message.getSamzaSqlRelRecord().getField("bytes_value").get()).getBytes(), testBytes.array()));
    Assert.assertTrue(Arrays.equals(((ByteString) message.getSamzaSqlRelRecord().getField("fixed_value").get()).getBytes(), DEFAULT_TRACKING_ID_BYTES));
    LOG.info(message.toString());
    KV<Object, Object> samzaMessage = complexRecordAvroRelConverter.convertToSamzaMessage(message);
    GenericRecord record = (GenericRecord) samzaMessage.getValue();
    for (Schema.Field field : ComplexRecord.SCHEMA$.getFields()) {
        if (field.name().equals("array_values")) {
            Assert.assertTrue(record.get(field.name()).equals(complexRecordValue.get(field.name())));
        } else {
            Object expected = complexRecordValue.get(field.name());
            Assert.assertEquals(expected, record.get(field.name()));
        }
    }
}
Also used : MyFixed(org.apache.samza.sql.avro.schemas.MyFixed) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Encoder(org.apache.avro.io.Encoder) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) RelSchemaConverter(org.apache.samza.sql.planner.RelSchemaConverter) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) ComplexRecord(org.apache.samza.sql.avro.schemas.ComplexRecord) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) ComplexUnion(org.apache.samza.sql.avro.schemas.ComplexUnion) StreetNumRecord(org.apache.samza.sql.avro.schemas.StreetNumRecord) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) MapConfig(org.apache.samza.config.MapConfig) KV(org.apache.samza.operators.KV) IndexedRecord(org.apache.avro.generic.IndexedRecord) EncoderFactory(org.apache.avro.io.EncoderFactory) Profile(org.apache.samza.sql.avro.schemas.Profile) Utf8(org.apache.avro.util.Utf8) RelDataType(org.apache.calcite.rel.type.RelDataType) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord) Schema(org.apache.avro.Schema) DatumWriter(org.apache.avro.io.DatumWriter) Logger(org.slf4j.Logger) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ByteString(org.apache.calcite.avatica.util.ByteString) RelRecordType(org.apache.calcite.rel.type.RelRecordType) BinaryDecoder(org.apache.avro.io.BinaryDecoder) SqlSchema(org.apache.samza.sql.schema.SqlSchema) IOException(java.io.IOException) Kind(org.apache.samza.sql.avro.schemas.Kind) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SimpleRecord(org.apache.samza.sql.avro.schemas.SimpleRecord) AddressRecord(org.apache.samza.sql.avro.schemas.AddressRecord) PhoneNumber(org.apache.samza.sql.avro.schemas.PhoneNumber) BinaryEncoder(org.apache.avro.io.BinaryEncoder) List(java.util.List) Ignore(org.junit.Ignore) Assert(org.junit.Assert) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) DecoderFactory(org.apache.avro.io.DecoderFactory) ByteString(org.apache.calcite.avatica.util.ByteString) Schema(org.apache.avro.Schema) SqlSchema(org.apache.samza.sql.schema.SqlSchema) Utf8(org.apache.avro.util.Utf8) ByteString(org.apache.calcite.avatica.util.ByteString) GenericRecord(org.apache.avro.generic.GenericRecord) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage)

Example 28 with SamzaSqlRelMessage

use of org.apache.samza.sql.data.SamzaSqlRelMessage in project samza by apache.

the class AvroRelConverter method convertToRelMessage.

/**
 * Create a SamzaSqlRelMessage for the specified key and Avro record using the schema from the Avro record.
 */
public static SamzaSqlRelMessage convertToRelMessage(Object key, IndexedRecord record, Schema schema) {
    List<String> payloadFieldNames = new ArrayList<>();
    List<Object> payloadFieldValues = new ArrayList<>();
    fetchFieldNamesAndValuesFromIndexedRecord(record, payloadFieldNames, payloadFieldValues, schema);
    return new SamzaSqlRelMessage(key, payloadFieldNames, payloadFieldValues, new SamzaSqlRelMsgMetadata(0L, 0L));
}
Also used : SamzaSqlRelMsgMetadata(org.apache.samza.sql.data.SamzaSqlRelMsgMetadata) ArrayList(java.util.ArrayList) ByteString(org.apache.calcite.avatica.util.ByteString) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage)

Aggregations

SamzaSqlRelMessage (org.apache.samza.sql.data.SamzaSqlRelMessage)28 Test (org.junit.Test)19 SamzaSqlRelMsgMetadata (org.apache.samza.sql.data.SamzaSqlRelMsgMetadata)14 ArrayList (java.util.ArrayList)13 GenericData (org.apache.avro.generic.GenericData)10 GenericRecord (org.apache.avro.generic.GenericRecord)10 HashMap (java.util.HashMap)9 IndexedRecord (org.apache.avro.generic.IndexedRecord)7 AddressRecord (org.apache.samza.sql.avro.schemas.AddressRecord)7 SimpleRecord (org.apache.samza.sql.avro.schemas.SimpleRecord)7 StreetNumRecord (org.apache.samza.sql.avro.schemas.StreetNumRecord)7 Schema (org.apache.avro.Schema)6 KV (org.apache.samza.operators.KV)6 MessageStream (org.apache.samza.operators.MessageStream)6 SamzaSqlRelRecord (org.apache.samza.sql.SamzaSqlRelRecord)6 List (java.util.List)5 ByteString (org.apache.calcite.avatica.util.ByteString)5 RelNode (org.apache.calcite.rel.RelNode)5 JoinRelType (org.apache.calcite.rel.core.JoinRelType)5 Logger (org.slf4j.Logger)5