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;
}
use of org.apache.avro.generic.GenericArray in project cdap by caskdata.
the class ThrowableProxyArraySerializer method encode.
static GenericArray<GenericRecord> encode(Schema schema, IThrowableProxy[] throwableProxies) {
if (throwableProxies != null) {
Schema tpArraySchema = schema.getTypes().get(1);
GenericArray<GenericRecord> steArray = new GenericData.Array<>(throwableProxies.length, tpArraySchema);
for (IThrowableProxy tp : throwableProxies) {
steArray.add(ThrowableProxySerializer.encode(tpArraySchema.getElementType(), tp));
}
return steArray;
}
return null;
}
use of org.apache.avro.generic.GenericArray in project cdap by caskdata.
the class CallerDataSerializer method encode.
static GenericArray<GenericRecord> encode(Schema schema, StackTraceElement[] stackTraceElements) {
if (stackTraceElements != null) {
Schema steArraySchema = schema.getTypes().get(1);
GenericArray<GenericRecord> steArray = new GenericData.Array<>(stackTraceElements.length, steArraySchema);
for (StackTraceElement stackTraceElement : stackTraceElements) {
steArray.add(StackTraceElementSerializer.encode(steArraySchema.getElementType(), stackTraceElement));
}
return steArray;
}
return null;
}
use of org.apache.avro.generic.GenericArray in project cdap by caskdata.
the class LoggingEventSerializer method toGenericRecord.
/**
* Creates a new {@link GenericRecord} that represents the given {@link ILoggingEvent}.
*/
public GenericRecord toGenericRecord(ILoggingEvent event) {
Schema schema = getAvroSchema();
GenericRecord datum = new GenericData.Record(schema);
datum.put("threadName", event.getThreadName());
datum.put("level", event.getLevel() == null ? Level.ERROR_INT : event.getLevel().toInt());
datum.put("message", event.getMessage());
Object[] arguments = event.getArgumentArray();
if (arguments != null) {
GenericArray<String> argArray = new GenericData.Array<>(arguments.length, schema.getField("argumentArray").schema().getTypes().get(1));
for (Object argument : arguments) {
argArray.add(argument == null ? null : argument.toString());
}
datum.put("argumentArray", argArray);
}
datum.put("formattedMessage", event.getFormattedMessage());
datum.put("loggerName", event.getLoggerName());
datum.put("loggerContextVO", LoggerContextSerializer.encode(schema.getField("loggerContextVO").schema(), event.getLoggerContextVO()));
datum.put("throwableProxy", ThrowableProxySerializer.encode(schema.getField("throwableProxy").schema(), event.getThrowableProxy()));
if (event.hasCallerData()) {
datum.put("callerData", CallerDataSerializer.encode(schema.getField("callerData").schema(), event.getCallerData()));
}
datum.put("hasCallerData", event.hasCallerData());
//datum.put("marker", marker);
datum.put("mdc", LoggingUtil.encodeMDC(event.getMDCPropertyMap()));
datum.put("timestamp", event.getTimeStamp());
return datum;
}
Aggregations