Java Code Examples for org.apache.pulsar.client.api.Schema#AUTO_CONSUME

The following examples show how to use org.apache.pulsar.client.api.Schema#AUTO_CONSUME . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TopicSchema.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static <T> Schema<T> newSchemaInstance(Class<T> clazz, SchemaType type, ConsumerConfig conf) {
    switch (type) {
    case NONE:
        return (Schema<T>) Schema.BYTES;

    case AUTO_CONSUME:
    case AUTO:
        return (Schema<T>) Schema.AUTO_CONSUME();

    case STRING:
        return (Schema<T>) Schema.STRING;

    case AVRO:
        return AvroSchema.of(SchemaDefinition.<T>builder()
                .withProperties(new HashMap<>(conf.getSchemaProperties()))
                .withPojo(clazz).build());

    case JSON:
        return JSONSchema.of(SchemaDefinition.<T>builder().withPojo(clazz).build());

    case KEY_VALUE:
        return (Schema<T>)Schema.KV_BYTES();

    case PROTOBUF:
        return ProtobufSchema.ofGenericClass(clazz, new HashMap<>());

    default:
        throw new RuntimeException("Unsupported schema type" + type);
    }
}
 
Example 2
Source File: AutoConsumeSchema.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public Schema<GenericRecord> clone() {
    Schema<GenericRecord> schema = Schema.AUTO_CONSUME();
    if (this.schema != null) {
        schema.configureSchemaInfo(topicName, componentName, this.schema.getSchemaInfo());
    } else {
        schema.configureSchemaInfo(topicName, componentName, null);
    }
    if (schemaInfoProvider != null) {
        schema.setSchemaInfoProvider(schemaInfoProvider);
    }
    return schema;
}
 
Example 3
Source File: SchemaUpdateStrategyTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void testAutoUpdateBackward(String namespace, String topicName) throws Exception {
    ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker(
            "namespaces", "get-schema-autoupdate-strategy", namespace);
    Assert.assertEquals(result.getStdout().trim(), "FULL");
    pulsarCluster.runAdminCommandOnAnyBroker("namespaces", "set-schema-autoupdate-strategy",
            "--compatibility", "BACKWARD", namespace);

    try (PulsarClient pulsarClient = PulsarClient.builder()
            .serviceUrl(pulsarCluster.getPlainTextServiceUrl()).build()) {
        V1Data v1Data = new V1Data("test1", 1);
        try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
            p.send(v1Data);
        }

        log.info("try with forward compat, should fail");
        try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
            Assert.fail("Forward compat schema should be rejected");
        } catch (PulsarClientException e) {
            Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
        }

        log.info("try with backward compat, should succeed");
        V2Data v2Data = new V2Data("test2");
        try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
            p.send(v2Data);
        }

        Schema<GenericRecord> schema = Schema.AUTO_CONSUME();
        try (Consumer<GenericRecord> consumer = pulsarClient.newConsumer(schema)
             .topic(topicName)
             .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
             .subscriptionName("sub")
             .subscribe()
        ) {
            log.info("Schema Info : {}", schema.getSchemaInfo().getSchemaDefinition());

            Message<GenericRecord> msg1 = consumer.receive();
            v1Data.assertEqualToRecord(msg1.getValue());

            Message<GenericRecord> msg2 = consumer.receive();
            v2Data.assertEqualToRecord(msg2.getValue());
        }
    }
}
 
Example 4
Source File: SchemaUpdateStrategyTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void testNone(String namespace, String topicName) throws Exception {
    ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker(
            "namespaces", "get-schema-autoupdate-strategy", namespace);
    Assert.assertEquals(result.getStdout().trim(), "FULL");
    pulsarCluster.runAdminCommandOnAnyBroker("namespaces", "set-schema-autoupdate-strategy",
            "--compatibility", "NONE", namespace);

    try (PulsarClient pulsarClient = PulsarClient.builder()
            .serviceUrl(pulsarCluster.getPlainTextServiceUrl()).build()) {
        V1Data v1Data = new V1Data("test1", 1);
        try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
            p.send(v1Data);
        }

        log.info("try with forward compat, should succeed");
        V3Data v3Data = new V3Data("test3", 1, 2);
        try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
            p.send(v3Data);
        }

        log.info("try with backward compat, should succeed");
        V2Data v2Data = new V2Data("test2");
        try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
            p.send(v2Data);
        }

        Schema<GenericRecord> schema = Schema.AUTO_CONSUME();
        try (Consumer<GenericRecord> consumer = pulsarClient.newConsumer(schema)
             .topic(topicName)
             .subscriptionName("sub")
             .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
             .subscribe()
        ) {
            log.info("Schema Info : {}", schema.getSchemaInfo().getSchemaDefinition());

            Message<GenericRecord> msg1 = consumer.receive();
            v1Data.assertEqualToRecord(msg1.getValue());

            Message<GenericRecord> msg2 = consumer.receive();
            v3Data.assertEqualToRecord(msg2.getValue());

            Message<GenericRecord> msg3 = consumer.receive();
            v2Data.assertEqualToRecord(msg3.getValue());
        }
    }
}
 
Example 5
Source File: SchemaUpdateStrategyTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void testAutoUpdateForward(String namespace, String topicName) throws Exception {
    ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker(
            "namespaces", "get-schema-autoupdate-strategy", namespace);
    Assert.assertEquals(result.getStdout().trim(), "FULL");
    pulsarCluster.runAdminCommandOnAnyBroker("namespaces", "set-schema-autoupdate-strategy",
                                             "--compatibility", "FORWARD", namespace);

    try (PulsarClient pulsarClient = PulsarClient.builder()
         .serviceUrl(pulsarCluster.getPlainTextServiceUrl()).build()) {

        V1Data v1Data = new V1Data("test1", 1);
        try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
            p.send(v1Data);
        }

        log.info("try with backward compat, should fail");
        try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
            Assert.fail("Backward compat schema should be rejected");
        } catch (PulsarClientException e) {
            Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
        }

        log.info("try with forward compat, should succeed");
        V3Data v3Data = new V3Data("test2", 1, 2);
        try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
            p.send(v3Data);
        }

        Schema<GenericRecord> schema = Schema.AUTO_CONSUME();
        try (Consumer<GenericRecord> consumer = pulsarClient.newConsumer(schema)
             .topic(topicName)
             .subscriptionName("sub")
             .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
             .subscribe()
        ) {
            log.info("Schema Info : {}", schema.getSchemaInfo().getSchemaDefinition());

            Message<GenericRecord> msg1 = consumer.receive();
            v1Data.assertEqualToRecord(msg1.getValue());

            Message<GenericRecord> msg2 = consumer.receive();
            v3Data.assertEqualToRecord(msg2.getValue());
        }
    }

}