Java Code Examples for org.codehaus.jettison.json.JSONObject#optJSONObject()

The following examples show how to use org.codehaus.jettison.json.JSONObject#optJSONObject() . 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 Project: appinventor-extensions   File: Compiler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Processes the conditional info from simple_components_build_info.json into
 * a structure mapping annotation types to component names to block names to
 * values.
 *
 * @param compJson Parsed component data from JSON
 * @param type The name of the type being processed
 * @param targetInfo Name of the annotation target being processed (e.g.,
 *                   permissions). Any of: PERMISSIONS_TARGET,
 *                   BROADCAST_RECEIVERS_TARGET
 */
private void processConditionalInfo(JSONObject compJson, String type, String targetInfo) {
  // Strip off the package name since SCM and BKY use unqualified names
  type = type.substring(type.lastIndexOf('.') + 1);

  JSONObject conditionals = compJson.optJSONObject(ComponentDescriptorConstants.CONDITIONALS_TARGET);
  if (conditionals != null) {
    JSONObject jsonBlockMap = conditionals.optJSONObject(targetInfo);
    if (jsonBlockMap != null) {
      if (!this.conditionals.containsKey(targetInfo)) {
        this.conditionals.put(targetInfo, new HashMap<String, Map<String, Set<String>>>());
      }
      Map<String, Set<String>> blockMap = new HashMap<>();
      this.conditionals.get(targetInfo).put(type, blockMap);
      for (String key : (List<String>) Lists.newArrayList(jsonBlockMap.keys())) {
        JSONArray data = jsonBlockMap.optJSONArray(key);
        HashSet<String> result = new HashSet<>();
        for (int i = 0; i < data.length(); i++) {
          result.add(data.optString(i));
        }
        blockMap.put(key, result);
      }
    }
  }
}
 
Example 2
Source Project: tez   File: ATSFileParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parse TezApplication json
 *
 * @param tezApplicationJson
 * @throws JSONException
 */
private void processApplication(JSONObject tezApplicationJson) throws JSONException {
  if (tezApplicationJson != null) {
    LOG.debug("Started parsing tez application");
    JSONObject otherInfoNode = tezApplicationJson.optJSONObject(Constants.OTHER_INFO);
    if (otherInfoNode != null) {
      JSONObject tezVersion = otherInfoNode.optJSONObject(Constants.TEZ_VERSION);
      if (tezVersion != null) {
        String version = tezVersion.optString(Constants.VERSION);
        String buildTime = tezVersion.optString(Constants.BUILD_TIME);
        String revision = tezVersion.optString(Constants.REVISION);
        this.versionInfo = new VersionInfo(version, revision, buildTime);
      }
      //Parse Config info
      this.config = Maps.newHashMap();
      JSONObject configNode = otherInfoNode.getJSONObject(Constants.CONFIG);
      Iterator it = configNode.keys();
      while(it.hasNext()) {
        String key = (String) it.next();
        String value = configNode.getString(key);
        config.put(key, value);
      }
    }
    LOG.debug("Finished parsing tez application");
  }
}
 
Example 3
private void getFlatMap(JSONObject jSONObject, Map<String, Object> map, String keyPrefix) throws Exception
{
  Iterator<String> iterator = jSONObject.keys();
  while (iterator.hasNext()) {
    String key = iterator.next();
    String insertKey = (keyPrefix == null) ? key : keyPrefix + CONCAT_CHAR + key;

    JSONObject value = jSONObject.optJSONObject(key);
    if (value == null) {
      map.put(insertKey, jSONObject.get(key));
    } else {
      getFlatMap(value, map, insertKey);
    }
  }
}
 
