use of org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord 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.GenericJsonRecord in project pulsar by apache.
the class TestJsonDecoder method testRow.
@Test
public void testRow() {
DecoderTestMessage message = new DecoderTestMessage();
message.stringField = "message_2";
DecoderTestMessage.TestRow testRow = new DecoderTestMessage.TestRow();
message.rowField = testRow;
testRow.intField = 22;
testRow.stringField = "message_2_testRow";
DecoderTestMessage.NestedRow nestedRow = new DecoderTestMessage.NestedRow();
nestedRow.longField = 222L;
nestedRow.stringField = "message_2_nestedRow";
testRow.nestedRow = nestedRow;
byte[] bytes = schema.encode(message);
ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
GenericJsonRecord genericRecord = (GenericJsonRecord) GenericJsonSchema.of(schemaInfo).decode(bytes);
Object fieldValue = genericRecord.getJsonNode().get("rowField");
Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
RowType columnType = RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("intField", INTEGER)).add(RowType.field("nestedRow", RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("longField", BIGINT)).add(RowType.field("stringField", VARCHAR)).build()))).add(RowType.field("stringField", VARCHAR)).build());
PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "rowField", columnType, false, false, "rowField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
checkRowValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
use of org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord in project pulsar by apache.
the class PulsarJsonRowDecoder method decodeRow.
/**
* decode ByteBuf by {@link org.apache.pulsar.client.api.schema.GenericSchema}.
* @param byteBuf
* @return
*/
@Override
public Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodeRow(ByteBuf byteBuf) {
GenericJsonRecord record = (GenericJsonRecord) genericJsonSchema.decode(byteBuf);
JsonNode tree = record.getJsonNode();
Map<DecoderColumnHandle, FieldValueProvider> decodedRow = new HashMap<>();
for (Map.Entry<DecoderColumnHandle, JsonFieldDecoder> entry : fieldDecoders.entrySet()) {
DecoderColumnHandle columnHandle = entry.getKey();
JsonFieldDecoder decoder = entry.getValue();
JsonNode node = locateNode(tree, columnHandle);
decodedRow.put(columnHandle, decoder.decode(node));
}
return Optional.of(decodedRow);
}
use of org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord in project pulsar by apache.
the class TestJsonDecoder method testMap.
@Test
public void testMap() {
DecoderTestMessage message = new DecoderTestMessage();
message.mapField = new HashMap<String, Long>() {
{
put("key1", 2L);
put("key2", 22L);
}
};
byte[] bytes = schema.encode(message);
ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
GenericJsonRecord genericRecord = (GenericJsonRecord) GenericJsonSchema.of(schemaInfo).decode(bytes);
Object fieldValue = genericRecord.getJsonNode().get("mapField");
Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
Type columnType = decoderFactory.getTypeManager().getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(VarcharType.VARCHAR.getTypeSignature()), TypeSignatureParameter.typeParameter(BigintType.BIGINT.getTypeSignature())));
PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "mapField", columnType, false, false, "mapField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
checkMapValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
use of org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord in project pulsar by apache.
the class TestJsonDecoder method testArray.
@Test
public void testArray() {
DecoderTestMessage message = new DecoderTestMessage();
message.arrayField = Arrays.asList("message_1", "message_2", "message_3");
byte[] bytes = schema.encode(message);
ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
GenericJsonRecord genericRecord = (GenericJsonRecord) GenericJsonSchema.of(schemaInfo).decode(bytes);
Object fieldValue = genericRecord.getJsonNode().get("arrayField");
Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
ArrayType columnType = new ArrayType(VARCHAR);
PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "arrayField", columnType, false, false, "arrayField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
checkArrayValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Aggregations