Search in sources :

Example 1 with JsonRowDeserializationSchema

use of org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema 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 2 with JsonRowDeserializationSchema

use of org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema in project flink by apache.

the class JsonRowSerializationSchemaTest method serializeAndDeserialize.

private Row serializeAndDeserialize(String[] fieldNames, Class[] fieldTypes, Row row) throws IOException {
    JsonRowSerializationSchema serializationSchema = new JsonRowSerializationSchema(fieldNames);
    JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(fieldNames, fieldTypes);
    byte[] bytes = serializationSchema.serialize(row);
    return deserializationSchema.deserialize(bytes);
}
Also used : JsonRowDeserializationSchema(org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema) JsonRowSerializationSchema(org.apache.flink.streaming.util.serialization.JsonRowSerializationSchema)

Example 3 with JsonRowDeserializationSchema

use of org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema in project flink by apache.

the class JsonRowDeserializationSchemaTest method testDeserialization.

/**
	 * Tests simple deserialization.
	 */
@Test
public void testDeserialization() throws Exception {
    long id = 1238123899121L;
    String name = "asdlkjasjkdla998y1122";
    byte[] bytes = new byte[1024];
    ThreadLocalRandom.current().nextBytes(bytes);
    ObjectMapper objectMapper = new ObjectMapper();
    // Root
    ObjectNode root = objectMapper.createObjectNode();
    root.put("id", id);
    root.put("name", name);
    root.put("bytes", bytes);
    byte[] serializedJson = objectMapper.writeValueAsBytes(root);
    JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(new String[] { "id", "name", "bytes" }, new Class<?>[] { Long.class, String.class, byte[].class });
    Row deserialized = deserializationSchema.deserialize(serializedJson);
    assertEquals(3, deserialized.getArity());
    assertEquals(id, deserialized.getField(0));
    assertEquals(name, deserialized.getField(1));
    assertArrayEquals(bytes, (byte[]) deserialized.getField(2));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonRowDeserializationSchema(org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema) Row(org.apache.flink.types.Row) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with JsonRowDeserializationSchema

use of org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema in project flink by apache.

the class JsonRowSerializationSchemaTest method testSerializationOfTwoRows.

@Test
public void testSerializationOfTwoRows() throws IOException {
    String[] fieldNames = new String[] { "f1", "f2", "f3" };
    Class[] fieldTypes = new Class[] { Integer.class, Boolean.class, String.class };
    Row row1 = new Row(3);
    row1.setField(0, 1);
    row1.setField(1, true);
    row1.setField(2, "str");
    JsonRowSerializationSchema serializationSchema = new JsonRowSerializationSchema(fieldNames);
    JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(fieldNames, fieldTypes);
    byte[] bytes = serializationSchema.serialize(row1);
    assertEqualRows(row1, deserializationSchema.deserialize(bytes));
    Row row2 = new Row(3);
    row2.setField(0, 10);
    row2.setField(1, false);
    row2.setField(2, "newStr");
    bytes = serializationSchema.serialize(row2);
    assertEqualRows(row2, deserializationSchema.deserialize(bytes));
}
Also used : JsonRowDeserializationSchema(org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema) JsonRowSerializationSchema(org.apache.flink.streaming.util.serialization.JsonRowSerializationSchema) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 5 with JsonRowDeserializationSchema

use of org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema in project flink by apache.

the class KafkaJsonTableSource method setFailOnMissingField.

/**
	 * Configures the failure behaviour if a JSON field is missing.
	 *
	 * <p>By default, a missing field is ignored and the field is set to null.
	 *
	 * @param failOnMissingField Flag indicating whether to fail or not on a missing field.
	 */
public void setFailOnMissingField(boolean failOnMissingField) {
    JsonRowDeserializationSchema deserializationSchema = (JsonRowDeserializationSchema) getDeserializationSchema();
    deserializationSchema.setFailOnMissingField(failOnMissingField);
}
Also used : JsonRowDeserializationSchema(org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema)

Aggregations

JsonRowDeserializationSchema (org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema)5 Row (org.apache.flink.types.Row)3 Test (org.junit.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 JsonRowSerializationSchema (org.apache.flink.streaming.util.serialization.JsonRowSerializationSchema)2 IOException (java.io.IOException)1