Search in sources :

Example 1 with GenericAvroRecord

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

the class TestAvroDecoder 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);
    GenericAvroRecord genericRecord = (GenericAvroRecord) GenericAvroSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getAvroRecord().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) RowType(io.prestosql.spi.type.RowType) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) Test(org.testng.annotations.Test)

Example 2 with GenericAvroRecord

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

the class TestAvroDecoder 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);
    GenericAvroRecord genericRecord = (GenericAvroRecord) GenericAvroSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getAvroRecord().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) BigintType(io.prestosql.spi.type.BigintType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) VarcharType(io.prestosql.spi.type.VarcharType) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) Test(org.testng.annotations.Test)

Example 3 with GenericAvroRecord

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

the class TestAvroDecoder 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);
    GenericAvroRecord genericRecord = (GenericAvroRecord) GenericAvroSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getAvroRecord().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 : ArrayType(io.prestosql.spi.type.ArrayType) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) 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)

Example 4 with GenericAvroRecord

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

the class PulsarAvroRowDecoder 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) {
    GenericRecord avroRecord;
    try {
        GenericAvroRecord record = (GenericAvroRecord) genericAvroSchema.decode(byteBuf);
        avroRecord = record.getAvroRecord();
    } catch (Exception e) {
        e.printStackTrace();
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Decoding avro record failed.", e);
    }
    return Optional.of(columnDecoders.entrySet().stream().collect(toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().decodeField(avroRecord))));
}
Also used : GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) PrestoException(io.prestosql.spi.PrestoException) GenericRecord(org.apache.avro.generic.GenericRecord) PrestoException(io.prestosql.spi.PrestoException)

Example 5 with GenericAvroRecord

use of org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord in project pulsar-flink by streamnative.

the class PulsarSerializer method newStructConverter.

private Function<Object, Object> newStructConverter(FieldsDataType dataType, Schema avroStruct) throws IncompatibleSchemaException {
    if (avroStruct.getType() != Schema.Type.RECORD || avroStruct.getFields().size() != dataType.getChildren().size()) {
        throw new IncompatibleSchemaException(String.format("Cannot convert Flink type %s to Avro type %s.", dataType.toString(), avroStruct.toString(true)));
    }
    RowType rowType = (RowType) dataType.getLogicalType();
    List<RowType.RowField> fields = rowType.getFields();
    List<BiFunction<PositionedGetter, Integer, Object>> fieldConverters = new ArrayList<>();
    for (int i = 0; i < fields.size(); i++) {
        org.apache.flink.table.types.logical.LogicalType logicalType = rowType.getTypeAt(i);
        DataType dt = TypeConversions.fromLogicalToDataType(logicalType);
        Schema.Field at = avroStruct.getFields().get(i);
        fieldConverters.add(newConverter(dt, resolveNullableType(at.schema(), dt.getLogicalType().isNullable())));
    }
    int numFields = dataType.getChildren().size();
    return row -> {
        GenericSchema<GenericRecord> pSchema = SchemaUtils.avroSchema2PulsarSchema(avroStruct);
        GenericRecordBuilder builder = pSchema.newRecordBuilder();
        Row rowX = (Row) row;
        for (int i = 0; i < numFields; i++) {
            if (rowX.getField(i) == null) {
                builder.set(pSchema.getFields().get(i), null);
            } else {
                builder.set(pSchema.getFields().get(i), fieldConverters.get(i).apply(new PositionedGetter(rowX), i));
            }
        }
        return (GenericAvroRecord) builder.build();
    };
}
Also used : DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) BiFunction(java.util.function.BiFunction) LocalDateTime(java.time.LocalDateTime) Field(org.apache.pulsar.client.api.schema.Field) LogicalType(org.apache.pulsar.shade.org.apache.avro.LogicalType) RowType(org.apache.flink.table.types.logical.RowType) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DecimalType(org.apache.flink.table.types.logical.DecimalType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) LogicalTypes(org.apache.pulsar.shade.org.apache.avro.LogicalTypes) Utf8(org.apache.pulsar.shade.org.apache.avro.util.Utf8) GenericData(org.apache.pulsar.shade.org.apache.avro.generic.GenericData) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) Schema(org.apache.pulsar.shade.org.apache.avro.Schema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Collectors(java.util.stream.Collectors) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Slf4j(lombok.extern.slf4j.Slf4j) Conversions(org.apache.pulsar.shade.org.apache.avro.Conversions) List(java.util.List) CollectionDataType(org.apache.flink.table.types.CollectionDataType) LocalDate(java.time.LocalDate) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Row(org.apache.flink.types.Row) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.shade.org.apache.avro.Schema) ArrayList(java.util.ArrayList) RowType(org.apache.flink.table.types.logical.RowType) BiFunction(java.util.function.BiFunction) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Row(org.apache.flink.types.Row)

Aggregations

GenericAvroRecord (org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord)9 Test (org.testng.annotations.Test)5 ByteBuf (io.netty.buffer.ByteBuf)4 DecoderColumnHandle (io.prestosql.decoder.DecoderColumnHandle)4 FieldValueProvider (io.prestosql.decoder.FieldValueProvider)4 PulsarColumnHandle (org.apache.pulsar.sql.presto.PulsarColumnHandle)4 DecoderTestMessage (org.apache.pulsar.sql.presto.decoder.DecoderTestMessage)4 ArrayType (io.prestosql.spi.type.ArrayType)3 RowType (io.prestosql.spi.type.RowType)3 LocalDateTime (java.time.LocalDateTime)3 List (java.util.List)3 ByteBuffer (java.nio.ByteBuffer)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 BiFunction (java.util.function.BiFunction)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Slf4j (lombok.extern.slf4j.Slf4j)2