Search in sources :

Example 6 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 7 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)

Example 8 with GenericArray

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;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) IThrowableProxy(ch.qos.logback.classic.spi.IThrowableProxy) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 9 with GenericArray

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;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 10 with GenericArray

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

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