Search in sources :

Example 1 with AddressRecord

use of org.apache.samza.sql.avro.schemas.AddressRecord in project samza by apache.

the class TestAvroRelConversion method testNestedRecordConversionWithSubRecordsBeingNull.

@Test
public void testNestedRecordConversionWithSubRecordsBeingNull() 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 = null;
    record.put("address", addressRecord);
    record.put("selfEmployed", "True");
    List<GenericData.Record> phoneNumbers = null;
    record.put("phoneNumbers", phoneNumbers);
    HashMap<String, IndexedRecord> mapValues = null;
    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) Schema(org.apache.avro.Schema) SqlSchema(org.apache.samza.sql.schema.SqlSchema) 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 2 with AddressRecord

use of org.apache.samza.sql.avro.schemas.AddressRecord 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)

Aggregations

Schema (org.apache.avro.Schema)2 GenericData (org.apache.avro.generic.GenericData)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 ByteString (org.apache.calcite.avatica.util.ByteString)2 AddressRecord (org.apache.samza.sql.avro.schemas.AddressRecord)2 ComplexRecord (org.apache.samza.sql.avro.schemas.ComplexRecord)2 SimpleRecord (org.apache.samza.sql.avro.schemas.SimpleRecord)2 StreetNumRecord (org.apache.samza.sql.avro.schemas.StreetNumRecord)2 SamzaSqlRelMessage (org.apache.samza.sql.data.SamzaSqlRelMessage)2 SqlSchema (org.apache.samza.sql.schema.SqlSchema)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1