use of org.apache.flink.types.Row in project flink by apache.
the class RowSerializerTest method createRow.
// ----------------------------------------------------------------------------------------------
private static Row createRow(Object f0, Object f1, Object f2, Object f3, Object f4) {
Row row = new Row(5);
row.setField(0, f0);
row.setField(1, f1);
row.setField(2, f2);
row.setField(3, f3);
row.setField(4, f4);
return row;
}
use of org.apache.flink.types.Row in project flink by apache.
the class JsonRowDeserializationSchema method deserialize.
@Override
public Row deserialize(byte[] message) throws IOException {
try {
JsonNode root = objectMapper.readTree(message);
Row row = new Row(fieldNames.length);
for (int i = 0; i < fieldNames.length; i++) {
JsonNode node = root.get(fieldNames[i]);
if (node == null) {
if (failOnMissingField) {
throw new IllegalStateException("Failed to find field with name '" + fieldNames[i] + "'.");
} else {
row.setField(i, null);
}
} else {
// Read the value as specified type
Object value = objectMapper.treeToValue(node, fieldTypes[i].getTypeClass());
row.setField(i, value);
}
}
return row;
} catch (Throwable t) {
throw new IOException("Failed to deserialize JSON object.", t);
}
}
use of org.apache.flink.types.Row in project flink by apache.
the class JsonRowDeserializationSchemaTest method testMissingNode.
/**
* Tests deserialization with non-existing field name.
*/
@Test
public void testMissingNode() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
// Root
ObjectNode root = objectMapper.createObjectNode();
root.put("id", 123123123);
byte[] serializedJson = objectMapper.writeValueAsBytes(root);
JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(new String[] { "name" }, new Class<?>[] { String.class });
Row row = deserializationSchema.deserialize(serializedJson);
assertEquals(1, row.getArity());
assertNull("Missing field not null", row.getField(0));
deserializationSchema.setFailOnMissingField(true);
try {
deserializationSchema.deserialize(serializedJson);
fail("Did not throw expected Exception");
} catch (IOException e) {
assertTrue(e.getCause() instanceof IllegalStateException);
}
}
use of org.apache.flink.types.Row in project flink by apache.
the class JsonRowSerializationSchemaTest method testRowSerialization.
@Test
public void testRowSerialization() throws IOException {
String[] fieldNames = new String[] { "f1", "f2", "f3" };
Class[] fieldTypes = new Class[] { Integer.class, Boolean.class, String.class };
Row row = new Row(3);
row.setField(0, 1);
row.setField(1, true);
row.setField(2, "str");
Row resultRow = serializeAndDeserialize(fieldNames, fieldTypes, row);
assertEqualRows(row, resultRow);
}
use of org.apache.flink.types.Row in project flink by apache.
the class HBaseRowInputFormat method mapResultToOutType.
@Override
protected Row mapResultToOutType(Result res) {
for (int f = 0; f < this.families.length; f++) {
// get family key
byte[] familyKey = families[f];
Row familyRow = familyRows[f];
for (int q = 0; q < this.qualifiers[f].length; q++) {
// get quantifier key
byte[] qualifier = qualifiers[f][q];
// get quantifier type idx
int typeIdx = types[f][q];
// read value
byte[] value = res.getValue(familyKey, qualifier);
if (value != null) {
familyRow.setField(q, deserialize(value, typeIdx));
} else {
familyRow.setField(q, null);
}
}
resultRow.setField(f, familyRow);
}
return resultRow;
}
Aggregations