Search in sources :

Example 1 with JSONKeyValueDeserializationSchema

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

the class JSONKeyValueDeserializationSchemaTest method testDeserializeWithMetadata.

@Test
public void testDeserializeWithMetadata() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode initialKey = mapper.createObjectNode();
    initialKey.put("index", 4);
    byte[] serializedKey = mapper.writeValueAsBytes(initialKey);
    ObjectNode initialValue = mapper.createObjectNode();
    initialValue.put("word", "world");
    byte[] serializedValue = mapper.writeValueAsBytes(initialValue);
    JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(true);
    ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "topic#1", 3, 4);
    Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
    Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
    Assert.assertEquals("topic#1", deserializedValue.get("metadata").get("topic").asText());
    Assert.assertEquals(4, deserializedValue.get("metadata").get("offset").asInt());
    Assert.assertEquals(3, deserializedValue.get("metadata").get("partition").asInt());
}
Also used : JSONKeyValueDeserializationSchema(org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with JSONKeyValueDeserializationSchema

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

the class JSONKeyValueDeserializationSchemaTest method testDeserializeWithoutMetadata.

@Test
public void testDeserializeWithoutMetadata() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode initialKey = mapper.createObjectNode();
    initialKey.put("index", 4);
    byte[] serializedKey = mapper.writeValueAsBytes(initialKey);
    ObjectNode initialValue = mapper.createObjectNode();
    initialValue.put("word", "world");
    byte[] serializedValue = mapper.writeValueAsBytes(initialValue);
    JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(false);
    ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "", 0, 0);
    Assert.assertTrue(deserializedValue.get("metadata") == null);
    Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
    Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
}
Also used : JSONKeyValueDeserializationSchema(org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 JSONKeyValueDeserializationSchema (org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema)2 Test (org.junit.Test)2