use of org.apache.flink.formats.avro.AvroRowDataSerializationSchema in project flink by apache.
the class RegistryAvroRowDataSeDeSchemaTest method testRowDataWriteReadWithSchema.
private void testRowDataWriteReadWithSchema(Schema schema) throws Exception {
DataType dataType = AvroSchemaConverter.convertToDataType(schema.toString());
RowType rowType = (RowType) dataType.getLogicalType();
AvroRowDataSerializationSchema serializer = getSerializationSchema(rowType, schema);
Schema writeSchema = AvroSchemaConverter.convertToSchema(dataType.getLogicalType());
AvroRowDataDeserializationSchema deserializer = getDeserializationSchema(rowType, writeSchema);
serializer.open(null);
deserializer.open(null);
assertNull(deserializer.deserialize(null));
RowData oriData = address2RowData(address);
byte[] serialized = serializer.serialize(oriData);
RowData rowData = deserializer.deserialize(serialized);
assertThat(rowData.getArity(), equalTo(schema.getFields().size()));
assertEquals(address.getNum(), rowData.getInt(0));
assertEquals(address.getStreet(), rowData.getString(1).toString());
if (schema != ADDRESS_SCHEMA_COMPATIBLE) {
assertEquals(address.getCity(), rowData.getString(2).toString());
assertEquals(address.getState(), rowData.getString(3).toString());
assertEquals(address.getZip(), rowData.getString(4).toString());
}
}
use of org.apache.flink.formats.avro.AvroRowDataSerializationSchema in project flink by apache.
the class RegistryAvroFormatFactory method createEncodingFormat.
@Override
public EncodingFormat<SerializationSchema<RowData>> createEncodingFormat(DynamicTableFactory.Context context, ReadableConfig formatOptions) {
FactoryUtil.validateFactoryOptions(this, formatOptions);
String schemaRegistryURL = formatOptions.get(URL);
Optional<String> subject = formatOptions.getOptional(SUBJECT);
Map<String, ?> optionalPropertiesMap = buildOptionalPropertiesMap(formatOptions);
if (!subject.isPresent()) {
throw new ValidationException(String.format("Option %s.%s is required for serialization", IDENTIFIER, SUBJECT.key()));
}
return new EncodingFormat<SerializationSchema<RowData>>() {
@Override
public SerializationSchema<RowData> createRuntimeEncoder(DynamicTableSink.Context context, DataType consumedDataType) {
final RowType rowType = (RowType) consumedDataType.getLogicalType();
return new AvroRowDataSerializationSchema(rowType, ConfluentRegistryAvroSerializationSchema.forGeneric(subject.get(), AvroSchemaConverter.convertToSchema(rowType), schemaRegistryURL, optionalPropertiesMap), RowDataToAvroConverters.createConverter(rowType));
}
@Override
public ChangelogMode getChangelogMode() {
return ChangelogMode.insertOnly();
}
};
}
use of org.apache.flink.formats.avro.AvroRowDataSerializationSchema in project flink by apache.
the class RegistryAvroFormatFactoryTest method testSerializationSchemaWithOptionalProperties.
@Test
public void testSerializationSchemaWithOptionalProperties() {
final AvroRowDataSerializationSchema expectedSer = new AvroRowDataSerializationSchema(ROW_TYPE, ConfluentRegistryAvroSerializationSchema.forGeneric(SUBJECT, AvroSchemaConverter.convertToSchema(ROW_TYPE), REGISTRY_URL, EXPECTED_OPTIONAL_PROPERTIES), RowDataToAvroConverters.createConverter(ROW_TYPE));
final DynamicTableSink actualSink = createTableSink(SCHEMA, getOptionalProperties());
assertThat(actualSink, instanceOf(TestDynamicTableFactory.DynamicTableSinkMock.class));
TestDynamicTableFactory.DynamicTableSinkMock sinkMock = (TestDynamicTableFactory.DynamicTableSinkMock) actualSink;
SerializationSchema<RowData> actualSer = sinkMock.valueFormat.createRuntimeEncoder(null, SCHEMA.toPhysicalRowDataType());
assertEquals(expectedSer, actualSer);
}
use of org.apache.flink.formats.avro.AvroRowDataSerializationSchema in project flink by apache.
the class RegistryAvroFormatFactoryTest method testSerializationSchema.
@Test
public void testSerializationSchema() {
final AvroRowDataSerializationSchema expectedSer = new AvroRowDataSerializationSchema(ROW_TYPE, ConfluentRegistryAvroSerializationSchema.forGeneric(SUBJECT, AvroSchemaConverter.convertToSchema(ROW_TYPE), REGISTRY_URL), RowDataToAvroConverters.createConverter(ROW_TYPE));
final DynamicTableSink actualSink = createTableSink(SCHEMA, getDefaultOptions());
assertThat(actualSink, instanceOf(TestDynamicTableFactory.DynamicTableSinkMock.class));
TestDynamicTableFactory.DynamicTableSinkMock sinkMock = (TestDynamicTableFactory.DynamicTableSinkMock) actualSink;
SerializationSchema<RowData> actualSer = sinkMock.valueFormat.createRuntimeEncoder(null, SCHEMA.toPhysicalRowDataType());
assertEquals(expectedSer, actualSer);
}
Aggregations