Search in sources :

Example 1 with DecoderColumnHandle

use of com.facebook.presto.decoder.DecoderColumnHandle in project presto by prestodb.

the class KafkaRecordSetProvider method getRecordSet.

@Override
public RecordSet getRecordSet(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorSplit split, List<? extends ColumnHandle> columns) {
    KafkaSplit kafkaSplit = convertSplit(split);
    ImmutableList.Builder<DecoderColumnHandle> handleBuilder = ImmutableList.builder();
    ImmutableMap.Builder<DecoderColumnHandle, FieldDecoder<?>> keyFieldDecoderBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<DecoderColumnHandle, FieldDecoder<?>> messageFieldDecoderBuilder = ImmutableMap.builder();
    RowDecoder keyDecoder = registry.getRowDecoder(kafkaSplit.getKeyDataFormat());
    RowDecoder messageDecoder = registry.getRowDecoder(kafkaSplit.getMessageDataFormat());
    for (ColumnHandle handle : columns) {
        KafkaColumnHandle columnHandle = convertColumnHandle(handle);
        handleBuilder.add(columnHandle);
        if (!columnHandle.isInternal()) {
            if (columnHandle.isKeyDecoder()) {
                FieldDecoder<?> fieldDecoder = registry.getFieldDecoder(kafkaSplit.getKeyDataFormat(), columnHandle.getType().getJavaType(), columnHandle.getDataFormat());
                keyFieldDecoderBuilder.put(columnHandle, fieldDecoder);
            } else {
                FieldDecoder<?> fieldDecoder = registry.getFieldDecoder(kafkaSplit.getMessageDataFormat(), columnHandle.getType().getJavaType(), columnHandle.getDataFormat());
                messageFieldDecoderBuilder.put(columnHandle, fieldDecoder);
            }
        }
    }
    ImmutableList<DecoderColumnHandle> handles = handleBuilder.build();
    ImmutableMap<DecoderColumnHandle, FieldDecoder<?>> keyFieldDecoders = keyFieldDecoderBuilder.build();
    ImmutableMap<DecoderColumnHandle, FieldDecoder<?>> messageFieldDecoders = messageFieldDecoderBuilder.build();
    return new KafkaRecordSet(kafkaSplit, consumerManager, handles, keyDecoder, messageDecoder, keyFieldDecoders, messageFieldDecoders);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) KafkaHandleResolver.convertColumnHandle(com.facebook.presto.kafka.KafkaHandleResolver.convertColumnHandle) ImmutableList(com.google.common.collect.ImmutableList) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) RowDecoder(com.facebook.presto.decoder.RowDecoder) ImmutableMap(com.google.common.collect.ImmutableMap) FieldDecoder(com.facebook.presto.decoder.FieldDecoder)

Example 2 with DecoderColumnHandle

use of com.facebook.presto.decoder.DecoderColumnHandle in project presto by prestodb.

the class TestRawDecoder method testSimple.

@Test
public void testSimple() {
    ByteBuffer buf = ByteBuffer.allocate(100);
    // 0 - 7
    buf.putLong(4815162342L);
    // 8 - 11
    buf.putInt(12345678);
    // 12 - 13
    buf.putShort((short) 4567);
    // 14
    buf.put((byte) 123);
    // 15+
    buf.put("Ich bin zwei Oeltanks".getBytes(StandardCharsets.UTF_8));
    byte[] row = new byte[buf.position()];
    System.arraycopy(buf.array(), 0, row, 0, buf.position());
    RawRowDecoder rowDecoder = new RawRowDecoder();
    DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", BigintType.BIGINT, "0", "LONG", null, false, false, false);
    DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", BigintType.BIGINT, "8", "INT", null, false, false, false);
    DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", BigintType.BIGINT, "12", "SHORT", null, false, false, false);
    DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BigintType.BIGINT, "14", "BYTE", null, false, false, false);
    DecoderTestColumnHandle row5 = new DecoderTestColumnHandle("", 4, "row5", createVarcharType(10), "15", null, null, false, false, false);
    List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4, row5);
    Set<FieldValueProvider> providers = new HashSet<>();
    boolean corrupt = rowDecoder.decodeRow(row, null, providers, columns, buildMap(columns));
    assertFalse(corrupt);
    assertEquals(providers.size(), columns.size());
    checkValue(providers, row1, 4815162342L);
    checkValue(providers, row2, 12345678);
    checkValue(providers, row3, 4567);
    checkValue(providers, row4, 123);
    checkValue(providers, row5, "Ich bin zw");
}
Also used : DecoderTestColumnHandle(com.facebook.presto.decoder.DecoderTestColumnHandle) FieldValueProvider(com.facebook.presto.decoder.FieldValueProvider) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with DecoderColumnHandle

use of com.facebook.presto.decoder.DecoderColumnHandle in project presto by prestodb.

the class TestRawDecoder method testEmptyRecord.

