Search in sources :

Example 1 with AvroRowDataDeserializationSchema

use of org.apache.flink.formats.avro.AvroRowDataDeserializationSchema 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 AvroRowDataDeserializationSchema

use of org.apache.flink.formats.avro.AvroRowDataDeserializationSchema in project flink by apache.

the class RegistryAvroRowDataSeDeSchemaTest method testRowDataReadWithNonRegistryAvro.

@Test
public void testRowDataReadWithNonRegistryAvro() throws Exception {
    DataType dataType = AvroSchemaConverter.convertToDataType(ADDRESS_SCHEMA.toString());
    RowType rowType = (RowType) dataType.getLogicalType();
    AvroRowDataDeserializationSchema deserializer = getDeserializationSchema(rowType, ADDRESS_SCHEMA);
    deserializer.open(null);
    client.register(SUBJECT, ADDRESS_SCHEMA);
    byte[] oriBytes = writeRecord(address, ADDRESS_SCHEMA);
    expectedEx.expect(IOException.class);
    expectedEx.expect(containsCause(new IOException("Unknown data format. Magic number does not match")));
    deserializer.deserialize(oriBytes);
}
Also used : DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType) IOException(java.io.IOException) AvroRowDataDeserializationSchema(org.apache.flink.formats.avro.AvroRowDataDeserializationSchema) Test(org.junit.Test)

Example 3 with AvroRowDataDeserializationSchema

use of org.apache.flink.formats.avro.AvroRowDataDeserializationSchema in project flink by apache.

the class RegistryAvroFormatFactoryTest method testDeserializationSchema.

@Test
public void testDeserializationSchema() {
    final AvroRowDataDeserializationSchema expectedDeser = new AvroRowDataDeserializationSchema(ConfluentRegistryAvroDeserializationSchema.forGeneric(AvroSchemaConverter.convertToSchema(ROW_TYPE), REGISTRY_URL), AvroToRowDataConverters.createRowConverter(ROW_TYPE), InternalTypeInfo.of(ROW_TYPE));
    final DynamicTableSource actualSource = createTableSource(SCHEMA, getDefaultOptions());
    assertThat(actualSource, instanceOf(TestDynamicTableFactory.DynamicTableSourceMock.class));
    TestDynamicTableFactory.DynamicTableSourceMock scanSourceMock = (TestDynamicTableFactory.DynamicTableSourceMock) actualSource;
    DeserializationSchema<RowData> actualDeser = scanSourceMock.valueFormat.createRuntimeDecoder(ScanRuntimeProviderContext.INSTANCE, SCHEMA.toPhysicalRowDataType());
    assertEquals(expectedDeser, actualDeser);
}
Also used : RowData(org.apache.flink.table.data.RowData) AvroRowDataDeserializationSchema(org.apache.flink.formats.avro.AvroRowDataDeserializationSchema) TestDynamicTableFactory(org.apache.flink.table.factories.TestDynamicTableFactory) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) Test(org.junit.Test)

Example 4 with AvroRowDataDeserializationSchema

use of org.apache.flink.formats.avro.AvroRowDataDeserializationSchema in project flink by apache.

the class RegistryAvroFormatFactoryTest method testDeserializationSchemaWithOptionalProperties.

@Test
public void testDeserializationSchemaWithOptionalProperties() {
    final AvroRowDataDeserializationSchema expectedDeser = new AvroRowDataDeserializationSchema(ConfluentRegistryAvroDeserializationSchema.forGeneric(AvroSchemaConverter.convertToSchema(ROW_TYPE), REGISTRY_URL, EXPECTED_OPTIONAL_PROPERTIES), AvroToRowDataConverters.createRowConverter(ROW_TYPE), InternalTypeInfo.of(ROW_TYPE));
    final DynamicTableSource actualSource = createTableSource(SCHEMA, getOptionalProperties());
    assertThat(actualSource, instanceOf(TestDynamicTableFactory.DynamicTableSourceMock.class));
    TestDynamicTableFactory.DynamicTableSourceMock scanSourceMock = (TestDynamicTableFactory.DynamicTableSourceMock) actualSource;
    DeserializationSchema<RowData> actualDeser = scanSourceMock.valueFormat.createRuntimeDecoder(ScanRuntimeProviderContext.INSTANCE, SCHEMA.toPhysicalRowDataType());
    assertEquals(expectedDeser, actualDeser);
}
Also used : RowData(org.apache.flink.table.data.RowData) AvroRowDataDeserializationSchema(org.apache.flink.formats.avro.AvroRowDataDeserializationSchema) TestDynamicTableFactory(org.apache.flink.table.factories.TestDynamicTableFactory) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) Test(org.junit.Test)

Example 5 with AvroRowDataDeserializationSchema

use of org.apache.flink.formats.avro.AvroRowDataDeserializationSchema in project flink by apache.

the class RegistryAvroFormatFactory method createDecodingFormat.

@Override
public DecodingFormat<DeserializationSchema<RowData>> createDecodingFormat(DynamicTableFactory.Context context, ReadableConfig formatOptions) {
    FactoryUtil.validateFactoryOptions(this, formatOptions);
    String schemaRegistryURL = formatOptions.get(URL);
    Map<String, ?> optionalPropertiesMap = buildOptionalPropertiesMap(formatOptions);
    return new ProjectableDecodingFormat<DeserializationSchema<RowData>>() {

        @Override
        public DeserializationSchema<RowData> createRuntimeDecoder(DynamicTableSource.Context context, DataType producedDataType, int[][] projections) {
            producedDataType = Projection.of(projections).project(producedDataType);
            final RowType rowType = (RowType) producedDataType.getLogicalType();
            final TypeInformation<RowData> rowDataTypeInfo = context.createTypeInformation(producedDataType);
            return new AvroRowDataDeserializationSchema(ConfluentRegistryAvroDeserializationSchema.forGeneric(AvroSchemaConverter.convertToSchema(rowType), schemaRegistryURL, optionalPropertiesMap), AvroToRowDataConverters.createRowConverter(rowType), rowDataTypeInfo);
        }

        @Override
        public ChangelogMode getChangelogMode() {
            return ChangelogMode.insertOnly();
        }
    };
}
Also used : RowData(org.apache.flink.table.data.RowData) ProjectableDecodingFormat(org.apache.flink.table.connector.format.ProjectableDecodingFormat) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType) AvroRowDataDeserializationSchema(org.apache.flink.formats.avro.AvroRowDataDeserializationSchema)

Aggregations

AvroRowDataDeserializationSchema (org.apache.flink.formats.avro.AvroRowDataDeserializationSchema)5 RowData (org.apache.flink.table.data.RowData)4 DataType (org.apache.flink.table.types.DataType)3 RowType (org.apache.flink.table.types.logical.RowType)3 Test (org.junit.Test)3 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)2 TestDynamicTableFactory (org.apache.flink.table.factories.TestDynamicTableFactory)2 IOException (java.io.IOException)1 Schema (org.apache.avro.Schema)1 AvroRowDataSerializationSchema (org.apache.flink.formats.avro.AvroRowDataSerializationSchema)1 RegistryAvroDeserializationSchema (org.apache.flink.formats.avro.RegistryAvroDeserializationSchema)1 RegistryAvroSerializationSchema (org.apache.flink.formats.avro.RegistryAvroSerializationSchema)1 ProjectableDecodingFormat (org.apache.flink.table.connector.format.ProjectableDecodingFormat)1 GenericRowData (org.apache.flink.table.data.GenericRowData)1