Search in sources :

Example 1 with PulsarSchema

use of org.apache.flink.connector.pulsar.common.schema.PulsarSchema in project flink by apache.

the class ProtobufNativeSchemaFactoryTest method createProtobufNativeSchemaFromSchemaInfo.

@Test
void createProtobufNativeSchemaFromSchemaInfo() {
    ProtobufNativeSchema<TestMessage> schema1 = ProtobufNativeSchema.of(TestMessage.class);
    PulsarSchema<TestMessage> pulsarSchema = new PulsarSchema<>(schema1, TestMessage.class);
    ProtobufNativeSchemaFactory<TestMessage> factory = new ProtobufNativeSchemaFactory<>();
    Schema<TestMessage> schema2 = factory.createSchema(pulsarSchema.getSchemaInfo());
    assertThat(schema2).hasFieldOrPropertyWithValue("schemaInfo", pulsarSchema.getSchemaInfo()).isInstanceOf(ProtobufNativeSchema.class);
    // Encode by original schema and decode by new schema
    TestMessage message = TestMessage.newBuilder().setStringField(randomAlphabetic(10)).setDoubleField(ThreadLocalRandom.current().nextDouble()).setIntField(ThreadLocalRandom.current().nextInt()).setTestEnum(TestEnum.SHARED).setNestedField(SubMessage.newBuilder().setFoo(randomAlphabetic(10)).setBar(ThreadLocalRandom.current().nextDouble()).build()).addRepeatedField(randomAlphabetic(13)).build();
    byte[] bytes = schema1.encode(message);
    TestMessage message1 = schema2.decode(bytes);
    assertEquals(message, message1);
}
Also used : TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) PulsarSchema(org.apache.flink.connector.pulsar.common.schema.PulsarSchema) Test(org.junit.jupiter.api.Test)

Example 2 with PulsarSchema

use of org.apache.flink.connector.pulsar.common.schema.PulsarSchema in project flink by apache.

the class AvroSchemaFactoryTest method createAvroTypeInformationAndSerializeValues.

@Test
void createAvroTypeInformationAndSerializeValues() throws Exception {
    AvroSchema<StructWithAnnotations> schema = AvroSchema.of(StructWithAnnotations.class);
    PulsarSchema<StructWithAnnotations> pulsarSchema = new PulsarSchema<>(schema, StructWithAnnotations.class);
    StructWithAnnotations struct1 = new StructWithAnnotations();
    struct1.setField1(5678);
    AvroSchemaFactory<StructWithAnnotations> factory = new AvroSchemaFactory<>();
    TypeInformation<StructWithAnnotations> information = factory.createTypeInfo(pulsarSchema.getSchemaInfo());
    assertThat(information).isInstanceOf(PulsarSchemaTypeInformation.class).hasFieldOrPropertyWithValue("typeClass", StructWithAnnotations.class);
    // Serialize by type information.
    TypeSerializer<StructWithAnnotations> serializer = information.createSerializer(new ExecutionConfig());
    // TypeInformation serialization.
    assertDoesNotThrow(() -> InstantiationUtil.clone(information));
    assertDoesNotThrow(() -> InstantiationUtil.clone(serializer));
    TestOutputView output = new TestOutputView();
    serializer.serialize(struct1, output);
    TestInputView input = output.getInputView();
    StructWithAnnotations struct2 = serializer.deserialize(input);
    assertThat(struct2).hasFieldOrPropertyWithValue("field1", struct1.getField1()).hasFieldOrPropertyWithValue("field2", null).hasFieldOrPropertyWithValue("field3", null);
}
Also used : TestOutputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView) TestInputView(org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView) PulsarSchemaTypeInformation(org.apache.flink.connector.pulsar.common.schema.PulsarSchemaTypeInformation) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) PulsarSchema(org.apache.flink.connector.pulsar.common.schema.PulsarSchema) Test(org.junit.jupiter.api.Test)

Aggregations

PulsarSchema (org.apache.flink.connector.pulsar.common.schema.PulsarSchema)2 Test (org.junit.jupiter.api.Test)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 TestInputView (org.apache.flink.api.common.typeutils.ComparatorTestBase.TestInputView)1 TestOutputView (org.apache.flink.api.common.typeutils.ComparatorTestBase.TestOutputView)1 TestMessage (org.apache.flink.connector.pulsar.SampleMessage.TestMessage)1 PulsarSchemaTypeInformation (org.apache.flink.connector.pulsar.common.schema.PulsarSchemaTypeInformation)1