use of org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl in project pulsar by apache.
the class JSONSchemaTest method testJsonGenericRecordBuilder.
@Test
public void testJsonGenericRecordBuilder() {
JSONSchema<Seller> sellerJsonSchema = JSONSchema.of(Seller.class);
RecordSchemaBuilder sellerSchemaBuilder = SchemaBuilder.record("seller");
sellerSchemaBuilder.field("state").type(SchemaType.STRING);
sellerSchemaBuilder.field("street").type(SchemaType.STRING);
sellerSchemaBuilder.field("zipCode").type(SchemaType.INT64);
SchemaInfo sellerSchemaInfo = sellerSchemaBuilder.build(SchemaType.JSON);
GenericSchemaImpl sellerGenericSchema = GenericSchemaImpl.of(sellerSchemaInfo);
JSONSchema<PC> pcJsonSchema = JSONSchema.of(PC.class);
RecordSchemaBuilder pcSchemaBuilder = SchemaBuilder.record("pc");
pcSchemaBuilder.field("brand").type(SchemaType.STRING);
pcSchemaBuilder.field("model").type(SchemaType.STRING);
pcSchemaBuilder.field("gpu").type(SchemaType.STRING);
pcSchemaBuilder.field("year").type(SchemaType.INT64);
pcSchemaBuilder.field("seller", sellerGenericSchema).type(SchemaType.JSON).optional();
SchemaInfo pcGenericSchemaInfo = pcSchemaBuilder.build(SchemaType.JSON);
GenericSchemaImpl pcGenericSchema = GenericSchemaImpl.of(pcGenericSchemaInfo);
Seller seller = new Seller("USA", "oakstreet", 9999);
PC pc = new PC("dell", "g3", 2020, GPU.AMD, seller);
byte[] bytes = pcJsonSchema.encode(pc);
Assert.assertTrue(bytes.length > 0);
Object pc2 = pcJsonSchema.decode(bytes);
assertEquals(pc, pc2);
GenericRecord sellerRecord = sellerGenericSchema.newRecordBuilder().set("state", "USA").set("street", "oakstreet").set("zipCode", 9999).build();
GenericRecord pcRecord = pcGenericSchema.newRecordBuilder().set("brand", "dell").set("model", "g3").set("year", 2020).set("gpu", GPU.AMD).set("seller", sellerRecord).build();
byte[] bytes3 = pcGenericSchema.encode(pcRecord);
Assert.assertTrue(bytes3.length > 0);
GenericRecord pc3Record = pcGenericSchema.decode(bytes3);
for (Field field : pc3Record.getFields()) {
assertTrue(pcGenericSchema.getFields().contains(field));
}
assertEquals("dell", pc3Record.getField("brand"));
assertEquals("g3", pc3Record.getField("model"));
assertEquals(2020, pc3Record.getField("year"));
assertEquals(GPU.AMD.toString(), pc3Record.getField("gpu"));
GenericRecord seller3Record = (GenericRecord) pc3Record.getField("seller");
assertEquals("USA", seller3Record.getField("state"));
assertEquals("oakstreet", seller3Record.getField("street"));
assertEquals(9999, seller3Record.getField("zipCode"));
assertTrue(pc3Record instanceof GenericJsonRecord);
Assertions.assertThatCode(() -> pc3Record.getField("I_DO_NOT_EXIST")).doesNotThrowAnyException();
}
use of org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl in project pulsar by apache.
the class TopicsTest method testProduceWithAvroSchema.
@Test
public void testProduceWithAvroSchema() throws Exception {
String topicName = "persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName;
admin.topics().createNonPartitionedTopic(topicName);
AsyncResponse asyncResponse = mock(AsyncResponse.class);
GenericSchemaImpl avroSchema = GenericAvroSchema.of(AvroSchema.of(SchemaDefinition.builder().withPojo(PC.class).build()).getSchemaInfo());
PC pc = new PC("dell", "alienware", 2021, GPU.AMD, new Seller("WA", "main street", 98004));
PC anotherPc = new PC("asus", "rog", 2020, GPU.NVIDIA, new Seller("CA", "back street", 90232));
Consumer consumer = pulsarClient.newConsumer(avroSchema).topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(avroSchema.getSchemaInfo()));
ReflectDatumWriter<PC> datumWriter = new ReflectDatumWriter(avroSchema.getAvroSchema());
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
JsonEncoder encoder1 = EncoderFactory.get().jsonEncoder(avroSchema.getAvroSchema(), outputStream1);
JsonEncoder encoder2 = EncoderFactory.get().jsonEncoder(avroSchema.getAvroSchema(), outputStream2);
datumWriter.write(pc, encoder1);
encoder1.flush();
datumWriter.write(anotherPc, encoder2);
encoder2.flush();
String message = "[" + "{\"key\":\"my-key\",\"payload\":\"" + outputStream1.toString().replace("\"", "\\\"") + "\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"" + outputStream2.toString().replace("\"", "\\\"") + "\",\"eventTime\":1603045262772,\"sequenceId\":2}]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
}));
topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
Object responseEntity = responseCaptor.getValue().getEntity();
Assert.assertTrue(responseEntity instanceof ProducerAcks);
ProducerAcks response = (ProducerAcks) responseEntity;
Assert.assertEquals(response.getMessagePublishResults().size(), 2);
Assert.assertEquals(response.getSchemaVersion(), 0);
for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), -1);
Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
}
List<PC> expected = Arrays.asList(pc, anotherPc);
Message<String> msg = null;
// Assert all messages published by REST producer can be received by consumer in expected order.
for (int i = 0; i < 2; i++) {
msg = consumer.receive(2, TimeUnit.SECONDS);
GenericAvroRecord avroRecord = (GenericAvroRecord) avroSchema.decode(msg.getData());
Assert.assertEquals(((Utf8) avroRecord.getAvroRecord().get("brand")).toString(), expected.get(i).brand);
Assert.assertEquals(((Utf8) avroRecord.getAvroRecord().get("model")).toString(), expected.get(i).model);
Assert.assertEquals((int) avroRecord.getAvroRecord().get("year"), expected.get(i).year);
Assert.assertEquals(((GenericData.EnumSymbol) avroRecord.getAvroRecord().get("gpu")).toString(), expected.get(i).gpu.toString());
Assert.assertEquals(((Utf8) ((GenericRecord) avroRecord.getAvroRecord().get("seller")).get("state")).toString(), expected.get(i).seller.state);
Assert.assertEquals(((Utf8) ((GenericRecord) avroRecord.getAvroRecord().get("seller")).get("street")).toString(), expected.get(i).seller.street);
Assert.assertEquals(((GenericRecord) avroRecord.getAvroRecord().get("seller")).get("zipCode"), expected.get(i).seller.zipCode);
Assert.assertEquals("my-key", msg.getKey());
}
}
Aggregations