Search in sources :

Example 6 with DecoderColumnHandle

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

the class CsvRowDecoder method decodeRow.

@Override
public boolean decodeRow(byte[] data, Map<String, String> dataMap, Set<FieldValueProvider> fieldValueProviders, List<DecoderColumnHandle> columnHandles, Map<DecoderColumnHandle, FieldDecoder<?>> fieldDecoders) {
    String[] fields;
    try {
        // TODO - There is no reason why the row can't have a formatHint and it could be used
        // to set the charset here.
        String line = new String(data, StandardCharsets.UTF_8);
        fields = parser.parseLine(line);
    } catch (Exception e) {
        return true;
    }
    for (DecoderColumnHandle columnHandle : columnHandles) {
        if (columnHandle.isInternal()) {
            continue;
        }
        String mapping = columnHandle.getMapping();
        checkState(mapping != null, "No mapping for column handle %s!", columnHandle);
        int columnIndex = Integer.parseInt(mapping);
        if (columnIndex >= fields.length) {
            continue;
        }
        @SuppressWarnings("unchecked") FieldDecoder<String> decoder = (FieldDecoder<String>) fieldDecoders.get(columnHandle);
        if (decoder != null) {
            fieldValueProviders.add(decoder.decode(fields[columnIndex], columnHandle));
        }
    }
    return false;
}
Also used : DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) FieldDecoder(com.facebook.presto.decoder.FieldDecoder)

Example 7 with DecoderColumnHandle

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

the class JsonRowDecoder method decodeRow.

@Override
public boolean decodeRow(byte[] data, Map<String, String> dataMap, Set<FieldValueProvider> fieldValueProviders, List<DecoderColumnHandle> columnHandles, Map<DecoderColumnHandle, FieldDecoder<?>> fieldDecoders) {
    JsonNode tree;
    try {
        tree = objectMapper.readTree(data);
    } catch (Exception e) {
        return true;
    }
    for (DecoderColumnHandle columnHandle : columnHandles) {
        if (columnHandle.isInternal()) {
            continue;
        }
        @SuppressWarnings("unchecked") FieldDecoder<JsonNode> decoder = (FieldDecoder<JsonNode>) fieldDecoders.get(columnHandle);
        if (decoder != null) {
            JsonNode node = locateNode(tree, columnHandle);
            fieldValueProviders.add(decoder.decode(node, columnHandle));
        }
    }
    return false;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) DecoderColumnHandle(com.facebook.presto.decoder.DecoderColumnHandle) FieldDecoder(com.facebook.presto.decoder.FieldDecoder)

Example 8 with DecoderColumnHandle

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

the class TestCsvDecoder method testNulls.

@Test
public void testNulls() {
    String csv = ",,,";
    CsvRowDecoder rowDecoder = new CsvRowDecoder();
    DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", createVarcharType(10), "0", null, null, false, false, false);
    DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", BigintType.BIGINT, "1", null, null, false, false, false);
    DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", DoubleType.DOUBLE, "2", null, null, false, false, false);
    DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BooleanType.BOOLEAN, "3", null, null, false, false, false);
    List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4);
    Set<FieldValueProvider> providers = new HashSet<>();
    boolean corrupt = rowDecoder.decodeRow(csv.getBytes(StandardCharsets.UTF_8), null, providers, columns, buildMap(columns));
    assertFalse(corrupt);
    assertEquals(providers.size(), columns.size());
    checkValue(providers, row1, "");
    checkValue(providers, row2, 0);
    checkValue(providers, row3, 0.0d);
    checkValue(providers, row4, false);
}
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 9 with DecoderColumnHandle

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

the class TestJsonDecoder method testStringNumber.

@Test
public void testStringNumber() throws Exception {
    byte[] json = "{\"a_number\":481516,\"a_string\":\"2342\"}".getBytes(StandardCharsets.UTF_8);
    JsonRowDecoder rowDecoder = new JsonRowDecoder(PROVIDER.get());
    DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", createVarcharType(100), "a_number", null, null, false, false, false);
    DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", BigintType.BIGINT, "a_number", null, null, false, false, false);
    DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", createVarcharType(100), "a_string", null, null, false, false, false);
    DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BigintType.BIGINT, "a_string", null, null, false, false, false);
    List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4);
    Set<FieldValueProvider> providers = new HashSet<>();
    boolean corrupt = rowDecoder.decodeRow(json, null, providers, columns, buildMap(columns));
    assertFalse(corrupt);
    assertEquals(providers.size(), columns.size());
    checkValue(providers, row1, "481516");
    checkValue(providers, row2, 481516);
    checkValue(providers, row3, "2342");
    checkValue(providers, row4, 2342);
}
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 10 with DecoderColumnHandle

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

the class TestJsonDecoder method testSimple.

@Test
public void testSimple() throws Exception {
    byte[] json = ByteStreams.toByteArray(TestJsonDecoder.class.getResourceAsStream("/decoder/json/message.json"));
    JsonRowDecoder rowDecoder = new JsonRowDecoder(PROVIDER.get());
    DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", createVarcharType(100), "source", null, null, false, false, false);
    DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", createVarcharType(10), "user/screen_name", null, null, false, false, false);
    DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", BigintType.BIGINT, "id", null, null, false, false, false);
    DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BigintType.BIGINT, "user/statuses_count", null, null, false, false, false);
    DecoderTestColumnHandle row5 = new DecoderTestColumnHandle("", 4, "row5", BooleanType.BOOLEAN, "user/geo_enabled", null, null, false, false, false);
    List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4, row5);
    Set<FieldValueProvider> providers = new HashSet<>();
    boolean corrupt = rowDecoder.decodeRow(json, null, providers, columns, buildMap(columns));
    assertFalse(corrupt);
    assertEquals(providers.size(), columns.size());
    checkValue(providers, row1, "<a href=\"http://twitterfeed.com\" rel=\"nofollow\">twitterfeed</a>");
    checkValue(providers, row2, "EKentuckyN");
    checkValue(providers, row3, 493857959588286460L);
    checkValue(providers, row4, 7630);
    checkValue(providers, row5, true);
}
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)

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