Search in sources :

Example 6 with RowType

use of org.apache.flink.table.types.logical.RowType 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 7 with RowType

use of org.apache.flink.table.types.logical.RowType 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 8 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class AvroFormatFactory method createDecodingFormat.

@Override
public DecodingFormat<DeserializationSchema<RowData>> createDecodingFormat(DynamicTableFactory.Context context, ReadableConfig formatOptions) {
    FactoryUtil.validateFactoryOptions(this, formatOptions);
    return new ProjectableDecodingFormat<DeserializationSchema<RowData>>() {

        @Override
        public DeserializationSchema<RowData> createRuntimeDecoder(DynamicTableSource.Context context, DataType physicalDataType, int[][] projections) {
            final DataType producedDataType = Projection.of(projections).project(physicalDataType);
            final RowType rowType = (RowType) producedDataType.getLogicalType();
            final TypeInformation<RowData> rowDataTypeInfo = context.createTypeInformation(producedDataType);
            return new AvroRowDataDeserializationSchema(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)

Example 9 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class DebeziumAvroSerDeSchemaTest method testDeserialization.

public List<String> testDeserialization(String dataPath) throws Exception {
    RowType rowTypeDe = DebeziumAvroDeserializationSchema.createDebeziumAvroRowType(fromLogicalToDataType(rowType));
    client.register(SUBJECT, DEBEZIUM_SCHEMA_COMPATIBLE_TEST, 1, 81);
    DebeziumAvroDeserializationSchema dbzDeserializer = new DebeziumAvroDeserializationSchema(InternalTypeInfo.of(rowType), getDeserializationSchema(rowTypeDe));
    dbzDeserializer.open(mock(DeserializationSchema.InitializationContext.class));
    SimpleCollector collector = new SimpleCollector();
    dbzDeserializer.deserialize(readBytesFromFile(dataPath), collector);
    return collector.list.stream().map(Object::toString).collect(Collectors.toList());
}
Also used : RowType(org.apache.flink.table.types.logical.RowType)

Example 10 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class AvroRowDataDeSerializationSchemaTest method createDeserializationSchema.

private AvroRowDataDeserializationSchema createDeserializationSchema(DataType dataType) throws Exception {
    final RowType rowType = (RowType) dataType.getLogicalType();
    final TypeInformation<RowData> typeInfo = InternalTypeInfo.of(rowType);
    AvroRowDataDeserializationSchema deserializationSchema = new AvroRowDataDeserializationSchema(rowType, typeInfo);
    deserializationSchema.open(null);
    return deserializationSchema;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) RowType(org.apache.flink.table.types.logical.RowType)

Aggregations

RowType (org.apache.flink.table.types.logical.RowType)212 RowData (org.apache.flink.table.data.RowData)108 LogicalType (org.apache.flink.table.types.logical.LogicalType)59 DataType (org.apache.flink.table.types.DataType)57 Transformation (org.apache.flink.api.dag.Transformation)50 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)46 TableException (org.apache.flink.table.api.TableException)37 Test (org.junit.Test)36 GenericRowData (org.apache.flink.table.data.GenericRowData)33 ArrayList (java.util.ArrayList)28 List (java.util.List)28 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)26 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)25 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)22 TableConfig (org.apache.flink.table.api.TableConfig)19 ArrayType (org.apache.flink.table.types.logical.ArrayType)19 TimestampType (org.apache.flink.table.types.logical.TimestampType)19 DecimalType (org.apache.flink.table.types.logical.DecimalType)17 Collections (java.util.Collections)16 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)16