@Test
public void testEmptyRecord() {
    RawRowDecoder rowDecoder = new RawRowDecoder();
    byte[] emptyRow = new byte[0];
    DecoderTestColumnHandle column = new DecoderTestColumnHandle("", 0, "row1", BigintType.BIGINT, null, "LONG", null, false, false, false);
    List<DecoderColumnHandle> columns = ImmutableList.of(column);
    Set<FieldValueProvider> providers = new HashSet<>();
    boolean corrupt = rowDecoder.decodeRow(emptyRow, null, providers, columns, buildMap(columns));
    assertFalse(corrupt);
    checkIsNull(providers, column);
}
Also used : DecoderTestColumnHandle(com.facebook.presto.decoder.DecoderTestColumnHandle) FieldValueProvider(com.facebook.presto.decoder.FieldValueProvider) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with DecoderColumnHandle

use of com.facebook.presto.decoder.DecoderColumnHandle in project presto by prestodb.

the class RedisRecordSetProvider method getRecordSet.

@Override
public RecordSet getRecordSet(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorSplit split, List<? extends ColumnHandle> columns) {
    RedisSplit redisSplit = convertSplit(split);
    ImmutableList.Builder<DecoderColumnHandle> handleBuilder = ImmutableList.builder();
    ImmutableMap.Builder<DecoderColumnHandle, FieldDecoder<?>> keyFieldDecoderBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<DecoderColumnHandle, FieldDecoder<?>> valueFieldDecoderBuilder = ImmutableMap.builder();
    RowDecoder keyDecoder = registry.getRowDecoder(redisSplit.getKeyDataFormat());
    RowDecoder valueDecoder = registry.getRowDecoder(redisSplit.getValueDataFormat());
    for (ColumnHandle handle : columns) {
        RedisColumnHandle columnHandle = convertColumnHandle(handle);
        handleBuilder.add(columnHandle);
        if (!columnHandle.isInternal()) {
            if (columnHandle.isKeyDecoder()) {
                FieldDecoder<?> fieldDecoder = registry.getFieldDecoder(redisSplit.getKeyDataFormat(), columnHandle.getType().getJavaType(), columnHandle.getDataFormat());
                keyFieldDecoderBuilder.put(columnHandle, fieldDecoder);
            } else {
                FieldDecoder<?> fieldDecoder = registry.getFieldDecoder(redisSplit.getValueDataFormat(), columnHandle.getType().getJavaType(), columnHandle.getDataFormat());
                valueFieldDecoderBuilder.put(columnHandle, fieldDecoder);
            }
        }
    }
    ImmutableList<DecoderColumnHandle> handles = handleBuilder.build();
    ImmutableMap<DecoderColumnHandle, FieldDecoder<?>> keyFieldDecoders = keyFieldDecoderBuilder.build();
    ImmutableMap<DecoderColumnHandle, FieldDecoder<?>> valueFieldDecoders = valueFieldDecoderBuilder.build();
    return new RedisRecordSet(redisSplit, jedisManager, handles, keyDecoder, valueDecoder, keyFieldDecoders, valueFieldDecoders);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) RedisHandleResolver.convertColumnHandle(com.facebook.presto.redis.RedisHandleResolver.convertColumnHandle) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) ImmutableList(com.google.common.collect.ImmutableList) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) RowDecoder(com.facebook.presto.decoder.RowDecoder) ImmutableMap(com.google.common.collect.ImmutableMap) FieldDecoder(com.facebook.presto.decoder.FieldDecoder)

Example 5 with DecoderColumnHandle

use of com.facebook.presto.decoder.DecoderColumnHandle in project presto by prestodb.

the class HashRedisRowDecoder method decodeRow.

@Override
public boolean decodeRow(byte[] data, Map<String, String> dataMap, Set<FieldValueProvider> fieldValueProviders, List<DecoderColumnHandle> columnHandles, Map<DecoderColumnHandle, FieldDecoder<?>> fieldDecoders) {
    if (dataMap == null) {
        return false;
    }
    for (DecoderColumnHandle columnHandle : columnHandles) {
        if (columnHandle.isInternal()) {
            continue;
        }
        String mapping = columnHandle.getMapping();
        checkState(mapping != null, "No mapping for column handle %s!", columnHandle);
        String valueField = dataMap.get(mapping);
        @SuppressWarnings("unchecked") FieldDecoder<String> decoder = (FieldDecoder<String>) fieldDecoders.get(columnHandle);
        if (decoder != null) {
            fieldValueProviders.add(decoder.decode(valueField, columnHandle));
        }
    }
    return false;
}
Also used : DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) FieldDecoder(com.facebook.presto.decoder.FieldDecoder)

Aggregations

DecoderColumnHandle (com.facebook.presto.decoder.DecoderColumnHandle)24 DecoderTestColumnHandle (com.facebook.presto.decoder.DecoderTestColumnHandle)19 FieldValueProvider (com.facebook.presto.decoder.FieldValueProvider)19 HashSet (java.util.HashSet)19 Test (org.testng.annotations.Test)19 FieldDecoder (com.facebook.presto.decoder.FieldDecoder)5 ByteBuffer (java.nio.ByteBuffer)3 RowDecoder (com.facebook.presto.decoder.RowDecoder)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 KafkaHandleResolver.convertColumnHandle (com.facebook.presto.kafka.KafkaHandleResolver.convertColumnHandle)1 RedisHandleResolver.convertColumnHandle (com.facebook.presto.redis.RedisHandleResolver.convertColumnHandle)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1