Search in sources :

Example 1 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class AvroRelConverter method convertToGenericRecord.

private static GenericRecord convertToGenericRecord(SamzaSqlRelRecord relRecord, Schema schema) {
    GenericRecord record = new GenericData.Record(schema);
    List<String> fieldNames = relRecord.getFieldNames();
    List<Object> values = relRecord.getFieldValues();
    for (int index = 0; index < fieldNames.size(); index++) {
        if (!fieldNames.get(index).equalsIgnoreCase(SamzaSqlRelMessage.KEY_NAME)) {
            String fieldName = fieldNames.get(index);
            /**
             * It is possible that the destination Avro schema doesn't have all the fields that are projected from the
             * SQL. This is especially possible in SQL statements like
             *        insert into kafka.outputTopic select id, company from profile
             * where company is an avro record in itself whose schema can evolve. When this happens we will end up with
             * fields in the SamzaSQLRelRecord for company field which doesn't have equivalent fields in the outputTopic's schema
             * for company. To support this scenario where the input schemas and output schemas can evolve in their own cadence,
             * We ignore the fields which doesn't have corresponding schema in the output topic.
             */
            if (schema.getField(fieldName) == null) {
                LOG.debug("Schema with Name {} and Namespace {} doesn't contain the fieldName {}, Skipping it.", schema.getName(), schema.getNamespace(), fieldName);
                continue;
            }
            Object relObj = values.get(index);
            Schema fieldSchema = schema.getField(fieldName).schema();
            record.put(fieldName, convertToAvroObject(relObj, getNonNullUnionSchema(fieldSchema)));
        }
    }
    return record;
}
Also used : Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) ByteString(org.apache.calcite.avatica.util.ByteString) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class AvroRelConverter method convertToRelRecord.

private static SamzaSqlRelRecord convertToRelRecord(IndexedRecord avroRecord) {
    List<Object> fieldValues = new ArrayList<>();
    List<String> fieldNames = new ArrayList<>();
    if (avroRecord != null) {
        fieldNames.addAll(avroRecord.getSchema().getFields().stream().map(Schema.Field::name).collect(Collectors.toList()));
        fieldValues.addAll(avroRecord.getSchema().getFields().stream().map(f -> convertToJavaObject(avroRecord.get(avroRecord.getSchema().getField(f.name()).pos()), getNonNullUnionSchema(avroRecord.getSchema().getField(f.name()).schema()))).collect(Collectors.toList()));
    } else {
        String msg = "Avro Record is null";
        LOG.error(msg);
        throw new SamzaException(msg);
    }
    return new SamzaSqlRelRecord(fieldNames, fieldValues);
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) ArrayList(java.util.ArrayList) ByteString(org.apache.calcite.avatica.util.ByteString) SamzaException(org.apache.samza.SamzaException)

Example 3 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class GetSqlFieldUdf method extractField.

static Object extractField(String fieldName, Object current, boolean validateField) {
    if (current instanceof SamzaSqlRelRecord) {
        SamzaSqlRelRecord record = (SamzaSqlRelRecord) current;
        if (validateField) {
            Validate.isTrue(record.getFieldNames().contains(fieldName), String.format("Invalid field %s in record %s", fieldName, record));
        }
        return record.getField(fieldName).orElse(null);
    } else if (current instanceof Map) {
        Map map = (Map) current;
        if (map.containsKey(fieldName)) {
            return map.get(fieldName);
        } else if (map.containsKey(new Utf8(fieldName))) {
            return map.get(new Utf8(fieldName));
        } else {
            throw new IllegalArgumentException(String.format("Couldn't find the field %s in map %s", fieldName, map));
        }
    } else if (current instanceof List && fieldName.endsWith("]")) {
        List list = (List) current;
        int index = Integer.parseInt(fieldName.substring(fieldName.indexOf("[") + 1, fieldName.length() - 1));
        return list.get(index);
    }
    throw new IllegalArgumentException(String.format("Unsupported accessing operation for data type: %s with field: %s.", current.getClass(), fieldName));
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) Utf8(org.apache.avro.util.Utf8) List(java.util.List) Map(java.util.Map)

Example 4 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class TestSamzaSqlRelMessage method testCompositeKeyCreationWithInEqualKeyNameValues.

@Test(expected = IllegalArgumentException.class)
public void testCompositeKeyCreationWithInEqualKeyNameValues() {
    List<String> keyPartNames = Arrays.asList("kfield1", "kfield2");
    SamzaSqlRelMessage message = new SamzaSqlRelMessage(names, values, new SamzaSqlRelMsgMetadata(0L, 0L));
    SamzaSqlRelRecord relRecord1 = SamzaSqlRelMessage.createSamzaSqlCompositeKey(message, Arrays.asList(1, 0), SamzaSqlRelMessage.getSamzaSqlCompositeKeyFieldNames(keyPartNames, Arrays.asList(1)));
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) Test(org.junit.Test)

Example 5 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class TestSamzaSqlRelRecordSerde 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 = TestSamzaSqlRelMessageSerde.createNestedSamzaSqlRelMessage(nestedRecordAvroRelConverter);
    SamzaSqlRelRecordSerdeFactory.SamzaSqlRelRecordSerde serde = (SamzaSqlRelRecordSerdeFactory.SamzaSqlRelRecordSerde) new SamzaSqlRelRecordSerdeFactory().getSerde(null, null);
    SamzaSqlRelRecord resultRecord = serde.fromBytes(serde.toBytes(messageRecordPair.getKey().getSamzaSqlRelRecord()));
    GenericData.Record recordPostConversion = (GenericData.Record) nestedRecordAvroRelConverter.convertToAvroObject(resultRecord, Profile.SCHEMA$);
    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) GenericData(org.apache.avro.generic.GenericData) AvroRelConverter(org.apache.samza.sql.avro.AvroRelConverter) SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) AvroRelSchemaProvider(org.apache.samza.sql.avro.AvroRelSchemaProvider) SamzaSqlRelRecordSerde(org.apache.samza.sql.serializers.SamzaSqlRelRecordSerdeFactory.SamzaSqlRelRecordSerde) ConfigBasedAvroRelSchemaProviderFactory(org.apache.samza.sql.avro.ConfigBasedAvroRelSchemaProviderFactory) SamzaSqlRelRecordSerde(org.apache.samza.sql.serializers.SamzaSqlRelRecordSerdeFactory.SamzaSqlRelRecordSerde) SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) Test(org.junit.Test)

Aggregations

SamzaSqlRelRecord (org.apache.samza.sql.SamzaSqlRelRecord)31 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)6 SamzaSqlRelMessage (org.apache.samza.sql.data.SamzaSqlRelMessage)5 List (java.util.List)3 Schema (org.apache.avro.Schema)3 JoinRelType (org.apache.calcite.rel.core.JoinRelType)3 SamzaSqlRelMsgMetadata (org.apache.samza.sql.data.SamzaSqlRelMsgMetadata)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GenericData (org.apache.avro.generic.GenericData)2 ByteString (org.apache.calcite.avatica.util.ByteString)2 SamzaException (org.apache.samza.SamzaException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 Collectors (java.util.stream.Collectors)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 IndexedRecord (org.apache.avro.generic.IndexedRecord)1