Search in sources :

Example 1 with GenericArray

use of org.apache.avro.generic.GenericArray in project jvm-serializers by eishay.

the class AvroTransformer method forwardMedia.

private Media forwardMedia(data.media.Media media) {
    Media m = new Media();
    m.setUri(media.uri);
    m.setTitle(media.title);
    m.setWidth(media.width);
    m.setHeight(media.height);
    m.setFormat(media.format);
    m.setDuration(media.duration);
    m.setSize(media.size);
    if (media.hasBitrate) {
        m.setBitrate(media.bitrate);
    }
    GenericArray<CharSequence> persons = new GenericData.Array<CharSequence>(media.persons.size(), Avro.Media.sPersons);
    for (String s : media.persons) {
        persons.add(s);
    }
    m.setPersons(persons);
    m.setPlayer(forwardPlayer(media.player));
    m.setCopyright(media.copyright);
    return m;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Media(serializers.avro.media.Media)

Example 2 with GenericArray

use of org.apache.avro.generic.GenericArray in project databus by linkedin.

the class OracleAvroGenericEventFactory method putArray.

private void putArray(GenericRecord record, String arrayFieldName, Schema schema, Array array) throws EventCreationException {
    // Make sure this is an array type
    if (schema.getType() != Type.ARRAY) {
        throw new EventCreationException("Not an array type. " + schema.getName());
    }
    Schema elementSchema = schema.getElementType();
    GenericArray<GenericRecord> avroArray = new GenericData.Array<GenericRecord>(0, schema);
    try {
        ResultSet arrayResultSet = array.getResultSet();
        try {
            while (arrayResultSet.next()) {
                // Create the avro record and add it to the array
                GenericRecord elemRecord = new GenericData.Record(elementSchema);
                avroArray.add(elemRecord);
                // Get the underlying structure from the database. Oracle returns the structure in the
                // second column of the array's ResultSet
                Struct struct = (Struct) arrayResultSet.getObject(2);
                putOracleRecord(elemRecord, elementSchema, struct);
            }
        } finally {
            arrayResultSet.close();
        }
    } catch (SQLException e) {
        throw new EventCreationException("putArray error: " + e.getMessage(), e);
    }
    record.put(arrayFieldName, avroArray);
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Array(java.sql.Array) SQLException(java.sql.SQLException) EventCreationException(com.linkedin.databus2.producers.EventCreationException) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) Struct(java.sql.Struct)

Example 3 with GenericArray

use of org.apache.avro.generic.GenericArray in project drill by apache.

the class AvroTestUtil method generateNestedArraySchema.

public static AvroTestRecordWriter generateNestedArraySchema(int numRecords, int numArrayItems) throws IOException {
    final File file = File.createTempFile("avro-nested-test", ".avro");
    file.deleteOnExit();
    final Schema schema = SchemaBuilder.record("AvroRecordReaderTest").namespace("org.apache.drill.exec.store.avro").fields().name("a_int").type().intType().noDefault().name("b_array").type().array().items().record("my_record_1").namespace("foo.blah.org").fields().name("nested_1_int").type().optional().intType().endRecord().arrayDefault(Collections.emptyList()).endRecord();
    final Schema arraySchema = schema.getField("b_array").schema();
    final Schema itemSchema = arraySchema.getElementType();
    final AvroTestRecordWriter record = new AvroTestRecordWriter(schema, file);
    try {
        for (int i = 0; i < numRecords; i++) {
            record.startRecord();
            record.put("a_int", i);
            GenericArray<GenericRecord> array = new GenericData.Array<>(ARRAY_SIZE, arraySchema);
            for (int j = 0; j < numArrayItems; j++) {
                final GenericRecord nestedRecord = new GenericData.Record(itemSchema);
                nestedRecord.put("nested_1_int", j);
                array.add(nestedRecord);
            }
            record.put("b_array", array);
            record.endRecord();
        }
    } finally {
        record.close();
    }
    return record;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File)

Example 4 with GenericArray

use of org.apache.avro.generic.GenericArray in project drill by apache.

the class AvroTestUtil method generateUnionNestedArraySchema_withNullValues.

public static AvroTestRecordWriter generateUnionNestedArraySchema_withNullValues() throws Exception {
    final File file = File.createTempFile("avro-nested-test", ".avro");
    file.deleteOnExit();
    final Schema schema = SchemaBuilder.record("AvroRecordReaderTest").namespace("org.apache.drill.exec.store.avro").fields().name("a_string").type().stringType().noDefault().name("b_int").type().intType().noDefault().name("c_array").type().optional().array().items().record("my_record_1").namespace("foo.blah.org").fields().name("nested_1_string").type().optional().stringType().name("nested_1_int").type().optional().intType().endRecord().endRecord();
    final Schema nestedSchema = schema.getField("c_array").schema();
    final Schema arraySchema = nestedSchema.getTypes().get(1);
    final Schema itemSchema = arraySchema.getElementType();
    final AvroTestRecordWriter record = new AvroTestRecordWriter(schema, file);
    try {
        for (int i = 0; i < RECORD_COUNT; i++) {
            record.startRecord();
            record.put("a_string", "a_" + i);
            record.put("b_int", i);
            if (i % 2 == 0) {
                GenericArray<GenericRecord> array = new GenericData.Array<>(1, arraySchema);
                final GenericRecord nestedRecord = new GenericData.Record(itemSchema);
                nestedRecord.put("nested_1_string", "nested_1_string_" + i);
                nestedRecord.put("nested_1_int", i * i);
                array.add(nestedRecord);
                record.put("c_array", array);
            }
            record.endRecord();
        }
    } finally {
        record.close();
    }
    return record;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File)

Example 5 with GenericArray

use of org.apache.avro.generic.GenericArray in project drill by apache.

the class AvroTestUtil method generateMapSchemaComplex_withNullValues.

public static AvroTestRecordWriter generateMapSchemaComplex_withNullValues() throws Exception {
    final File file = File.createTempFile("avro-nested-test", ".avro");
    file.deleteOnExit();
    final Schema schema = SchemaBuilder.record("AvroRecordReaderTest").namespace("org.apache.drill.exec.store.avro").fields().name("a_string").type().stringType().noDefault().name("b_int").type().intType().noDefault().name("c_map").type().optional().map().values(Schema.create(Type.STRING)).name("d_map").type().optional().map().values(Schema.createArray(Schema.create(Type.DOUBLE))).endRecord();
    final Schema arrayMapSchema = schema.getField("d_map").schema();
    final Schema arrayItemSchema = arrayMapSchema.getTypes().get(1).getValueType();
    final AvroTestRecordWriter record = new AvroTestRecordWriter(schema, file);
    try {
        for (int i = 0; i < RECORD_COUNT; i++) {
            record.startRecord();
            record.put("a_string", "a_" + i);
            record.put("b_int", i);
            if (i % 2 == 0) {
                Map<String, String> c_map = new HashMap<>();
                c_map.put("key1", "nested_1_string_" + i);
                c_map.put("key2", "nested_1_string_" + (i + 1));
                record.put("c_map", c_map);
            } else {
                Map<String, GenericArray<Double>> d_map = new HashMap<>();
                GenericArray<Double> array = new GenericData.Array<>(ARRAY_SIZE, arrayItemSchema);
                for (int j = 0; j < ARRAY_SIZE; j++) {
                    array.add((double) j);
                }
                d_map.put("key1", array);
                d_map.put("key2", array);
                record.put("d_map", d_map);
            }
            record.endRecord();
        }
    } finally {
        record.close();
    }
    return record;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) JsonStringHashMap(org.apache.drill.exec.util.JsonStringHashMap) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) GenericArray(org.apache.avro.generic.GenericArray) File(java.io.File)

Aggregations

GenericArray (org.apache.avro.generic.GenericArray)18 Schema (org.apache.avro.Schema)15 GenericRecord (org.apache.avro.generic.GenericRecord)11 HashMap (java.util.HashMap)5 File (java.io.File)4 Map (java.util.Map)3 Type (org.apache.avro.Schema.Type)3 EventCreationException (com.linkedin.databus2.producers.EventCreationException)2 Array (java.sql.Array)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Struct (java.sql.Struct)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Field (org.apache.avro.Schema.Field)2 GenericData (org.apache.avro.generic.GenericData)2 Utf8 (org.apache.avro.util.Utf8)2 IThrowableProxy (ch.qos.logback.classic.spi.IThrowableProxy)1 StackTraceElementProxy (ch.qos.logback.classic.spi.StackTraceElementProxy)1 IOException (java.io.IOException)1