Example 4
Source Project: tez   File: DAGClientTimelineImpl.java    License: Apache License 2.0 5 votes vote down vote up
private VertexStatusProto.Builder parseVertexStatus(JSONObject jsonRoot,
                                                    Set<StatusGetOpts> statusOptions)
    throws JSONException {
  final JSONObject otherInfoNode = jsonRoot.getJSONObject(ATSConstants.OTHER_INFO);
  final VertexStatusProto.Builder vertexStatusBuilder = VertexStatusProto.newBuilder();

  final String status = otherInfoNode.optString(ATSConstants.STATUS);
  final String diagnostics = otherInfoNode.optString(ATSConstants.DIAGNOSTICS);
  if (status.equals("")) {
    return null;
  }

  vertexStatusBuilder.setState(vertexStateProtoMap.get(status))
      .addAllDiagnostics(Collections.singleton(diagnostics));

  int numRunningTasks = otherInfoNode.optInt(ATSConstants.NUM_TASKS) -
      otherInfoNode.optInt(ATSConstants.NUM_COMPLETED_TASKS);
  ProgressProto.Builder progressBuilder = ProgressProto.newBuilder();
  progressBuilder.setTotalTaskCount(otherInfoNode.optInt(ATSConstants.NUM_TASKS));
  progressBuilder.setRunningTaskCount(numRunningTasks);
  progressBuilder.setSucceededTaskCount(otherInfoNode.optInt(ATSConstants.NUM_SUCCEEDED_TASKS));
  progressBuilder.setKilledTaskCount(otherInfoNode.optInt(ATSConstants.NUM_KILLED_TASKS));
  progressBuilder.setFailedTaskCount(otherInfoNode.optInt(ATSConstants.NUM_FAILED_TASKS));
  vertexStatusBuilder.setProgress(progressBuilder);

  if (statusOptions != null && statusOptions.contains(StatusGetOpts.GET_COUNTERS)) {
    final TezCountersProto.Builder tezCounterBuilder;
    final JSONObject countersNode = otherInfoNode.optJSONObject(ATSConstants.COUNTERS);
    tezCounterBuilder = parseDagCounters(countersNode);
    if (tezCounterBuilder != null) {
      vertexStatusBuilder.setVertexCounters(tezCounterBuilder);
    }
  }

  return vertexStatusBuilder;
}
 
Example 5
Source Project: Bats   File: LogicalPlanSerializer.java    License: Apache License 2.0 4 votes vote down vote up
public static PropertiesConfiguration convertToProperties(JSONObject json) throws JSONException
{
  PropertiesConfiguration props = new PropertiesConfiguration();
  JSONArray allOperators = json.getJSONArray("operators");
  JSONArray allStreams = json.getJSONArray("streams");

  for (int j = 0; j < allOperators.length(); j++) {
    JSONObject operatorDetail = allOperators.getJSONObject(j);
    String operatorName = operatorDetail.getString("name");
    String operatorKey = LogicalPlanConfiguration.OPERATOR_PREFIX + operatorName;
    props.setProperty(operatorKey + ".classname", operatorDetail.getString("class"));
    JSONObject properties = operatorDetail.optJSONObject("properties");
    if (properties != null) {
      Iterator<String> iter2 = properties.keys();
      while (iter2.hasNext()) {
        String propertyName = iter2.next();
        if (!propertyName.equals("name") && !propertyName.equals("class") && properties.opt(propertyName) != null) {
          JSONArray list = properties.optJSONArray(propertyName);
          String value = "";
          if (list != null) {
            for (int i = 0; i < list.length(); i++) {
              if (i != 0) {
                value += ",";
              }
              value += list.get(i).toString();
            }
            props.setProperty(operatorKey + "." + propertyName, value);
          } else {
            props.setProperty(operatorKey + "." + propertyName, properties.get(propertyName));
          }
        }
      }
    }
  }

  for (int j = 0; j < allStreams.length(); j++) {
    JSONObject streamDetail = allStreams.getJSONObject(j);
    String streamName = streamDetail.getString("name");
    String streamKey = LogicalPlanConfiguration.STREAM_PREFIX + streamName;
    JSONObject sourceDetail = streamDetail.getJSONObject("source");
    JSONArray sinksList = streamDetail.getJSONArray("sinks");

    props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_SOURCE, sourceDetail.getString("operatorName") + "." + sourceDetail.getString("portName"));
    String sinksValue = "";
    for (int i = 0; i < sinksList.length(); i++) {
      if (!sinksValue.isEmpty()) {
        sinksValue += ",";
      }
      sinksValue += sinksList.getJSONObject(i).getString("operatorName") + "." + sinksList.getJSONObject(i).getString("portName");
    }
    props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_SINKS, sinksValue);
    String locality = streamDetail.optString("locality", null);
    if (locality != null) {
      props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_LOCALITY, Locality.valueOf(locality));
    }
  }

  // TBD: Attributes

  return props;
}
 
