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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations