Search in sources :

Example 41 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project JavaBigData by TALKDATA.

the class TestAvroEventSerializer method createAvroFile.

public void createAvroFile(File file, String codec, boolean useSchemaUrl, boolean useStaticSchemaUrl) throws IOException {
    // serialize a few events using the reflection-based avro serializer
    OutputStream out = new FileOutputStream(file);
    Context ctx = new Context();
    if (codec != null) {
        ctx.put("compressionCodec", codec);
    }
    Schema schema = Schema.createRecord("myrecord", null, null, false);
    schema.setFields(Arrays.asList(new Schema.Field[] { new Schema.Field("message", Schema.create(Schema.Type.STRING), null, null) }));
    GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema);
    File schemaFile = null;
    if (useSchemaUrl || useStaticSchemaUrl) {
        schemaFile = File.createTempFile(getClass().getSimpleName(), ".avsc");
        Files.write(schema.toString(), schemaFile, Charsets.UTF_8);
    }
    if (useStaticSchemaUrl) {
        ctx.put(AvroEventSerializerConfigurationConstants.STATIC_SCHEMA_URL, schemaFile.toURI().toURL().toExternalForm());
    }
    EventSerializer.Builder builder = new AvroEventSerializer.Builder();
    EventSerializer serializer = builder.build(ctx, out);
    serializer.afterCreate();
    for (int i = 0; i < 3; i++) {
        GenericRecord record = recordBuilder.set("message", "Hello " + i).build();
        Event event = EventBuilder.withBody(serializeAvro(record, schema));
        if (schemaFile == null && !useSchemaUrl) {
            event.getHeaders().put(AvroEventSerializer.AVRO_SCHEMA_LITERAL_HEADER, schema.toString());
        } else if (useSchemaUrl) {
            event.getHeaders().put(AvroEventSerializer.AVRO_SCHEMA_URL_HEADER, schemaFile.toURI().toURL().toExternalForm());
        }
        serializer.write(event);
    }
    serializer.flush();
    serializer.beforeClose();
    out.flush();
    out.close();
    if (schemaFile != null) {
        schemaFile.delete();
    }
}
Also used : Context(org.apache.flume.Context) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Schema(org.apache.avro.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) EventBuilder(org.apache.flume.event.EventBuilder) FileOutputStream(java.io.FileOutputStream) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Event(org.apache.flume.Event) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File) EventSerializer(org.apache.flume.serialization.EventSerializer)

Example 42 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project avro by a0x8o.

the class TestSchemaBuilder method testDefaults.

@Test
public void testDefaults() throws IOException {
    Schema writeSchema = SchemaBuilder.record("r").fields().name("requiredInt").type().intType().noDefault().name("optionalInt").type().optional().intType().name("nullableIntWithDefault").type().nullable().intType().intDefault(3).endRecord();
    GenericData.Record rec1 = new GenericRecordBuilder(writeSchema).set("requiredInt", 1).build();
    Assert.assertEquals(1, rec1.get("requiredInt"));
    Assert.assertEquals(null, rec1.get("optionalInt"));
    Assert.assertEquals(3, rec1.get("nullableIntWithDefault"));
    GenericData.Record rec2 = new GenericRecordBuilder(writeSchema).set("requiredInt", 1).set("optionalInt", 2).set("nullableIntWithDefault", 13).build();
    Assert.assertEquals(1, rec2.get("requiredInt"));
    Assert.assertEquals(2, rec2.get("optionalInt"));
    Assert.assertEquals(13, rec2.get("nullableIntWithDefault"));
    // write to file
    File file = new File(DIR.getRoot().getPath(), "testDefaults.avro");
    try (DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>())) {
        writer.create(writeSchema, file);
        writer.append(rec1);
        writer.append(rec2);
    }
    Schema readSchema = SchemaBuilder.record("r").fields().name("requiredInt").type().intType().noDefault().name("optionalInt").type().optional().intType().name("nullableIntWithDefault").type().nullable().intType().intDefault(3).name("newOptionalInt").type().optional().intType().name("newNullableIntWithDefault").type().nullable().intType().intDefault(5).endRecord();
    try (DataFileReader<GenericData.Record> reader = new DataFileReader<>(file, new GenericDatumReader<>(writeSchema, readSchema))) {
        GenericData.Record rec1read = reader.iterator().next();
        Assert.assertEquals(1, rec1read.get("requiredInt"));
        Assert.assertNull(rec1read.get("optionalInt"));
        Assert.assertEquals(3, rec1read.get("nullableIntWithDefault"));
        Assert.assertNull(rec1read.get("newOptionalInt"));
        Assert.assertEquals(5, rec1read.get("newNullableIntWithDefault"));
        GenericData.Record rec2read = reader.iterator().next();
        Assert.assertEquals(1, rec2read.get("requiredInt"));
        Assert.assertEquals(2, rec2read.get("optionalInt"));
        Assert.assertEquals(13, rec2read.get("nullableIntWithDefault"));
        Assert.assertNull(rec2read.get("newOptionalInt"));
        Assert.assertEquals(5, rec2read.get("newNullableIntWithDefault"));
    }
}
Also used : DataFileWriter(org.apache.avro.file.DataFileWriter) GenericData(org.apache.avro.generic.GenericData) DataFileReader(org.apache.avro.file.DataFileReader) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) File(java.io.File) Test(org.junit.Test)

Example 43 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project avro by a0x8o.

the class TestResolvingGrammarGenerator method testDifferingEnumNamespaces.

