Search in sources :

Example 1 with AvroReader

use of org.apache.pulsar.client.impl.schema.reader.AvroReader in project pulsar by yahoo.

the class SimpleSchemaTest method newNativeAvroProducerForMessageSchemaWithBatch.

@Test
public void newNativeAvroProducerForMessageSchemaWithBatch() throws Exception {
    String topic = "my-property/my-ns/schema-test";
    Schema<V1Data> v1Schema = Schema.AVRO(V1Data.class);
    byte[] v1SchemaBytes = v1Schema.getSchemaInfo().getSchema();
    org.apache.avro.Schema v1SchemaAvroNative = new Parser().parse(new ByteArrayInputStream(v1SchemaBytes));
    AvroWriter<V1Data> v1Writer = new AvroWriter<>(v1SchemaAvroNative);
    Schema<V2Data> v2Schema = Schema.AVRO(V2Data.class);
    byte[] v2SchemaBytes = v2Schema.getSchemaInfo().getSchema();
    org.apache.avro.Schema v2SchemaAvroNative = new Parser().parse(new ByteArrayInputStream(v2SchemaBytes));
    AvroWriter<V2Data> v2Writer = new AvroWriter<>(v2SchemaAvroNative);
    Consumer<byte[]> c = pulsarClient.newConsumer(Schema.BYTES).topic(topic).subscriptionName("sub1").subscribe();
    Producer<byte[]> p = pulsarClient.newProducer(Schema.NATIVE_AVRO(v1SchemaAvroNative)).topic(topic).enableBatching(true).batchingMaxPublishDelay(10, TimeUnit.SECONDS).create();
    AvroWriter<V1Data> v1DataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(V1Data.class));
    AvroWriter<V2Data> v2DataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(V2Data.class));
    AvroWriter<IncompatibleData> incompatibleDataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(IncompatibleData.class));
    int total = 20;
    int batch = 5;
    int incompatible = 3;
    for (int i = 0; i < total; ++i) {
        if (i / batch % 2 == 0) {
            byte[] content = v1DataAvroWriter.write(new V1Data(i));
            p.newMessage(Schema.NATIVE_AVRO(v1SchemaAvroNative)).value(content).sendAsync();
        } else {
            byte[] content = v2DataAvroWriter.write(new V2Data(i, i + total));
            p.newMessage(Schema.NATIVE_AVRO(v2SchemaAvroNative)).value(content).sendAsync();
        }
        if ((i + 1) % incompatible == 0) {
            Schema<IncompatibleData> incompatibleSchema = Schema.AVRO(IncompatibleData.class);
            byte[] incompatibleSchemaBytes = incompatibleSchema.getSchemaInfo().getSchema();
            org.apache.avro.Schema incompatibleSchemaAvroNative = new Parser().parse(new ByteArrayInputStream(incompatibleSchemaBytes));
            byte[] content = incompatibleDataAvroWriter.write(new IncompatibleData(-i, -i));
            try {
                p.newMessage(Schema.NATIVE_AVRO(incompatibleSchemaAvroNative)).value(content).send();
            } catch (Exception e) {
                Assert.assertTrue(e instanceof IncompatibleSchemaException, e.getMessage());
            }
        }
    }
    p.flush();
    for (int i = 0; i < total; ++i) {
        byte[] raw = c.receive().getData();
        if (i / batch % 2 == 0) {
            AvroReader<V1Data> reader = new AvroReader<>(v1SchemaAvroNative);
            V1Data value = reader.read(raw);
            Assert.assertEquals(value.i, i);
        } else {
            AvroReader<V2Data> reader = new AvroReader<>(v2SchemaAvroNative);
            V2Data value = reader.read(raw);
            Assert.assertEquals(value, new V2Data(i, i + total));
        }
    }
    c.close();
}
Also used : AvroReader(org.apache.pulsar.client.impl.schema.reader.AvroReader) AvroWriter(org.apache.pulsar.client.impl.schema.writer.AvroWriter) InvalidMessageException(org.apache.pulsar.client.api.PulsarClientException.InvalidMessageException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) EOFException(java.io.EOFException) ExecutionException(java.util.concurrent.ExecutionException) IncompatibleSchemaException(org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException) Parser(org.apache.avro.Schema.Parser) IncompatibleSchemaException(org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test)

Example 2 with AvroReader

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

the class SimpleSchemaTest method newNativeAvroProducerForMessageSchemaWithBatch.

