Search in sources :

Example 1 with AvroRowDataSerializationSchema

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());
    }
}
Also used : AvroRowDataSerializationSchema(org.apache.flink.formats.avro.AvroRowDataSerializationSchema) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) RegistryAvroDeserializationSchema(org.apache.flink.formats.avro.RegistryAvroDeserializationSchema) AvroRowDataDeserializationSchema(org.apache.flink.formats.avro.AvroRowDataDeserializationSchema) Schema(org.apache.avro.Schema) RegistryAvroSerializationSchema(org.apache.flink.formats.avro.RegistryAvroSerializationSchema) AvroRowDataSerializationSchema(org.apache.flink.formats.avro.AvroRowDataSerializationSchema) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType) AvroRowDataDeserializationSchema(org.apache.flink.formats.avro.AvroRowDataDeserializationSchema)

Example 2 with AvroRowDataSerializationSchema

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();
        }
    };
}
Also used : EncodingFormat(org.apache.flink.table.connector.format.EncodingFormat) RowData(org.apache.flink.table.data.RowData) AvroRowDataSerializationSchema(org.apache.flink.formats.avro.AvroRowDataSerializationSchema) ValidationException(org.apache.flink.table.api.ValidationException) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType)

Example 3 with AvroRowDataSerializationSchema

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);
}
Also used : AvroRowDataSerializationSchema(org.apache.flink.formats.avro.AvroRowDataSerializationSchema) RowData(org.apache.flink.table.data.RowData) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) TestDynamicTableFactory(org.apache.flink.table.factories.TestDynamicTableFactory) Test(org.junit.Test)

Example 4 with AvroRowDataSerializationSchema

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);
}
Also used : AvroRowDataSerializationSchema(org.apache.flink.formats.avro.AvroRowDataSerializationSchema) RowData(org.apache.flink.table.data.RowData) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) TestDynamicTableFactory(org.apache.flink.table.factories.TestDynamicTableFactory) Test(org.junit.Test)

Aggregations

AvroRowDataSerializationSchema (org.apache.flink.formats.avro.AvroRowDataSerializationSchema)4 RowData (org.apache.flink.table.data.RowData)4 DynamicTableSink (org.apache.flink.table.connector.sink.DynamicTableSink)2 TestDynamicTableFactory (org.apache.flink.table.factories.TestDynamicTableFactory)2 DataType (org.apache.flink.table.types.DataType)2 RowType (org.apache.flink.table.types.logical.RowType)2 Test (org.junit.Test)2 Schema (org.apache.avro.Schema)1 AvroRowDataDeserializationSchema (org.apache.flink.formats.avro.AvroRowDataDeserializationSchema)1 RegistryAvroDeserializationSchema (org.apache.flink.formats.avro.RegistryAvroDeserializationSchema)1 RegistryAvroSerializationSchema (org.apache.flink.formats.avro.RegistryAvroSerializationSchema)1 ValidationException (org.apache.flink.table.api.ValidationException)1 EncodingFormat (org.apache.flink.table.connector.format.EncodingFormat)1 GenericRowData (org.apache.flink.table.data.GenericRowData)1