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);
}
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);
}
Aggregations