Search in sources :

Example 46 with JsonNode

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

Example 47 with JsonNode

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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 48 with JsonNode

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());
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 49 with JsonNode

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);
}
Also used : PendingCheckpointStats(org.apache.flink.runtime.checkpoint.PendingCheckpointStats) TaskStateStats(org.apache.flink.runtime.checkpoint.TaskStateStats) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 50 with JsonNode

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());
    }
}
Also used : TaskStateStats(org.apache.flink.runtime.checkpoint.TaskStateStats) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1055 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)275 Test (org.junit.Test)267 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)165 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)125 IOException (java.io.IOException)124 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)97 HashMap (java.util.HashMap)78 ArrayList (java.util.ArrayList)75 HttpGet (org.apache.http.client.methods.HttpGet)69 JsonException (jmri.server.json.JsonException)66 Deployment (org.activiti.engine.test.Deployment)66 InputStream (java.io.InputStream)64 StringEntity (org.apache.http.entity.StringEntity)54 ByteArrayInputStream (java.io.ByteArrayInputStream)53 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)49 Map (java.util.Map)45 Task (org.activiti.engine.task.Task)41 HttpPost (org.apache.http.client.methods.HttpPost)39 Tree (org.apache.jackrabbit.oak.api.Tree)30