Search in sources :

Example 1 with StringSchema

use of org.apache.pulsar.client.impl.schema.StringSchema in project incubator-pulsar by apache.

the class KeyValueSchemaCompatibilityCheckTest method testCheckSchemaTypeOtherCompatibility.

@Test
public void testCheckSchemaTypeOtherCompatibility() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());
    StringSchema stringSchema = new StringSchema();
    SchemaData fromSchemaData = SchemaData.builder().type(SchemaType.STRING).data(stringSchema.getSchemaInfo().getSchema()).build();
    SchemaData toSchemaData = SchemaData.builder().type(SchemaType.KEY_VALUE).data(KeyValueSchemaImpl.of(fooSchema, barSchema).getSchemaInfo().getSchema()).build();
    Assert.assertFalse(checkers.get(SchemaType.KEY_VALUE).isCompatible(fromSchemaData, toSchemaData, SchemaCompatibilityStrategy.ALWAYS_INCOMPATIBLE));
}
Also used : SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) Test(org.testng.annotations.Test)

Example 2 with StringSchema

use of org.apache.pulsar.client.impl.schema.StringSchema in project incubator-pulsar by apache.

the class KeyValueSchemaCompatibilityCheckTest method testCheckSchemaTypeAlwaysCompatibility.

@Test
public void testCheckSchemaTypeAlwaysCompatibility() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());
    StringSchema stringSchema = new StringSchema();
    SchemaData fromSchemaData = SchemaData.builder().type(SchemaType.STRING).data(stringSchema.getSchemaInfo().getSchema()).build();
    SchemaData toSchemaData = SchemaData.builder().type(SchemaType.KEY_VALUE).data(KeyValueSchemaImpl.of(fooSchema, barSchema).getSchemaInfo().getSchema()).build();
    Assert.assertTrue(checkers.get(SchemaType.KEY_VALUE).isCompatible(fromSchemaData, toSchemaData, SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE));
}
Also used : SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) Test(org.testng.annotations.Test)

Example 3 with StringSchema

use of org.apache.pulsar.client.impl.schema.StringSchema in project pulsar by apache.

the class KeyValueSchemaCompatibilityCheckTest method testCheckSchemaTypeAlwaysCompatibility.

@Test
public void testCheckSchemaTypeAlwaysCompatibility() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());
    StringSchema stringSchema = new StringSchema();
    SchemaData fromSchemaData = SchemaData.builder().type(SchemaType.STRING).data(stringSchema.getSchemaInfo().getSchema()).build();
    SchemaData toSchemaData = SchemaData.builder().type(SchemaType.KEY_VALUE).data(KeyValueSchemaImpl.of(fooSchema, barSchema).getSchemaInfo().getSchema()).build();
    Assert.assertTrue(checkers.get(SchemaType.KEY_VALUE).isCompatible(fromSchemaData, toSchemaData, SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE));
}
Also used : SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) Test(org.testng.annotations.Test)

Example 4 with StringSchema

use of org.apache.pulsar.client.impl.schema.StringSchema in project pulsar by apache.

the class KeyValueSchemaCompatibilityCheckTest method testCheckSchemaTypeOtherCompatibility.

@Test
public void testCheckSchemaTypeOtherCompatibility() {
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build());
    StringSchema stringSchema = new StringSchema();
    SchemaData fromSchemaData = SchemaData.builder().type(SchemaType.STRING).data(stringSchema.getSchemaInfo().getSchema()).build();
    SchemaData toSchemaData = SchemaData.builder().type(SchemaType.KEY_VALUE).data(KeyValueSchemaImpl.of(fooSchema, barSchema).getSchemaInfo().getSchema()).build();
    Assert.assertFalse(checkers.get(SchemaType.KEY_VALUE).isCompatible(fromSchemaData, toSchemaData, SchemaCompatibilityStrategy.ALWAYS_INCOMPATIBLE));
}
Also used : SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) Test(org.testng.annotations.Test)

Example 5 with StringSchema

use of org.apache.pulsar.client.impl.schema.StringSchema in project pulsar-adapters by apache.

the class KafkaApiTest method testConsumerAvroSchemaWithPulsarKafkaClient.

@Test
public void testConsumerAvroSchemaWithPulsarKafkaClient() throws Exception {
    String topic = "testConsumerAvroSchemaWithPulsarKafkaClient";
    StringSchema stringSchema = new StringSchema();
    AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
    Properties props = new Properties();
    props.put("bootstrap.servers", getPlainTextServiceUrl());
    props.put("group.id", "my-subscription-name");
    props.put("enable.auto.commit", "false");
    props.put("key.deserializer", StringDeserializer.class.getName());
    props.put("value.deserializer", StringDeserializer.class.getName());
    @Cleanup Consumer<String, Foo> consumer = new KafkaConsumer<String, Foo>(props, new StringSchema(), fooSchema);
    consumer.subscribe(Arrays.asList(topic));
    @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(getPlainTextServiceUrl()).build();
    org.apache.pulsar.client.api.Producer<Foo> pulsarProducer = pulsarClient.newProducer(fooSchema).topic(topic).create();
    for (int i = 0; i < 10; i++) {
        Foo foo = new Foo();
        foo.setField1("field1");
        foo.setField2("field2");
        foo.setField3(i);
        pulsarProducer.newMessage().keyBytes(stringSchema.encode(Integer.toString(i))).value(foo).send();
    }
    AtomicInteger received = new AtomicInteger();
    while (received.get() < 10) {
        ConsumerRecords<String, Foo> records = consumer.poll(100);
        if (!records.isEmpty()) {
            records.forEach(record -> {
                Assert.assertEquals(record.key(), Integer.toString(received.get()));
                Foo value = record.value();
                Assert.assertEquals(value.getField1(), "field1");
                Assert.assertEquals(value.getField2(), "field2");
                Assert.assertEquals(value.getField3(), received.get());
                received.incrementAndGet();
            });
            consumer.commitSync();
        }
    }
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ToString(lombok.ToString) Properties(java.util.Properties) Cleanup(lombok.Cleanup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

StringSchema (org.apache.pulsar.client.impl.schema.StringSchema)7 Test (org.testng.annotations.Test)7 SchemaData (org.apache.pulsar.common.protocol.schema.SchemaData)6 Properties (java.util.Properties)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Cleanup (lombok.Cleanup)1 ToString (lombok.ToString)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 PulsarClient (org.apache.pulsar.client.api.PulsarClient)1