@Test
public void newNativeAvroProducerForMessageSchemaWithBatch() throws Exception {
    String topic = "my-property/my-ns/schema-test";
    Schema<V1Data> v1Schema = Schema.AVRO(V1Data.class);
    byte[] v1SchemaBytes = v1Schema.getSchemaInfo().getSchema();
    org.apache.avro.Schema v1SchemaAvroNative = new Parser().parse(new ByteArrayInputStream(v1SchemaBytes));
    AvroWriter<V1Data> v1Writer = new AvroWriter<>(v1SchemaAvroNative);
    Schema<V2Data> v2Schema = Schema.AVRO(V2Data.class);
    byte[] v2SchemaBytes = v2Schema.getSchemaInfo().getSchema();
    org.apache.avro.Schema v2SchemaAvroNative = new Parser().parse(new ByteArrayInputStream(v2SchemaBytes));
    AvroWriter<V2Data> v2Writer = new AvroWriter<>(v2SchemaAvroNative);
    Consumer<byte[]> c = pulsarClient.newConsumer(Schema.BYTES).topic(topic).subscriptionName("sub1").subscribe();
    Producer<byte[]> p = pulsarClient.newProducer(Schema.NATIVE_AVRO(v1SchemaAvroNative)).topic(topic).enableBatching(true).batchingMaxPublishDelay(10, TimeUnit.SECONDS).create();
    AvroWriter<V1Data> v1DataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(V1Data.class));
    AvroWriter<V2Data> v2DataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(V2Data.class));
    AvroWriter<IncompatibleData> incompatibleDataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(IncompatibleData.class));
    int total = 20;
    int batch = 5;
    int incompatible = 3;
    for (int i = 0; i < total; ++i) {
        if (i / batch % 2 == 0) {
            byte[] content = v1DataAvroWriter.write(new V1Data(i));
            p.newMessage(Schema.NATIVE_AVRO(v1SchemaAvroNative)).value(content).sendAsync();
        } else {
            byte[] content = v2DataAvroWriter.write(new V2Data(i, i + total));
            p.newMessage(Schema.NATIVE_AVRO(v2SchemaAvroNative)).value(content).sendAsync();
        }
        if ((i + 1) % incompatible == 0) {
            Schema<IncompatibleData> incompatibleSchema = Schema.AVRO(IncompatibleData.class);
            byte[] incompatibleSchemaBytes = incompatibleSchema.getSchemaInfo().getSchema();
            org.apache.avro.Schema incompatibleSchemaAvroNative = new Parser().parse(new ByteArrayInputStream(incompatibleSchemaBytes));
            byte[] content = incompatibleDataAvroWriter.write(new IncompatibleData(-i, -i));
            try {
                p.newMessage(Schema.NATIVE_AVRO(incompatibleSchemaAvroNative)).value(content).send();
            } catch (Exception e) {
                Assert.assertTrue(e instanceof IncompatibleSchemaException, e.getMessage());
            }
        }
    }
    p.flush();
    for (int i = 0; i < total; ++i) {
        byte[] raw = c.receive().getData();
        if (i / batch % 2 == 0) {
            AvroReader<V1Data> reader = new AvroReader<>(v1SchemaAvroNative);
            V1Data value = reader.read(raw);
            Assert.assertEquals(value.i, i);
        } else {
            AvroReader<V2Data> reader = new AvroReader<>(v2SchemaAvroNative);
            V2Data value = reader.read(raw);
            Assert.assertEquals(value, new V2Data(i, i + total));
        }
    }
    c.close();
}
Also used : AvroReader(org.apache.pulsar.client.impl.schema.reader.AvroReader) AvroWriter(org.apache.pulsar.client.impl.schema.writer.AvroWriter) InvalidMessageException(org.apache.pulsar.client.api.PulsarClientException.InvalidMessageException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) EOFException(java.io.EOFException) ExecutionException(java.util.concurrent.ExecutionException) IncompatibleSchemaException(org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException) Parser(org.apache.avro.Schema.Parser) IncompatibleSchemaException(org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test)

Example 3 with AvroReader

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

the class SimpleSchemaTest method newNativeAvroProducerForMessageSchemaWithBatch.

