Search in sources :

Example 61 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project hydrator-plugins by cdapio.

the class JoinerTestRun method verifyOuterJoinWithoutRequiredInputs.

private Void verifyOuterJoinWithoutRequiredInputs(Schema outputSchema, TimePartitionedFileSet fileSet) {
    try {
        Set<GenericRecord> actual = Sets.newHashSet(readOutput(fileSet, outputSchema));
        Assert.assertEquals(8, actual.size());
        org.apache.avro.Schema avroOutputSchema = new org.apache.avro.Schema.Parser().parse(outputSchema.toString());
        GenericRecord humtumRecord = new GenericRecordBuilder(avroOutputSchema).set("film_id", "4").set("film_name", "humtum").set("renamed_actor", null).set("renamed_category", null).build();
        GenericRecord sultanRecord = new GenericRecordBuilder(avroOutputSchema).set("film_id", null).set("film_name", null).set("renamed_actor", null).set("renamed_category", "comedy").build();
        Set<GenericRecord> expected = ImmutableSet.of(getBobRecord1(avroOutputSchema), getBobRecord2(avroOutputSchema), getAlexRecord1(avroOutputSchema), getAlexRecord2(avroOutputSchema), getCathieRecord1(avroOutputSchema), getAvatarRecord1(avroOutputSchema), humtumRecord, sultanRecord);
        Assert.assertEquals(expected, actual);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return null;
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 62 with GenericRecordBuilder

use of org.apache.avro.generic.GenericRecordBuilder in project hydrator-plugins by cdapio.

the class StructuredToAvroTransformer method transform.

@Override
public GenericRecord transform(StructuredRecord structuredRecord, io.cdap.cdap.api.data.schema.Schema schema) throws IOException {
    io.cdap.cdap.api.data.schema.Schema structuredRecordSchema = structuredRecord.getSchema();
    Schema avroSchema = getAvroSchema(schema);
    GenericRecordBuilder recordBuilder = new GenericRecordBuilder(avroSchema);
    for (Schema.Field field : avroSchema.getFields()) {
        String fieldName = field.name();
        io.cdap.cdap.api.data.schema.Schema.Field schemaField = structuredRecordSchema.getField(fieldName);
        if (schemaField == null) {
            throw new IllegalArgumentException("Input record does not contain the " + fieldName + " field.");
        }
        recordBuilder.set(fieldName, convertField(structuredRecord.get(fieldName), schemaField));
    }
    return recordBuilder.build();
}
Also used : Schema(org.apache.avro.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder)

Example 63 with GenericRecordBuilder

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

the class AvroGenericRecordBoltTest method setupClass.

@BeforeClass
public static void setupClass() {
    Schema.Parser parser = new Schema.Parser();
    schema1 = parser.parse(schemaV1);
    parser = new Schema.Parser();
    schema2 = parser.parse(schemaV2);
    GenericRecordBuilder builder1 = new GenericRecordBuilder(schema1);
    builder1.set("foo1", "bar1");
    builder1.set("int1", 1);
    tuple1 = generateTestTuple(builder1.build());
    GenericRecordBuilder builder2 = new GenericRecordBuilder(schema2);
    builder2.set("foo1", "bar2");
    builder2.set("int1", 2);
    tuple2 = generateTestTuple(builder2.build());
}
Also used : Schema(org.apache.avro.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) BeforeClass(org.junit.BeforeClass)

Example 64 with GenericRecordBuilder

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

the class KafkaMessageGenerator method populateAvroMsgIntoKafka.

public void populateAvroMsgIntoKafka(String topic, int numMsg) {
    producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
    try (KafkaProducer<Object, GenericRecord> producer = new KafkaProducer<>(producerProperties)) {
        Schema.Parser parser = new Schema.Parser();
        String userSchema = "{\"type\":\"record\"," + "\"name\":\"myrecord\"," + "\"fields\":[" + "{\"name\":\"key1\",\"type\":\"string\"}," + "{\"name\":\"key2\",\"type\":\"int\"}," + "{\"name\":\"key3\",\"type\":\"boolean\"}," + "{\"name\":\"key5\",\"type\":{\"type\":\"array\",\"items\":\"int\"}}," + "{\"name\":\"key6\",\"type\":{\"type\":\"record\",\"name\":\"myrecord6\",\"fields\":[" + "{\"name\":\"key61\",\"type\":\"double\"}," + "{\"name\":\"key62\",\"type\":\"double\"}]}}]}";
        Schema valueSchema = parser.parse(userSchema);
        GenericRecordBuilder valueBuilder = new GenericRecordBuilder(valueSchema);
        String key1Schema = "{\"type\":\"record\"," + "\"name\":\"key1record\"," + "\"fields\":[" + "{\"name\":\"key1\",\"type\":\"string\"}]}\"";
        Schema keySchema = parser.parse(key1Schema);
        GenericRecordBuilder keyBuilder = new GenericRecordBuilder(keySchema);
        Random rand = new Random();
        for (int i = 0; i < numMsg; ++i) {
            // value record
            String key1 = UUID.randomUUID().toString();
            valueBuilder.set("key1", key1);
            valueBuilder.set("key2", rand.nextInt());
            valueBuilder.set("key3", rand.nextBoolean());
            List<Integer> list = Lists.newArrayList();
            list.add(rand.nextInt(100));
            list.add(rand.nextInt(100));
            list.add(rand.nextInt(100));
            valueBuilder.set("key5", list);
            GenericRecordBuilder innerBuilder = new GenericRecordBuilder(valueSchema.getField("key6").schema());
            innerBuilder.set("key61", rand.nextDouble());
            innerBuilder.set("key62", rand.nextDouble());
            valueBuilder.set("key6", innerBuilder.build());
            Record producerRecord = valueBuilder.build();
            // key record
            keyBuilder.set("key1", key1);
            Record keyRecord = keyBuilder.build();
            ProducerRecord<Object, GenericRecord> record = new ProducerRecord<>(topic, keyRecord, producerRecord);
            producer.send(record);
        }
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Schema(org.apache.avro.Schema) Random(java.util.Random) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) JsonObject(com.google.gson.JsonObject) GenericRecord(org.apache.avro.generic.GenericRecord) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Record(org.apache.avro.generic.GenericData.Record) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 65 with GenericRecordBuilder

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

the class AvroUtilsTest method testUnionFieldInAvroSchema.

@Test
public void testUnionFieldInAvroSchema() {
    List<org.apache.avro.Schema.Field> fields = Lists.newArrayList();
    List<org.apache.avro.Schema> unionFields = Lists.newArrayList();
    unionFields.add(org.apache.avro.Schema.create(Type.INT));
    unionFields.add(org.apache.avro.Schema.create(Type.STRING));
    fields.add(new org.apache.avro.Schema.Field("union", org.apache.avro.Schema.createUnion(unionFields), "", null));
    org.apache.avro.Schema avroSchema = org.apache.avro.Schema.createRecord("topLevelRecord", null, null, false, fields);
    OneOfType oneOfType = OneOfType.create(Field.of("int", FieldType.INT32), Field.of("string", FieldType.STRING));
    Schema expectedSchema = Schema.builder().addLogicalTypeField("union", oneOfType).build();
    assertEquals(expectedSchema, AvroUtils.toBeamSchema(avroSchema));
    GenericRecord genericRecord = new GenericRecordBuilder(avroSchema).set("union", 23423).build();
    Row expectedRow = Row.withSchema(expectedSchema).addValue(oneOfType.createValue(0, 23423)).build();
    assertEquals(expectedRow, AvroUtils.toBeamRowStrict(genericRecord, expectedSchema));
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Field(org.apache.beam.sdk.schemas.Schema.Field) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Row(org.apache.beam.sdk.values.Row) GenericRecord(org.apache.avro.generic.GenericRecord) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) 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