Java Code Examples for org.codehaus.jackson.node.ObjectNode#putArray()

The following examples show how to use org.codehaus.jackson.node.ObjectNode#putArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobHistoryEventHandler.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Private
public JsonNode countersToJSON(Counters counters) {
  ObjectMapper mapper = new ObjectMapper();
  ArrayNode nodes = mapper.createArrayNode();
  if (counters != null) {
    for (CounterGroup counterGroup : counters) {
      ObjectNode groupNode = nodes.addObject();
      groupNode.put("NAME", counterGroup.getName());
      groupNode.put("DISPLAY_NAME", counterGroup.getDisplayName());
      ArrayNode countersNode = groupNode.putArray("COUNTERS");
      for (Counter counter : counterGroup) {
        ObjectNode counterNode = countersNode.addObject();
        counterNode.put("NAME", counter.getName());
        counterNode.put("DISPLAY_NAME", counter.getDisplayName());
        counterNode.put("VALUE", counter.getValue());
      }
    }
  }
  return nodes;
}
 
Example 2
Source File: JobHistoryEventHandler.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Private
public JsonNode countersToJSON(Counters counters) {
  ObjectMapper mapper = new ObjectMapper();
  ArrayNode nodes = mapper.createArrayNode();
  if (counters != null) {
    for (CounterGroup counterGroup : counters) {
      ObjectNode groupNode = nodes.addObject();
      groupNode.put("NAME", counterGroup.getName());
      groupNode.put("DISPLAY_NAME", counterGroup.getDisplayName());
      ArrayNode countersNode = groupNode.putArray("COUNTERS");
      for (Counter counter : counterGroup) {
        ObjectNode counterNode = countersNode.addObject();
        counterNode.put("NAME", counter.getName());
        counterNode.put("DISPLAY_NAME", counter.getDisplayName());
        counterNode.put("VALUE", counter.getValue());
      }
    }
  }
  return nodes;
}
 
Example 3
Source File: PerInstanceAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("healthreports")
public Response getHealthReportsOnInstance(@PathParam("clusterId") String clusterId,
    @PathParam("instanceName") String instanceName) throws IOException {
  HelixDataAccessor accessor = getDataAccssor(clusterId);

  ObjectNode root = JsonNodeFactory.instance.objectNode();
  root.put(Properties.id.name(), instanceName);
  ArrayNode healthReportsNode = root.putArray(PerInstanceProperties.healthreports.name());

  List<String> healthReports =
      accessor.getChildNames(accessor.keyBuilder().healthReports(instanceName));

  if (healthReports != null && healthReports.size() > 0) {
    healthReportsNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(healthReports));
  }

  return JSONRepresentation(root);
}
 
Example 4
Source File: JobAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@GET
public Response getJobs(@PathParam("clusterId") String clusterId,
    @PathParam("workflowName") String workflowName) {
  TaskDriver driver = getTaskDriver(clusterId);
  WorkflowConfig workflowConfig = driver.getWorkflowConfig(workflowName);
  ObjectNode root = JsonNodeFactory.instance.objectNode();

  if (workflowConfig == null) {
    return badRequest(String.format("Workflow %s is not found!", workflowName));
  }

  Set<String> jobs = workflowConfig.getJobDag().getAllNodes();
  root.put(Properties.id.name(), JobProperties.Jobs.name());
  ArrayNode jobsNode = root.putArray(JobProperties.Jobs.name());

  if (jobs != null) {
    jobsNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(jobs));
  }
  return JSONRepresentation(root);
}
 
