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