Search in sources :

Example 66 with GenericRecordBuilder

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

the class AvroUtilsTest method testJdbcLogicalVarCharRowDataToGenericRecord.

@Test
public void testJdbcLogicalVarCharRowDataToGenericRecord() {
    Schema beamSchema = Schema.builder().addField(Field.of("my_varchar_field", FieldType.logicalType(JdbcType.StringType.varchar(10)))).addField(Field.of("my_longvarchar_field", FieldType.logicalType(JdbcType.StringType.longvarchar(50)))).addField(Field.of("my_nvarchar_field", FieldType.logicalType(JdbcType.StringType.nvarchar(10)))).addField(Field.of("my_longnvarchar_field", FieldType.logicalType(JdbcType.StringType.longnvarchar(50)))).build();
    Row rowData = Row.withSchema(beamSchema).addValue("varchar_value").addValue("longvarchar_value").addValue("nvarchar_value").addValue("longnvarchar_value").build();
    org.apache.avro.Schema avroSchema = AvroUtils.toAvroSchema(beamSchema);
    GenericRecord expectedRecord = new GenericRecordBuilder(avroSchema).set("my_varchar_field", "varchar_value").set("my_longvarchar_field", "longvarchar_value").set("my_nvarchar_field", "nvarchar_value").set("my_longnvarchar_field", "longnvarchar_value").build();
    assertEquals(expectedRecord, AvroUtils.toGenericRecord(rowData, avroSchema));
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Row(org.apache.beam.sdk.values.Row) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 67 with GenericRecordBuilder

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

the class AvroUtilsTest method testNullableFieldsInBeamSchema.

@Test
public void testNullableFieldsInBeamSchema() {
    Schema beamSchema = Schema.builder().addNullableField("int", FieldType.INT32).addArrayField("array", FieldType.INT32.withNullable(true)).addMapField("map", FieldType.STRING, FieldType.INT32.withNullable(true)).build();
    List<org.apache.avro.Schema.Field> fields = Lists.newArrayList();
    fields.add(new org.apache.avro.Schema.Field("int", ReflectData.makeNullable(org.apache.avro.Schema.create(Type.INT)), "", null));
    fields.add(new org.apache.avro.Schema.Field("array", org.apache.avro.Schema.createArray(ReflectData.makeNullable(org.apache.avro.Schema.create(Type.INT))), "", null));
    fields.add(new org.apache.avro.Schema.Field("map", org.apache.avro.Schema.createMap(ReflectData.makeNullable(org.apache.avro.Schema.create(Type.INT))), "", null));
    org.apache.avro.Schema avroSchema = org.apache.avro.Schema.createRecord("topLevelRecord", null, null, false, fields);
    assertEquals(avroSchema, AvroUtils.toAvroSchema(beamSchema));
    Map<Utf8, Object> nullMapUtf8 = Maps.newHashMap();
    nullMapUtf8.put(new Utf8("k1"), null);
    Map<String, Object> nullMapString = Maps.newHashMap();
    nullMapString.put("k1", null);
    GenericRecord expectedGenericRecord = new GenericRecordBuilder(avroSchema).set("int", null).set("array", Lists.newArrayList((Object) null)).set("map", nullMapUtf8).build();
    Row row = Row.withSchema(beamSchema).addValue(null).addValue(Lists.newArrayList((Object) null)).addValue(nullMapString).build();
    assertEquals(expectedGenericRecord, AvroUtils.toGenericRecord(row, avroSchema));
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Field(org.apache.beam.sdk.schemas.Schema.Field) Utf8(org.apache.avro.util.Utf8) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Row(org.apache.beam.sdk.values.Row) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 68 with GenericRecordBuilder

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

the class AvroUtilsTest method testUnionFieldInBeamSchema.

@Test
public void testUnionFieldInBeamSchema() {
    OneOfType oneOfType = OneOfType.create(Field.of("int", FieldType.INT32), Field.of("string", FieldType.STRING));
    Schema beamSchema = Schema.builder().addLogicalTypeField("union", oneOfType).build();
    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);
    GenericRecord expectedGenericRecord = new GenericRecordBuilder(avroSchema).set("union", 23423).build();
    Row row = Row.withSchema(beamSchema).addValue(oneOfType.createValue(0, 23423)).build();
    assertEquals(expectedGenericRecord, AvroUtils.toGenericRecord(row, avroSchema));
}
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)

Example 69 with GenericRecordBuilder

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

the class AvroUtilsTest method getGenericRecord.

private static GenericRecord getGenericRecord() {
    LogicalType decimalType = LogicalTypes.decimal(Integer.MAX_VALUE).addToSchema(org.apache.avro.Schema.create(Type.BYTES)).getLogicalType();
    ByteBuffer encodedDecimal = new Conversions.DecimalConversion().toBytes(BIG_DECIMAL, null, decimalType);
    return new GenericRecordBuilder(getAvroSchema()).set("bool", true).set("int", 43).set("long", 44L).set("float", (float) 44.1).set("double", (double) 44.2).set("string", new Utf8("string")).set("bytes", ByteBuffer.wrap(BYTE_ARRAY)).set("decimal", encodedDecimal).set("timestampMillis", DATE_TIME.getMillis()).set("row", getSubGenericRecord("row")).set("array", ImmutableList.of(getSubGenericRecord("array"), getSubGenericRecord("array"))).set("map", ImmutableMap.of(new Utf8("k1"), getSubGenericRecord("map"), new Utf8("k2"), getSubGenericRecord("map"))).build();
}
Also used : Conversions(org.apache.avro.Conversions) LogicalType(org.apache.avro.LogicalType) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Utf8(org.apache.avro.util.Utf8) ByteBuffer(java.nio.ByteBuffer)

Example 70 with GenericRecordBuilder

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

the class FakeJobService method writeRowsHelper.

private void writeRowsHelper(List<TableRow> rows, Schema avroSchema, String destinationPattern, int shard) {
    String filename = destinationPattern.replace("*", String.format("%012d", shard));
    try (WritableByteChannel channel = FileSystems.create(FileSystems.matchNewResource(filename, false), MimeTypes.BINARY);
        DataFileWriter<GenericRecord> tableRowWriter = new DataFileWriter<>(new GenericDatumWriter<GenericRecord>(avroSchema)).create(avroSchema, Channels.newOutputStream(channel))) {
        for (Map<String, Object> record : rows) {
            GenericRecordBuilder genericRecordBuilder = new GenericRecordBuilder(avroSchema);
            for (Map.Entry<String, Object> field : record.entrySet()) {
                genericRecordBuilder.set(field.getKey(), field.getValue());
            }
            tableRowWriter.append(genericRecordBuilder.build());
        }
    } catch (IOException e) {
        throw new IllegalStateException(String.format("Could not create destination for extract job %s", filename), e);
    }
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord) Map(java.util.Map)

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