Search in sources :

Example 1 with SchemaInfo

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

the class TopicProducerRegister method getOrCreateProducer.

/**
 * Create or return the cached topic-related producer.
 */
@SuppressWarnings("unchecked")
private <T> Producer<T> getOrCreateProducer(String topic, Schema<T> schema) {
    Map<SchemaInfo, Producer<?>> producers = producerRegister.computeIfAbsent(topic, key -> new HashMap<>());
    SchemaInfo schemaInfo = schema.getSchemaInfo();
    if (producers.containsKey(schemaInfo)) {
        return (Producer<T>) producers.get(schemaInfo);
    } else {
        ProducerBuilder<T> builder = createProducerBuilder(pulsarClient, schema, sinkConfiguration);
        // Set the required topic name.
        builder.topic(topic);
        Producer<T> producer = sneakyClient(builder::create);
        producers.put(schemaInfo, producer);
        return producer;
    }
}
Also used : Producer(org.apache.pulsar.client.api.Producer) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo)

Example 2 with SchemaInfo

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

the class PulsarSchemaUtilsTest method encodeAndDecodeClassInfo.

@Test
void encodeAndDecodeClassInfo() {
    Schema<Foo> schema = Schema.AVRO(Foo.class);
    SchemaInfo info = schema.getSchemaInfo();
    SchemaInfo newInfo = PulsarSchemaUtils.encodeClassInfo(info, Foo.class);
    assertDoesNotThrow(() -> PulsarSchemaUtils.decodeClassInfo(newInfo));
    Class<Foo> clazz = PulsarSchemaUtils.decodeClassInfo(newInfo);
    assertEquals(clazz, Foo.class);
}
Also used : Foo(org.apache.flink.connector.pulsar.testutils.SampleData.Foo) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.junit.jupiter.api.Test)

Example 3 with SchemaInfo

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

the class PulsarSchemaUtilsTest method createSchemaForComplexSchema.

@Test
void createSchemaForComplexSchema() {
    // Avro
    Schema<Foo> avro1 = Schema.AVRO(Foo.class);
    PulsarSchema<Foo> avro2 = new PulsarSchema<>(avro1, Foo.class);
    SchemaInfo info1 = avro1.getSchemaInfo();
    assertThrows(NullPointerException.class, () -> PulsarSchemaUtils.createSchema(info1));
    Schema<Foo> schema = PulsarSchemaUtils.createSchema(avro2.getSchemaInfo());
    assertNotEquals(schema.getSchemaInfo(), avro1.getSchemaInfo());
    assertEquals(schema.getSchemaInfo(), avro2.getSchemaInfo());
    // JSON
    Schema<FL> json1 = Schema.JSON(FL.class);
    PulsarSchema<FL> json2 = new PulsarSchema<>(json1, FL.class);
    Schema<FL> json3 = PulsarSchemaUtils.createSchema(json2.getSchemaInfo());
    assertNotEquals(json3.getSchemaInfo(), json1.getSchemaInfo());
    assertEquals(json3.getSchemaInfo(), json2.getSchemaInfo());
    // Protobuf Native
    Schema<TestMessage> proto1 = Schema.PROTOBUF_NATIVE(TestMessage.class);
    PulsarSchema<TestMessage> proto2 = new PulsarSchema<>(proto1, TestMessage.class);
    Schema<TestMessage> proto3 = PulsarSchemaUtils.createSchema(proto2.getSchemaInfo());
    assertNotEquals(proto3.getSchemaInfo(), proto1.getSchemaInfo());
    assertEquals(proto3.getSchemaInfo(), proto2.getSchemaInfo());
    // KeyValue
    Schema<KeyValue<byte[], byte[]>> kvBytes1 = Schema.KV_BYTES();
    PulsarSchema<KeyValue<byte[], byte[]>> kvBytes2 = new PulsarSchema<>(kvBytes1, byte[].class, byte[].class);
    Schema<KeyValue<byte[], byte[]>> kvBytes3 = PulsarSchemaUtils.createSchema(kvBytes2.getSchemaInfo());
    assertNotEquals(kvBytes3.getSchemaInfo(), kvBytes1.getSchemaInfo());
}
Also used : KeyValue(org.apache.pulsar.common.schema.KeyValue) FL(org.apache.flink.connector.pulsar.testutils.SampleData.FL) Foo(org.apache.flink.connector.pulsar.testutils.SampleData.Foo) TestMessage(org.apache.flink.connector.pulsar.SampleMessage.TestMessage) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.junit.jupiter.api.Test)

Example 4 with SchemaInfo

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

the class KeyValueSchemaFactory method createSchema.

@Override
public Schema<KeyValue<K, V>> createSchema(SchemaInfo info) {
    KeyValue<SchemaInfo, SchemaInfo> kvSchemaInfo = decodeKeyValueSchemaInfo(info);
    Schema<K> keySchema = PulsarSchemaUtils.createSchema(kvSchemaInfo.getKey());
    Schema<V> valueSchema = PulsarSchemaUtils.createSchema(kvSchemaInfo.getValue());
    KeyValueEncodingType encodingType = decodeKeyValueEncodingType(info);
    Schema<KeyValue<K, V>> schema = KeyValueSchemaImpl.of(keySchema, valueSchema, encodingType);
    // Append extra class name into schema info properties.
    // KeyValueSchema don't have custom properties builder method, we have to use side effects.
    SchemaInfo schemaInfo = schema.getSchemaInfo();
    Map<String, String> properties = schemaInfo.getProperties();
    properties.put(CLASS_INFO_PLACEHOLDER, KeyValue.class.getName());
    return schema;
}
Also used : KeyValue(org.apache.pulsar.common.schema.KeyValue) KeyValueSchemaInfo.decodeKeyValueEncodingType(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo.decodeKeyValueEncodingType) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) KeyValueSchemaInfo.decodeKeyValueSchemaInfo(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo.decodeKeyValueSchemaInfo)

Aggregations

SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)4 Foo (org.apache.flink.connector.pulsar.testutils.SampleData.Foo)2 KeyValue (org.apache.pulsar.common.schema.KeyValue)2 Test (org.junit.jupiter.api.Test)2 TestMessage (org.apache.flink.connector.pulsar.SampleMessage.TestMessage)1 FL (org.apache.flink.connector.pulsar.testutils.SampleData.FL)1 Producer (org.apache.pulsar.client.api.Producer)1 KeyValueSchemaInfo.decodeKeyValueEncodingType (org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo.decodeKeyValueEncodingType)1 KeyValueSchemaInfo.decodeKeyValueSchemaInfo (org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo.decodeKeyValueSchemaInfo)1 KeyValueEncodingType (org.apache.pulsar.common.schema.KeyValueEncodingType)1