use of com.fasterxml.jackson.databind.JsonNode 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 com.fasterxml.jackson.databind.JsonNode in project flink by apache.
the class JsonRowSerializationSchema method serialize.
@Override
public byte[] serialize(Row row) {
if (row.getArity() != fieldNames.length) {
throw new IllegalStateException(String.format("Number of elements in the row %s is different from number of field names: %d", row, fieldNames.length));
}
ObjectNode objectNode = mapper.createObjectNode();
for (int i = 0; i < row.getArity(); i++) {
JsonNode node = mapper.valueToTree(row.getField(i));
objectNode.set(fieldNames[i], node);
}
try {
return mapper.writeValueAsBytes(objectNode);
} catch (Exception e) {
throw new RuntimeException("Failed to serialize row", e);
}
}
use of com.fasterxml.jackson.databind.JsonNode in project flink by apache.
the class ArchivedJobGenerationUtils method compareStringifiedAccumulators.
// ========================================================================
// utility methods
// ========================================================================
public static void compareStringifiedAccumulators(StringifiedAccumulatorResult[] expectedAccs, ArrayNode writtenAccs) {
assertEquals(expectedAccs.length, writtenAccs.size());
for (int x = 0; x < expectedAccs.length; x++) {
JsonNode acc = writtenAccs.get(x);
assertEquals(expectedAccs[x].getName(), acc.get("name").asText());
assertEquals(expectedAccs[x].getType(), acc.get("type").asText());
assertEquals(expectedAccs[x].getValue(), acc.get("value").asText());
}
}
use of com.fasterxml.jackson.databind.JsonNode in project flink by apache.
the class CheckpointStatsDetailsHandlerTest method testCheckpointDetailsRequestInProgressCheckpoint.
/**
* Tests a checkpoint details request for an in progress checkpoint.
*/
@Test
public void testCheckpointDetailsRequestInProgressCheckpoint() throws Exception {
PendingCheckpointStats checkpoint = mock(PendingCheckpointStats.class);
when(checkpoint.getCheckpointId()).thenReturn(1992139L);
when(checkpoint.getStatus()).thenReturn(CheckpointStatsStatus.IN_PROGRESS);
when(checkpoint.getProperties()).thenReturn(CheckpointProperties.forStandardCheckpoint());
when(checkpoint.getTriggerTimestamp()).thenReturn(1919191900L);
when(checkpoint.getLatestAckTimestamp()).thenReturn(1977791901L);
when(checkpoint.getStateSize()).thenReturn(111939272822L);
when(checkpoint.getEndToEndDuration()).thenReturn(121191L);
when(checkpoint.getAlignmentBuffered()).thenReturn(1L);
when(checkpoint.getNumberOfSubtasks()).thenReturn(501);
when(checkpoint.getNumberOfAcknowledgedSubtasks()).thenReturn(101);
List<TaskStateStats> taskStats = new ArrayList<>();
TaskStateStats task1 = createTaskStateStats();
TaskStateStats task2 = createTaskStateStats();
taskStats.add(task1);
taskStats.add(task2);
when(checkpoint.getAllTaskStateStats()).thenReturn(taskStats);
JsonNode rootNode = triggerRequest(checkpoint);
assertEquals(checkpoint.getCheckpointId(), rootNode.get("id").asLong());
assertEquals(checkpoint.getStatus().toString(), rootNode.get("status").asText());
assertEquals(checkpoint.getProperties().isSavepoint(), rootNode.get("is_savepoint").asBoolean());
assertEquals(checkpoint.getTriggerTimestamp(), rootNode.get("trigger_timestamp").asLong());
assertEquals(checkpoint.getLatestAckTimestamp(), rootNode.get("latest_ack_timestamp").asLong());
assertEquals(checkpoint.getStateSize(), rootNode.get("state_size").asLong());
assertEquals(checkpoint.getEndToEndDuration(), rootNode.get("end_to_end_duration").asLong());
assertEquals(checkpoint.getAlignmentBuffered(), rootNode.get("alignment_buffered").asLong());
assertEquals(checkpoint.getNumberOfSubtasks(), rootNode.get("num_subtasks").asInt());
assertEquals(checkpoint.getNumberOfAcknowledgedSubtasks(), rootNode.get("num_acknowledged_subtasks").asInt());
verifyTaskNodes(taskStats, rootNode);
}
use of com.fasterxml.jackson.databind.JsonNode in project flink by apache.
the class CheckpointStatsDetailsHandlerTest method verifyTaskNodes.
private static void verifyTaskNodes(Collection<TaskStateStats> tasks, JsonNode parentNode) {
for (TaskStateStats task : tasks) {
long duration = ThreadLocalRandom.current().nextInt(128);
JsonNode taskNode = parentNode.get("tasks").get(task.getJobVertexId().toString());
assertEquals(task.getLatestAckTimestamp(), taskNode.get("latest_ack_timestamp").asLong());
assertEquals(task.getStateSize(), taskNode.get("state_size").asLong());
assertEquals(task.getEndToEndDuration(task.getLatestAckTimestamp() - duration), taskNode.get("end_to_end_duration").asLong());
assertEquals(task.getAlignmentBuffered(), taskNode.get("alignment_buffered").asLong());
assertEquals(task.getNumberOfSubtasks(), taskNode.get("num_subtasks").asInt());
assertEquals(task.getNumberOfAcknowledgedSubtasks(), taskNode.get("num_acknowledged_subtasks").asInt());
}
}
Aggregations