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