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