@Test
public void newNativeAvroProducerForMessageSchemaWithBatch() throws Exception {
    String topic = "my-property/my-ns/schema-test";
    Schema<V1Data> v1Schema = Schema.AVRO(V1Data.class);
    byte[] v1SchemaBytes = v1Schema.getSchemaInfo().getSchema();
    org.apache.avro.Schema v1SchemaAvroNative = new Parser().parse(new ByteArrayInputStream(v1SchemaBytes));
    AvroWriter<V1Data> v1Writer = new AvroWriter<>(v1SchemaAvroNative);
    Schema<V2Data> v2Schema = Schema.AVRO(V2Data.class);
    byte[] v2SchemaBytes = v2Schema.getSchemaInfo().getSchema();
    org.apache.avro.Schema v2SchemaAvroNative = new Parser().parse(new ByteArrayInputStream(v2SchemaBytes));
    AvroWriter<V2Data> v2Writer = new AvroWriter<>(v2SchemaAvroNative);
    Consumer<byte[]> c = pulsarClient.newConsumer(Schema.BYTES).topic(topic).subscriptionName("sub1").subscribe();
    Producer<byte[]> p = pulsarClient.newProducer(Schema.NATIVE_AVRO(v1SchemaAvroNative)).topic(topic).enableBatching(true).batchingMaxPublishDelay(10, TimeUnit.SECONDS).create();
    AvroWriter<V1Data> v1DataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(V1Data.class));
    AvroWriter<V2Data> v2DataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(V2Data.class));
    AvroWriter<IncompatibleData> incompatibleDataAvroWriter = new AvroWriter<>(ReflectData.AllowNull.get().getSchema(IncompatibleData.class));
    int total = 20;
    int batch = 5;
    int incompatible = 3;
    for (int i = 0; i < total; ++i) {
        if (i / batch % 2 == 0) {
            byte[] content = v1DataAvroWriter.write(new V1Data(i));
            p.newMessage(Schema.NATIVE_AVRO(v1SchemaAvroNative)).value(content).sendAsync();
        } else {
            byte[] content = v2DataAvroWriter.write(new V2Data(i, i + total));
            p.newMessage(Schema.NATIVE_AVRO(v2SchemaAvroNative)).value(content).sendAsync();
        }
        if ((i + 1) % incompatible == 0) {
            Schema<IncompatibleData> incompatibleSchema = Schema.AVRO(IncompatibleData.class);
            byte[] incompatibleSchemaBytes = incompatibleSchema.getSchemaInfo().getSchema();
            org.apache.avro.Schema incompatibleSchemaAvroNative = new Parser().parse(new ByteArrayInputStream(incompatibleSchemaBytes));
            byte[] content = incompatibleDataAvroWriter.write(new IncompatibleData(-i, -i));
            try {
                p.newMessage(Schema.NATIVE_AVRO(incompatibleSchemaAvroNative)).value(content).send();
            } catch (Exception e) {
                Assert.assertTrue(e instanceof IncompatibleSchemaException, e.getMessage());
            }
        }
    }
    p.flush();
    for (int i = 0; i < total; ++i) {
        byte[] raw = c.receive().getData();
        if (i / batch % 2 == 0) {
            AvroReader<V1Data> reader = new AvroReader<>(v1SchemaAvroNative);
            V1Data value = reader.read(raw);
            Assert.assertEquals(value.i, i);
        } else {
            AvroReader<V2Data> reader = new AvroReader<>(v2SchemaAvroNative);
            V2Data value = reader.read(raw);
            Assert.assertEquals(value, new V2Data(i, i + total));
        }
    }
    c.close();
}
Also used : AvroReader(org.apache.pulsar.client.impl.schema.reader.AvroReader) AvroWriter(org.apache.pulsar.client.impl.schema.writer.AvroWriter) InvalidMessageException(org.apache.pulsar.client.api.PulsarClientException.InvalidMessageException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) IncompatibleSchemaException(org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException) Parser(org.apache.avro.Schema.Parser) IncompatibleSchemaException(org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 ExecutionException (java.util.concurrent.ExecutionException)3 Parser (org.apache.avro.Schema.Parser)3 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)3 IncompatibleSchemaException (org.apache.pulsar.client.api.PulsarClientException.IncompatibleSchemaException)3 InvalidMessageException (org.apache.pulsar.client.api.PulsarClientException.InvalidMessageException)3 AvroReader (org.apache.pulsar.client.impl.schema.reader.AvroReader)3 AvroWriter (org.apache.pulsar.client.impl.schema.writer.AvroWriter)3 Test (org.testng.annotations.Test)3 EOFException (java.io.EOFException)2