Search in sources :

Example 46 with GenericRecordBuilder

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

the class TestAvroEventDeserializer method newTestFile.

private File newTestFile(boolean deleteOnExit) throws IOException {
    File tempFile = File.createTempFile("testDirectFile", "tmp");
    if (deleteOnExit) {
        tempFile.deleteOnExit();
    }
    DataFileWriter<GenericRecord> writer = new DataFileWriter<GenericRecord>(new GenericDatumWriter<GenericRecord>(schema));
    writer.create(schema, tempFile);
    GenericRecordBuilder recordBuilder;
    recordBuilder = new GenericRecordBuilder(schema);
    recordBuilder.set("foo", "bar");
    GenericRecord record = recordBuilder.build();
    writer.append(record);
    writer.sync();
    recordBuilder = new GenericRecordBuilder(schema);
    recordBuilder.set("foo", "baz");
    record = recordBuilder.build();
    writer.append(record);
    writer.sync();
    writer.flush();
    writer.close();
    return tempFile;
}
Also used : DataFileWriter(org.apache.avro.file.DataFileWriter) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File)

Example 47 with GenericRecordBuilder

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

the class AvroCompactionTaskTest method createRandomRecord.

private GenericRecord createRandomRecord() {
    GenericRecordBuilder keyRecordBuilder = new GenericRecordBuilder(getSchema());
    keyRecordBuilder.set("partitionKey", new Long(1));
    keyRecordBuilder.set("environment", "test");
    keyRecordBuilder.set("subKey", "2");
    GenericRecord record = keyRecordBuilder.build();
    return record;
}
Also used : GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 48 with GenericRecordBuilder

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

the class CompactionGMCEPublishingActionTest method createEvolvedSchemaRecord.

public GenericRecord createEvolvedSchemaRecord() {
    Schema evolvedSchema = SchemaBuilder.record("evolved").fields().requiredLong("partitionKey").requiredString("environment").requiredString("subKey").optionalString("oppo").endRecord();
    GenericRecordBuilder keyRecordBuilder = new GenericRecordBuilder(evolvedSchema);
    keyRecordBuilder.set("partitionKey", new Long(1));
    keyRecordBuilder.set("environment", "test");
    keyRecordBuilder.set("subKey", "2");
    keyRecordBuilder.set("oppo", "poop");
    return keyRecordBuilder.build();
}
Also used : Schema(org.apache.avro.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder)

Example 49 with GenericRecordBuilder

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

the class CompactionGMCEPublishingActionTest method createRandomRecord.

public GenericRecord createRandomRecord() {
    GenericRecordBuilder keyRecordBuilder = new GenericRecordBuilder(getSchema());
    keyRecordBuilder.set("partitionKey", new Long(1));
    keyRecordBuilder.set("environment", "test");
    keyRecordBuilder.set("subKey", "2");
    GenericRecord record = keyRecordBuilder.build();
    return record;
}
Also used : GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 50 with GenericRecordBuilder

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

the class KafkaDeserializerExtractorTest method testConfluentAvroDeserializer.

@Test
public void testConfluentAvroDeserializer() throws IOException, RestClientException {
    WorkUnitState mockWorkUnitState = getMockWorkUnitState(0L, 10L);
    mockWorkUnitState.setProp("schema.registry.url", TEST_URL);
    Schema schema = SchemaBuilder.record(TEST_RECORD_NAME).namespace(TEST_NAMESPACE).fields().name(TEST_FIELD_NAME).type().stringType().noDefault().endRecord();
    GenericRecord testGenericRecord = new GenericRecordBuilder(schema).set(TEST_FIELD_NAME, "testValue").build();
    SchemaRegistryClient mockSchemaRegistryClient = mock(SchemaRegistryClient.class);
    when(mockSchemaRegistryClient.getByID(any(Integer.class))).thenReturn(schema);
    Serializer<Object> kafkaEncoder = new KafkaAvroSerializer(mockSchemaRegistryClient);
    Deserializer<Object> kafkaDecoder = new KafkaAvroDeserializer(mockSchemaRegistryClient);
    ByteBuffer testGenericRecordByteBuffer = ByteBuffer.wrap(kafkaEncoder.serialize(TEST_TOPIC_NAME, testGenericRecord));
    KafkaSchemaRegistry<Integer, Schema> mockKafkaSchemaRegistry = mock(KafkaSchemaRegistry.class);
    KafkaDeserializerExtractor kafkaDecoderExtractor = new KafkaDeserializerExtractor(mockWorkUnitState, Optional.fromNullable(Deserializers.CONFLUENT_AVRO), kafkaDecoder, mockKafkaSchemaRegistry);
    ByteArrayBasedKafkaRecord mockMessageAndOffset = getMockMessageAndOffset(testGenericRecordByteBuffer);
    Assert.assertEquals(kafkaDecoderExtractor.decodeRecord(mockMessageAndOffset), testGenericRecord);
}
Also used : WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) Schema(org.apache.avro.Schema) KafkaAvroSerializer(io.confluent.kafka.serializers.KafkaAvroSerializer) KafkaAvroDeserializer(io.confluent.kafka.serializers.KafkaAvroDeserializer) ByteBuffer(java.nio.ByteBuffer) ByteArrayBasedKafkaRecord(org.apache.gobblin.kafka.client.ByteArrayBasedKafkaRecord) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Test(org.testng.annotations.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