use of org.apache.beam.sdk.coders.AvroCoder in project components by Talend.
the class LazyAvroCoder method encode.
@Override
public void encode(Object value, OutputStream outputStream) throws IOException {
if (converter == null) {
converter = ConvertToIndexedRecord.getConverter((T) value);
}
IndexedRecord ir = converter.convertToAvro((T) value);
if (internalAvroCoder == null) {
Schema s = converter.getSchema();
avroSchemaHolder.put(s);
@SuppressWarnings("unchecked") AvroCoder<IndexedRecord> tCoder = (AvroCoder<IndexedRecord>) (AvroCoder<? extends IndexedRecord>) AvroCoder.of(ir.getSchema());
internalAvroCoder = tCoder;
}
LOG.debug("Internal AvroCoder's schema is {}", internalAvroCoder.getSchema());
LOG.debug("Encode value is {}", value);
internalAvroCoder.encode(convertToAvro(value), outputStream);
}
use of org.apache.beam.sdk.coders.AvroCoder in project beam by apache.
the class ConfluentSchemaRegistryDeserializerProviderTest method testGetCoder.
@Test
public void testGetCoder() {
String schemaRegistryUrl = "mock://my-scope-name";
String subject = "mytopic";
SchemaRegistryClient mockRegistryClient = mockSchemaRegistryClient(schemaRegistryUrl, subject);
CoderRegistry coderRegistry = CoderRegistry.createDefault();
AvroCoder coderV0 = (AvroCoder) mockDeserializerProvider(schemaRegistryUrl, subject, null).getCoder(coderRegistry);
assertEquals(AVRO_SCHEMA, coderV0.getSchema());
try {
Integer version = mockRegistryClient.getVersion(subject, AVRO_SCHEMA_V1);
AvroCoder coderV1 = (AvroCoder) mockDeserializerProvider(schemaRegistryUrl, subject, version).getCoder(coderRegistry);
assertEquals(AVRO_SCHEMA_V1, coderV1.getSchema());
} catch (IOException | RestClientException e) {
throw new RuntimeException("Unable to register schema for subject: " + subject, e);
}
}
use of org.apache.beam.sdk.coders.AvroCoder in project beam by apache.
the class AvroUtilsTest method testAvroSchemaCoders.
@Test
public void testAvroSchemaCoders() {
Pipeline pipeline = Pipeline.create();
org.apache.avro.Schema schema = org.apache.avro.Schema.createRecord("TestSubRecord", "TestSubRecord doc", "org.apache.beam.sdk.schemas.utils", false, getAvroSubSchemaFields());
GenericRecord record = new GenericRecordBuilder(getAvroSubSchema("simple")).set("bool", true).set("int", 42).build();
PCollection<GenericRecord> records = pipeline.apply(Create.of(record).withCoder(AvroCoder.of(schema)));
assertFalse(records.hasSchema());
records.setCoder(AvroUtils.schemaCoder(schema));
assertTrue(records.hasSchema());
CoderProperties.coderSerializable(records.getCoder());
AvroGeneratedUser user = new AvroGeneratedUser("foo", 42, "green");
PCollection<AvroGeneratedUser> users = pipeline.apply(Create.of(user).withCoder(AvroCoder.of(AvroGeneratedUser.class)));
assertFalse(users.hasSchema());
users.setCoder(AvroUtils.schemaCoder((AvroCoder<AvroGeneratedUser>) users.getCoder()));
assertTrue(users.hasSchema());
CoderProperties.coderSerializable(users.getCoder());
}
Aggregations