Example 6
public static PropertiesConfiguration convertToProperties(JSONObject json) throws JSONException
{
  PropertiesConfiguration props = new PropertiesConfiguration();
  JSONArray allOperators = json.getJSONArray("operators");
  JSONArray allStreams = json.getJSONArray("streams");

  for (int j = 0; j < allOperators.length(); j++) {
    JSONObject operatorDetail = allOperators.getJSONObject(j);
    String operatorName = operatorDetail.getString("name");
    String operatorKey = LogicalPlanConfiguration.OPERATOR_PREFIX + operatorName;
    props.setProperty(operatorKey + ".classname", operatorDetail.getString("class"));
    JSONObject properties = operatorDetail.optJSONObject("properties");
    if (properties != null) {
      Iterator<String> iter2 = properties.keys();
      while (iter2.hasNext()) {
        String propertyName = iter2.next();
        if (!propertyName.equals("name") && !propertyName.equals("class") && properties.opt(propertyName) != null) {
          JSONArray list = properties.optJSONArray(propertyName);
          String value = "";
          if (list != null) {
            for (int i = 0; i < list.length(); i++) {
              if (i != 0) {
                value += ",";
              }
              value += list.get(i).toString();
            }
            props.setProperty(operatorKey + "." + propertyName, value);
          } else {
            props.setProperty(operatorKey + "." + propertyName, properties.get(propertyName));
          }
        }
      }
    }
  }

  for (int j = 0; j < allStreams.length(); j++) {
    JSONObject streamDetail = allStreams.getJSONObject(j);
    String streamName = streamDetail.getString("name");
    String streamKey = LogicalPlanConfiguration.STREAM_PREFIX + streamName;
    JSONObject sourceDetail = streamDetail.getJSONObject("source");
    JSONArray sinksList = streamDetail.getJSONArray("sinks");

    props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_SOURCE, sourceDetail.getString("operatorName") + "." + sourceDetail.getString("portName"));
    String sinksValue = "";
    for (int i = 0; i < sinksList.length(); i++) {
      if (!sinksValue.isEmpty()) {
        sinksValue += ",";
      }
      sinksValue += sinksList.getJSONObject(i).getString("operatorName") + "." + sinksList.getJSONObject(i).getString("portName");
    }
    props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_SINKS, sinksValue);
    String locality = streamDetail.optString("locality", null);
    if (locality != null) {
      props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_LOCALITY, Locality.valueOf(locality));
    }
  }

  // TBD: Attributes

  return props;
}
 
Example 7
Source Project: tez   File: DAGClientTimelineImpl.java    License: Apache License 2.0 4 votes vote down vote up
private DAGStatusProto.Builder parseDagStatus(JSONObject jsonRoot, Set<StatusGetOpts> statusOptions)
    throws JSONException, TezException {
  final JSONObject otherInfoNode = jsonRoot.getJSONObject(ATSConstants.OTHER_INFO);

  DAGStatusProto.Builder dagStatusBuilder = DAGStatusProto.newBuilder();

  final String status = otherInfoNode.optString(ATSConstants.STATUS);
  final String diagnostics = otherInfoNode.optString(ATSConstants.DIAGNOSTICS);
  if (status.equals("")) {
    return null;
  }

  dagStatusBuilder.setState(dagStateProtoMap.get(status))
      .addAllDiagnostics(Collections.singleton(diagnostics));

  if (statusOptions != null && statusOptions.contains(StatusGetOpts.GET_COUNTERS)) {
    final TezCountersProto.Builder tezCounterBuilder;
    final JSONObject countersNode = otherInfoNode.optJSONObject(ATSConstants.COUNTERS);
    tezCounterBuilder = parseDagCounters(countersNode);
    if (tezCounterBuilder != null) {
      dagStatusBuilder.setDagCounters(tezCounterBuilder);
    }
  }

  final Map<String, VertexTaskStats> vertexTaskStatsMap = parseTaskStatsForVertexes();
  if (vertexTaskStatsMap.size() > 0) {
    ProgressProto.Builder dagProgressBuilder = getProgressBuilder(vertexTaskStatsMap, null);
    dagStatusBuilder.setDAGProgress(dagProgressBuilder);

    List<StringProgressPairProto> vertexProgressBuilder =
        new ArrayList<StringProgressPairProto>(vertexTaskStatsMap.size());
    for (Map.Entry<String, VertexTaskStats> v : vertexTaskStatsMap.entrySet()) {
      StringProgressPairProto vertexProgressProto = StringProgressPairProto
          .newBuilder()
          .setKey(v.getKey())
          .setProgress(getProgressBuilder(vertexTaskStatsMap, v.getKey()))
          .build();
      vertexProgressBuilder.add(vertexProgressProto);
    }
    dagStatusBuilder.addAllVertexProgress(vertexProgressBuilder);
  }

  return dagStatusBuilder;
}
 