Example 5
Source File: PerInstanceAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
@GET
@Path("errors")
public Response getErrorsOnInstance(@PathParam("clusterId") String clusterId,
    @PathParam("instanceName") String instanceName) throws IOException {
  HelixDataAccessor accessor = getDataAccssor(clusterId);

  ObjectNode root = JsonNodeFactory.instance.objectNode();
  root.put(Properties.id.name(), instanceName);
  ObjectNode errorsNode = JsonNodeFactory.instance.objectNode();

  List<String> sessionIds = accessor.getChildNames(accessor.keyBuilder().errors(instanceName));

  if (sessionIds == null || sessionIds.size() == 0) {
    return notFound();
  }

  for (String sessionId : sessionIds) {
    List<String> resources =
        accessor.getChildNames(accessor.keyBuilder().errors(instanceName, sessionId));
    if (resources != null) {
      ObjectNode resourcesNode = JsonNodeFactory.instance.objectNode();
      for (String resourceName : resources) {
        List<String> partitions = accessor
            .getChildNames(accessor.keyBuilder().errors(instanceName, sessionId, resourceName));
        if (partitions != null) {
          ArrayNode partitionsNode = resourcesNode.putArray(resourceName);
          partitionsNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(partitions));
        }
      }
      errorsNode.put(sessionId, resourcesNode);
    }
  }
  root.put(PerInstanceProperties.errors.name(), errorsNode);

  return JSONRepresentation(root);
}
 
Example 6
Source File: PendingTasksTest.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link JsonNode} object to mimic the response.
 *
 * @param penaltyMs
 * @param taskIds
 * @param name
 * @param reasons
 * @return Json node for pending tasks whose values are initialized to the provided values.
 * @throws IOException
 */
private JsonNode getMimicResponseJson(
    long penaltyMs, String[] taskIds, String name, List<String> reasons) throws IOException {
  ObjectMapper mapper = new ObjectMapper();
  ObjectNode mutablePendingTaskJson = mapper.createObjectNode();
  // Adding the key=value pairs to mutablePendingTaskJson.
  mutablePendingTaskJson.put("penaltyMs", penaltyMs);
  mutablePendingTaskJson.putArray("taskIds");
  for (String taskId : taskIds) {
    ((ArrayNode) mutablePendingTaskJson.get("taskIds")).add(taskId);
  }
  mutablePendingTaskJson.put("name", name);
  mutablePendingTaskJson.put("reason", reasons.toString());
  return mutablePendingTaskJson;
}
 
Example 7
Source File: IndexerDefinitionJsonSerDeser.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
private void setStringArrayProperty(ObjectNode node, String property, String[] strings) {
    if (strings != null) {
        ArrayNode arrayNode = node.putArray(property);
        for (String string : strings) {
            arrayNode.add(string);
        }
        node.put(property, arrayNode);
    }
}
 
Example 8
Source File: PerInstanceAccessor.java    From helix with Apache License 2.0 4 votes vote down vote up
@GET
@Path("messages")
public Response getMessagesOnInstance(@PathParam("clusterId") String clusterId,
    @PathParam("instanceName") String instanceName,
    @QueryParam("stateModelDef") String stateModelDef) {
  HelixDataAccessor accessor = getDataAccssor(clusterId);

  ObjectNode root = JsonNodeFactory.instance.objectNode();
  root.put(Properties.id.name(), instanceName);
  ArrayNode newMessages = root.putArray(PerInstanceProperties.new_messages.name());
  ArrayNode readMessages = root.putArray(PerInstanceProperties.read_messages.name());

  List<String> messageNames =
      accessor.getChildNames(accessor.keyBuilder().messages(instanceName));
  if (messageNames == null || messageNames.size() == 0) {
    LOG.warn("Unable to get any messages on instance: " + instanceName);
    return notFound();
  }

  for (String messageName : messageNames) {
    Message message = accessor.getProperty(accessor.keyBuilder().message(instanceName, messageName));
    if (message == null) {
      LOG.warn("Message is deleted given message name: ", messageName);
      continue;
    }
    // if stateModelDef is valid, keep messages with StateModelDef equals to the parameter
    if (StringUtil.isNotBlank(stateModelDef) && !stateModelDef.equals(message.getStateModelDef())) {
      continue;
    }

    if (Message.MessageState.NEW.equals(message.getMsgState())) {
      newMessages.add(messageName);
    } else if (Message.MessageState.READ.equals(message.getMsgState())) {
      readMessages.add(messageName);
    }
  }

  root.put(PerInstanceProperties.total_message_count.name(),
      newMessages.size() + readMessages.size());
  root.put(PerInstanceProperties.read_message_count.name(), readMessages.size());

  return JSONRepresentation(root);
}