Search in sources :

Example 1 with FlinkSchema

use of org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema in project pulsar-flink by streamnative.

the class RowDataDerSerializationSchemaTest method testProtoBufSerializeDeserialize.

@Test
public void testProtoBufSerializeDeserialize() throws Exception {
    String topicName = newTopic();
    RowType rowType = PbRowTypeInformation.generateRowType(SimpleTest.getDescriptor());
    PbRowDataSerializationSchema serializationSchema = new PbRowDataSerializationSchema(rowType, SimpleTest.class.getName());
    PbRowDataDeserializationSchema deserializationSchema = new PbRowDataDeserializationSchema(rowType, InternalTypeInfo.of(rowType), SimpleTest.class.getName(), false, false);
    SimpleTest simple = SimpleTest.newBuilder().setA(1).setB(2L).setC(false).setD(0.1f).setE(0.01).setF("haha").setG(ByteString.copyFrom(new byte[] { 1 })).setH(SimpleTest.Corpus.IMAGES).setFAbc7D(// test fieldNameToJsonName
    1).build();
    RowData rowData = deserializationSchema.deserialize(simple.toByteArray());
    org.apache.pulsar.client.api.Schema<SimpleTest> schema = org.apache.pulsar.client.api.Schema.PROTOBUF_NATIVE(SimpleTest.class);
    final FlinkSchema<RowData> rowDataFlinkSchema = new FlinkSchema<>(schema.getSchemaInfo(), serializationSchema, deserializationSchema);
    sendMessage(topicName, rowDataFlinkSchema, rowData);
    final CompletableFuture<byte[]> consumer = autoConsumer(topicName);
    RowData newRowData = deserializationSchema.deserialize(consumer.get(10000, TimeUnit.MILLISECONDS));
    newRowData = validatePbRow(newRowData, PbRowTypeInformation.generateRowType(SimpleTest.getDescriptor()));
    assertEquals(9, newRowData.getArity());
    assertEquals(1, newRowData.getInt(0));
    assertEquals(2L, newRowData.getLong(1));
    assertFalse((boolean) newRowData.getBoolean(2));
    assertEquals(Float.valueOf(0.1f), Float.valueOf(newRowData.getFloat(3)));
    assertEquals(Double.valueOf(0.01d), Double.valueOf(newRowData.getDouble(4)));
    assertEquals("haha", newRowData.getString(5).toString());
    assertEquals(1, (newRowData.getBinary(6))[0]);
    assertEquals("IMAGES", newRowData.getString(7).toString());
    assertEquals(1, newRowData.getInt(8));
}
Also used : PbRowDataDeserializationSchema(org.apache.flink.formats.protobuf.deserialize.PbRowDataDeserializationSchema) RowType(org.apache.flink.table.types.logical.RowType) ByteString(com.google.protobuf.ByteString) PbRowDataSerializationSchema(org.apache.flink.formats.protobuf.serialize.PbRowDataSerializationSchema) RowData(org.apache.flink.table.data.RowData) SimpleTest(org.apache.flink.formats.protobuf.testproto.SimpleTest) FlinkSchema(org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema) SimpleTest(org.apache.flink.formats.protobuf.testproto.SimpleTest) Test(org.junit.Test)

Example 2 with FlinkSchema

use of org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema in project pulsar-flink by streamnative.

the class DynamicPulsarSerializationSchema method buildSchema.

private FlinkSchema<RowData> buildSchema() {
    if (StringUtils.isBlank(valueFormatType)) {
        return new FlinkSchema<>(Schema.BYTES.getSchemaInfo(), valueSerialization, null);
    }
    Configuration configuration = new Configuration();
    hackPbSerializationSchema(configuration);
    SchemaInfo schemaInfo = SchemaUtils.tableSchemaToSchemaInfo(valueFormatType, valueDataType, configuration);
    return new FlinkSchema<>(schemaInfo, valueSerialization, null);
}
Also used : Configuration(org.apache.flink.configuration.Configuration) FlinkSchema(org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo)

Example 3 with FlinkSchema

use of org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema in project pulsar-flink by streamnative.

the class RowDataDerSerializationSchemaTest method toPulsarSchema.

public <T> org.apache.pulsar.client.api.Schema<T> toPulsarSchema(SchemaType schemaType, Schema avroSchema, SerializationSchema<T> serializationSchema, DeserializationSchema<T> deserializationSchema) {
    byte[] schemaBytes = avroSchema.toString().getBytes(StandardCharsets.UTF_8);
    SchemaInfoImpl si = new SchemaInfoImpl();
    si.setName("Record");
    si.setSchema(schemaBytes);
    si.setType(schemaType);
    return new FlinkSchema<>(si, serializationSchema, deserializationSchema);
}
Also used : SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) FlinkSchema(org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema)

Aggregations

FlinkSchema (org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema)3 ByteString (com.google.protobuf.ByteString)1 Configuration (org.apache.flink.configuration.Configuration)1 PbRowDataDeserializationSchema (org.apache.flink.formats.protobuf.deserialize.PbRowDataDeserializationSchema)1 PbRowDataSerializationSchema (org.apache.flink.formats.protobuf.serialize.PbRowDataSerializationSchema)1 SimpleTest (org.apache.flink.formats.protobuf.testproto.SimpleTest)1 RowData (org.apache.flink.table.data.RowData)1 RowType (org.apache.flink.table.types.logical.RowType)1 SchemaInfoImpl (org.apache.pulsar.client.impl.schema.SchemaInfoImpl)1 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)1 Test (org.junit.Test)1