Example 8
Source Project: tez   File: DagInfo.java    License: Apache License 2.0 4 votes vote down vote up
DagInfo(JSONObject jsonObject) throws JSONException {
  super(jsonObject);

  vertexNameMap = Maps.newHashMap();
  vertexNameIDMapping = new DualHashBidiMap<>();
  edgeInfoMap = Maps.newHashMap();
  basicVertexInfoMap = Maps.newHashMap();
  containerMapping = LinkedHashMultimap.create();

  Preconditions.checkArgument(jsonObject.getString(Constants.ENTITY_TYPE).equalsIgnoreCase
      (Constants.TEZ_DAG_ID));

  dagId = StringInterner.weakIntern(jsonObject.getString(Constants.ENTITY));

  //Parse additional Info
  JSONObject otherInfoNode = jsonObject.getJSONObject(Constants.OTHER_INFO);

  long sTime = otherInfoNode.optLong(Constants.START_TIME);
  long eTime= otherInfoNode.optLong(Constants.FINISH_TIME);
  userName = otherInfoNode.optString(Constants.USER);
  if (eTime < sTime) {
    LOG.warn("DAG has got wrong start/end values. "
        + "startTime=" + sTime + ", endTime=" + eTime + ". Will check "
        + "timestamps in DAG started/finished events");

    // Check if events DAG_STARTED, DAG_FINISHED can be made use of
    for(Event event : eventList) {
      switch (HistoryEventType.valueOf(event.getType())) {
      case DAG_STARTED:
        sTime = event.getAbsoluteTime();
        break;
      case DAG_FINISHED:
        eTime = event.getAbsoluteTime();
        break;
      default:
        break;
      }
    }

    if (eTime < sTime) {
      LOG.warn("DAG has got wrong start/end values in events as well. "
          + "startTime=" + sTime + ", endTime=" + eTime);
    }
  }
  startTime = sTime;
  endTime = eTime;

  //TODO: Not getting populated correctly for lots of jobs.  Verify
  submitTime = otherInfoNode.optLong(Constants.START_REQUESTED_TIME);
  diagnostics = otherInfoNode.optString(Constants.DIAGNOSTICS);
  failedTasks = otherInfoNode.optInt(Constants.NUM_FAILED_TASKS);
  JSONObject dagPlan = otherInfoNode.optJSONObject(Constants.DAG_PLAN);
  name = StringInterner.weakIntern((dagPlan != null) ? (dagPlan.optString(Constants.DAG_NAME)) : null);
  if (dagPlan != null) {
    JSONArray vertices = dagPlan.optJSONArray(Constants.VERTICES);
    if (vertices != null) {
      numVertices = vertices.length();
    } else {
      numVertices = 0;
    }
    parseDAGPlan(dagPlan);
  } else {
    numVertices = 0;
  }
  status = StringInterner.weakIntern(otherInfoNode.optString(Constants.STATUS));

  //parse name id mapping
  JSONObject vertexIDMappingJson = otherInfoNode.optJSONObject(Constants.VERTEX_NAME_ID_MAPPING);
  if (vertexIDMappingJson != null) {
    //get vertex name
    for (Map.Entry<String, BasicVertexInfo> entry : basicVertexInfoMap.entrySet()) {
      String vertexId = vertexIDMappingJson.optString(entry.getKey());
      //vertexName --> vertexId
      vertexNameIDMapping.put(entry.getKey(), vertexId);
    }
  }
}
 
Example 9
Source Project: tez   File: ATSFileParser.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Read zip file contents. Every file can contain "dag", "vertices", "tasks", "task_attempts"
 *
 * @param atsFile
 * @throws IOException
 * @throws JSONException
 */
private void parseATSZipFile(File atsFile)
    throws IOException, JSONException, TezException, InterruptedException {
  final ZipFile atsZipFile = new ZipFile(atsFile);
  try {
    Enumeration<? extends ZipEntry> zipEntries = atsZipFile.entries();
    while (zipEntries.hasMoreElements()) {
      ZipEntry zipEntry = zipEntries.nextElement();
      LOG.debug("Processing " + zipEntry.getName());
      InputStream inputStream = atsZipFile.getInputStream(zipEntry);
      JSONObject jsonObject = readJson(inputStream);

      //This json can contain dag, vertices, tasks, task_attempts
      JSONObject dagJson = jsonObject.optJSONObject(Constants.DAG);
      if (dagJson != null) {
        //TODO: support for multiple dags per ATS file later.
        dagInfo = DagInfo.create(dagJson);
      }

      //Process vertex
      JSONArray vertexJson = jsonObject.optJSONArray(Constants.VERTICES);
      if (vertexJson != null) {
        processVertices(vertexJson);
      }

      //Process task
      JSONArray taskJson = jsonObject.optJSONArray(Constants.TASKS);
      if (taskJson != null) {
        processTasks(taskJson);
      }

      //Process task attempts
      JSONArray attemptsJson = jsonObject.optJSONArray(Constants.TASK_ATTEMPTS);
      if (attemptsJson != null) {
        processAttempts(attemptsJson);
      }

      //Process application (mainly versionInfo)
      JSONObject tezAppJson = jsonObject.optJSONObject(Constants.APPLICATION);
      if (tezAppJson != null) {
        processApplication(tezAppJson);
      }
    }
  } finally {
    atsZipFile.close();
  }
}