Search in sources :

Example 1 with GenericProtobufNativeRecord

use of org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord in project pulsar by apache.

the class TestProtobufNativeDecoder method testMap.

@Test
public void testMap() {
    TestMsg.TestMessage testMessage = TestMsg.TestMessage.newBuilder().putMapField("key_a", 1.1d).putMapField("key_b", 2.2d).build();
    byte[] bytes = schema.encode(testMessage);
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
    GenericProtobufNativeRecord genericRecord = (GenericProtobufNativeRecord) GenericProtobufNativeSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getProtobufRecord().getField(genericRecord.getProtobufRecord().getDescriptorForType().findFieldByName("mapField"));
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    Type columnType = decoderFactory.getTypeManager().getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(VARCHAR.getTypeSignature()), TypeSignatureParameter.typeParameter(DOUBLE.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 : RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) GenericProtobufNativeRecord(org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Example 2 with GenericProtobufNativeRecord

use of org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord in project pulsar by apache.

the class PulsarProtobufNativeRowDecoder 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) {
    DynamicMessage dynamicMessage;
    try {
        GenericProtobufNativeRecord record = (GenericProtobufNativeRecord) genericProtobufNativeSchema.decode(byteBuf);
        dynamicMessage = record.getProtobufRecord();
    } catch (Exception e) {
        log.error(e);
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Decoding protobuf record failed.", e);
    }
    return Optional.of(columnDecoders.entrySet().stream().collect(toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().decodeField(dynamicMessage))));
}
Also used : PrestoException(io.prestosql.spi.PrestoException) DynamicMessage(com.google.protobuf.DynamicMessage) GenericProtobufNativeRecord(org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord) PrestoException(io.prestosql.spi.PrestoException)

Example 3 with GenericProtobufNativeRecord

use of org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord in project pulsar by apache.

the class TestProtobufNativeDecoder method testRow.

@Test
public void testRow() {
    TestMsg.SubMessage.NestedMessage nestedMessage = TestMsg.SubMessage.NestedMessage.newBuilder().setTitle("nestedMessage_title").addUrls("aa").addUrls("bb").build();
    TestMsg.SubMessage subMessage = TestMsg.SubMessage.newBuilder().setBar(0.2).setFoo("fooValue").setBar(3.9d).setNestedMessage(nestedMessage).build();
    TestMsg.TestMessage testMessage = TestMsg.TestMessage.newBuilder().setSubMessage(subMessage).build();
    byte[] bytes = schema.encode(testMessage);
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
    GenericProtobufNativeRecord genericRecord = (GenericProtobufNativeRecord) GenericProtobufNativeSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getProtobufRecord().getField(genericRecord.getProtobufRecord().getDescriptorForType().findFieldByName("subMessage"));
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    RowType columnType = RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("foo", VARCHAR)).add(RowType.field("bar", DOUBLE)).add(RowType.field("nestedMessage", RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("title", VARCHAR)).add(RowType.field("urls", new ArrayType(VARCHAR))).build()))).build());
    PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "subMessage", columnType, false, false, "subMessage", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkRowValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Also used : FieldValueProvider(io.prestosql.decoder.FieldValueProvider) RowType(io.prestosql.spi.type.RowType) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) ArrayType(io.prestosql.spi.type.ArrayType) GenericProtobufNativeRecord(org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord) Test(org.testng.annotations.Test)

Example 4 with GenericProtobufNativeRecord

use of org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord in project pulsar by apache.

the class TestProtobufNativeDecoder method testArray.

@Test
public void testArray() {
    TestMsg.TestMessage testMessage = TestMsg.TestMessage.newBuilder().addRepeatedField("first").addRepeatedField("second").build();
    byte[] bytes = schema.encode(testMessage);
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
    GenericProtobufNativeRecord genericRecord = (GenericProtobufNativeRecord) GenericProtobufNativeSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getProtobufRecord().getField(genericRecord.getProtobufRecord().getDescriptorForType().findFieldByName("repeatedField"));
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    ArrayType columnType = new ArrayType(VARCHAR);
    PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "repeatedField", columnType, false, false, "repeatedField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkArrayValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) GenericProtobufNativeRecord(org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Aggregations

GenericProtobufNativeRecord (org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord)4 ByteBuf (io.netty.buffer.ByteBuf)3 DecoderColumnHandle (io.prestosql.decoder.DecoderColumnHandle)3 FieldValueProvider (io.prestosql.decoder.FieldValueProvider)3 ArrayType (io.prestosql.spi.type.ArrayType)3 PulsarColumnHandle (org.apache.pulsar.sql.presto.PulsarColumnHandle)3 Test (org.testng.annotations.Test)3 RowType (io.prestosql.spi.type.RowType)2 DynamicMessage (com.google.protobuf.DynamicMessage)1 PrestoException (io.prestosql.spi.PrestoException)1 Type (io.prestosql.spi.type.Type)1