Search in sources :

Example 1 with Row

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;
}
Also used : Row(org.apache.flink.types.Row)

Example 2 with 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);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Row(org.apache.flink.types.Row) IOException(java.io.IOException)

Example 3 with Row

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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonRowDeserializationSchema(org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema) Row(org.apache.flink.types.Row) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with Row

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);
}
Also used : Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 5 with Row

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;
}
Also used : Row(org.apache.flink.types.Row)

Aggregations

Row (org.apache.flink.types.Row)346 Test (org.junit.Test)241 Table (org.apache.flink.table.api.Table)75 ArrayList (java.util.ArrayList)59 TableEnvironment (org.apache.flink.table.api.TableEnvironment)56 StreamTableEnvironment (org.apache.flink.table.api.bridge.java.StreamTableEnvironment)52 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)50 Configuration (org.apache.flink.configuration.Configuration)45 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)36 RowTypeInfo (org.apache.flink.api.java.typeutils.RowTypeInfo)33 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)30 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)25 TableResult (org.apache.flink.table.api.TableResult)25 List (java.util.List)24 HashMap (java.util.HashMap)22 DataType (org.apache.flink.table.types.DataType)22 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)20 RowData (org.apache.flink.table.data.RowData)20 Path (org.apache.flink.core.fs.Path)19 BatchTableEnvironment (org.apache.flink.table.api.java.BatchTableEnvironment)19