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