@Test
public void testDifferingEnumNamespaces() throws Exception {
    Schema schema1 = SchemaBuilder.record("MyRecord").fields().name("field").type(ENUM1_AB_SCHEMA_NAMESPACE_1).noDefault().endRecord();
    Schema schema2 = SchemaBuilder.record("MyRecord").fields().name("field").type(ENUM1_AB_SCHEMA_NAMESPACE_2).noDefault().endRecord();
    GenericData.EnumSymbol genericEnumSymbol = new GenericData.EnumSymbol(ENUM1_AB_SCHEMA_NAMESPACE_1, "A");
    GenericData.Record record = new GenericRecordBuilder(schema1).set("field", genericEnumSymbol).build();
    byte[] data = writeRecord(schema1, record);
    Assert.assertEquals(genericEnumSymbol, readRecord(schema1, data).get("field"));
    Assert.assertEquals(genericEnumSymbol, readRecord(schema2, data).get("field"));
}
Also used : Schema(org.apache.avro.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericData(org.apache.avro.generic.GenericData) Test(org.junit.Test)

Example 44 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project flume by apache.

the class TestLog4jAppenderWithAvro method testDifferentEventTypesInBatchWithAvroGeneric.

@Test
public void testDifferentEventTypesInBatchWithAvroGeneric() throws IOException {
    loadProperties("flume-log4jtest-avro-generic.properties");
    props.put("log4j.appender.out2.Port", String.valueOf(port));
    PropertyConfigurator.configure(props);
    Logger logger = LogManager.getLogger(getClass());
    String msg = "Avro log message";
    Schema schema = new Schema.Parser().parse(getClass().getClassLoader().getResource("myrecord.avsc").openStream());
    GenericRecordBuilder builder = new GenericRecordBuilder(schema);
    GenericRecord record = builder.set("message", msg).build();
    List<Object> events = Arrays.asList("string", record);
    logger.info(events);
    Transaction transaction = ch.getTransaction();
    transaction.begin();
    Event event = ch.take();
    Assert.assertNotNull(event);
    Assert.assertEquals("string", new String(event.getBody()));
    event = ch.take();
    Assert.assertNotNull(event);
    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(event.getBody(), null);
    GenericRecord recordFromEvent = reader.read(null, decoder);
    Assert.assertEquals(msg, recordFromEvent.get("message").toString());
    Map<String, String> hdrs = event.getHeaders();
    Assert.assertNull(hdrs.get(Log4jAvroHeaders.MESSAGE_ENCODING.toString()));
    Assert.assertEquals("Schema URL should be set", "file:///tmp/myrecord.avsc", hdrs.get(Log4jAvroHeaders.AVRO_SCHEMA_URL.toString()));
    Assert.assertNull("Schema string should not be set", hdrs.get(Log4jAvroHeaders.AVRO_SCHEMA_LITERAL.toString()));
    transaction.commit();
    transaction.close();
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) Logger(org.apache.log4j.Logger) BinaryDecoder(org.apache.avro.io.BinaryDecoder) Transaction(org.apache.flume.Transaction) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Event(org.apache.flume.Event) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 45 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project flume by apache.

the class TestLog4jAppenderWithAvro method testAvroGeneric.

@Test
public void testAvroGeneric() throws IOException {
    loadProperties("flume-log4jtest-avro-generic.properties");
    props.put("log4j.appender.out2.Port", String.valueOf(port));
    PropertyConfigurator.configure(props);
    Logger logger = LogManager.getLogger(TestLog4jAppenderWithAvro.class);
    String msg = "This is log message number " + String.valueOf(0);
    Schema schema = new Schema.Parser().parse(getClass().getClassLoader().getResource("myrecord.avsc").openStream());
    GenericRecordBuilder builder = new GenericRecordBuilder(schema);
    GenericRecord record = builder.set("message", msg).build();
    logger.info(record);
    Transaction transaction = ch.getTransaction();
    transaction.begin();
    Event event = ch.take();
    Assert.assertNotNull(event);
    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(event.getBody(), null);
    GenericRecord recordFromEvent = reader.read(null, decoder);
    Assert.assertEquals(msg, recordFromEvent.get("message").toString());
    Map<String, String> hdrs = event.getHeaders();
    Assert.assertNull(hdrs.get(Log4jAvroHeaders.MESSAGE_ENCODING.toString()));
    Assert.assertEquals("Schema URL should be set", "file:///tmp/myrecord.avsc", hdrs.get(Log4jAvroHeaders.AVRO_SCHEMA_URL.toString()));
    Assert.assertNull("Schema string should not be set", hdrs.get(Log4jAvroHeaders.AVRO_SCHEMA_LITERAL.toString()));
    transaction.commit();
    transaction.close();
}
Also used : Transaction(org.apache.flume.Transaction) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Event(org.apache.flume.Event) Logger(org.apache.log4j.Logger) GenericRecord(org.apache.avro.generic.GenericRecord) BinaryDecoder(org.apache.avro.io.BinaryDecoder) Test(org.junit.Test)

Aggregations

GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)399 GenericRecord (org.apache.avro.generic.GenericRecord)263 Test (org.junit.Test)263 Schema (org.apache.avro.Schema)216 GenericData (org.apache.avro.generic.GenericData)69 ArrayList (java.util.ArrayList)45 EnumTest (foo.bar.EnumTest)41 File (java.io.File)41 IndexedRecord (org.apache.avro.generic.IndexedRecord)39 Schema (org.apache.kafka.connect.data.Schema)39 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)35 Path (org.apache.hadoop.fs.Path)33 List (java.util.List)30 ByteBuffer (java.nio.ByteBuffer)29 HashMap (java.util.HashMap)29 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)28 Struct (org.apache.kafka.connect.data.Struct)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 Record (org.apache.avro.generic.GenericData.Record)25 SchemaBuilder (org.apache.avro.SchemaBuilder)22