Search in sources :

Example 1 with GenericJsonRecord

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();
}
Also used : GenericSchemaImpl(org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl) Field(org.apache.pulsar.client.api.schema.Field) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.testng.annotations.Test)

Example 2 with GenericJsonRecord

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);
}
Also used : FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) Test(org.testng.annotations.Test)

Example 3 with GenericJsonRecord

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);
}
Also used : HashMap(java.util.HashMap) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonFieldDecoder(io.prestosql.decoder.json.JsonFieldDecoder) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) HashMap(java.util.HashMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Map(java.util.Map)

Example 4 with GenericJsonRecord

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);
}
Also used : FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) Test(org.testng.annotations.Test)

Example 5 with GenericJsonRecord

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);
}
Also used : FieldValueProvider(io.prestosql.decoder.FieldValueProvider) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Aggregations

GenericJsonRecord (org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord)7 DecoderColumnHandle (io.prestosql.decoder.DecoderColumnHandle)5 FieldValueProvider (io.prestosql.decoder.FieldValueProvider)5 Test (org.testng.annotations.Test)5 ByteBuf (io.netty.buffer.ByteBuf)4 PulsarColumnHandle (org.apache.pulsar.sql.presto.PulsarColumnHandle)4 DecoderTestMessage (org.apache.pulsar.sql.presto.decoder.DecoderTestMessage)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 JsonFieldDecoder (io.prestosql.decoder.json.JsonFieldDecoder)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)1