Java Code Examples for org.codehaus.jettison.json.JSONArray#getJSONObject()

The following examples show how to use org.codehaus.jettison.json.JSONArray#getJSONObject() . 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: GraphBackedMetadataRepositoryTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test(dependsOnMethods = "testSubmitEntity")
public void testSearchByDSLWithInheritance() throws Exception {
    String dslQuery = "Person where name = 'Jane'";
    System.out.println("Executing dslQuery = " + dslQuery);
    String jsonResults = discoveryService.searchByDSL(dslQuery, queryParams);
    Assert.assertNotNull(jsonResults);

    JSONObject results = new JSONObject(jsonResults);
    Assert.assertEquals(results.length(), 3);
    System.out.println("results = " + results);

    Object query = results.get("query");
    Assert.assertNotNull(query);

    JSONObject dataType = results.getJSONObject("dataType");
    Assert.assertNotNull(dataType);
    String typeName = dataType.getString("typeName");
    Assert.assertEquals(typeName, "Person");

    JSONArray rows = results.getJSONArray("rows");
    Assert.assertEquals(rows.length(), 1);

    JSONObject row = rows.getJSONObject(0);
    Assert.assertEquals(row.getString("$typeName$"), "Manager");
    Assert.assertEquals(row.getString("name"), "Jane");
}
 
Example 2
Source File: TestHsWebServicesAttempts.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public void verifyHsTaskAttempts(JSONObject json, Task task)
    throws JSONException {
  assertEquals("incorrect number of elements", 1, json.length());
  JSONObject attempts = json.getJSONObject("taskAttempts");
  assertEquals("incorrect number of elements", 1, json.length());
  JSONArray arr = attempts.getJSONArray("taskAttempt");
  for (TaskAttempt att : task.getAttempts().values()) {
    TaskAttemptId id = att.getID();
    String attid = MRApps.toString(id);
    Boolean found = false;

    for (int i = 0; i < arr.length(); i++) {
      JSONObject info = arr.getJSONObject(i);
      if (attid.matches(info.getString("id"))) {
        found = true;
        verifyHsTaskAttempt(info, att, task.getType());
      }
    }
    assertTrue("task attempt with id: " + attid
        + " not in web service output", found);
  }
}
 
Example 3
Source File: TypeGraph.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
/**
 * A utility method that tells whether a class is considered a bean.<br/>
 * For simplicity we exclude classes that have any type-args.
 *
 * @param className name of the class
 * @return true if it is a bean false otherwise.
 */
public boolean isInstantiableBean(String className) throws JSONException
{
  JSONObject classDesc = describeClass(className);
  if (classDesc.has("typeArgs")) {
    //any type with generics is not considered a bean
    return false;
  }
  JSONArray classProps = classDesc.optJSONArray("properties");
  if (classProps == null || classProps.length() == 0) {
    //no properties then cannot be a bean
    return false;
  }
  for (int p = 0; p < classProps.length(); p++) {
    JSONObject propDesc = classProps.getJSONObject(p);
    if (propDesc.optBoolean("canGet", false)) {
      return true;
    }
  }
  return false;
}
 
Example 4
Source File: TestAMWebServicesTasks.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public void verifyAMTask(JSONArray arr, Job job, String type)
    throws JSONException {
  for (Task task : job.getTasks().values()) {
    TaskId id = task.getID();
    String tid = MRApps.toString(id);
    Boolean found = false;
    if (type != null && task.getType() == MRApps.taskType(type)) {

      for (int i = 0; i < arr.length(); i++) {
        JSONObject info = arr.getJSONObject(i);
        if (tid.matches(info.getString("id"))) {
          found = true;
          verifyAMSingleTask(info, task);
        }
      }
      assertTrue("task with id: " + tid + " not in web service output", found);
    }
  }
}
 
Example 5
Source File: TestAMWebServicesAttempts.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void verifyAMTaskAttempts(JSONObject json, Task task)
    throws JSONException {
  assertEquals("incorrect number of elements", 1, json.length());
  JSONObject attempts = json.getJSONObject("taskAttempts");
  assertEquals("incorrect number of elements", 1, json.length());
  JSONArray arr = attempts.getJSONArray("taskAttempt");
  for (TaskAttempt att : task.getAttempts().values()) {
    TaskAttemptId id = att.getID();
    String attid = MRApps.toString(id);
    Boolean found = false;

    for (int i = 0; i < arr.length(); i++) {
      JSONObject info = arr.getJSONObject(i);
      if (attid.matches(info.getString("id"))) {
        found = true;
        verifyAMTaskAttempt(info, att, task.getType());
      }
    }
    assertTrue("task attempt with id: " + attid
        + " not in web service output", found);
  }
}
 
Example 6
Source File: TestRMWebServicesCapacitySched.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void verifyClusterScheduler(JSONObject json) throws JSONException,
    Exception {
  assertEquals("incorrect number of elements", 1, json.length());
  JSONObject info = json.getJSONObject("scheduler");
  assertEquals("incorrect number of elements", 1, info.length());
  info = info.getJSONObject("schedulerInfo");
  assertEquals("incorrect number of elements", 6, info.length());
  verifyClusterSchedulerGeneric(info.getString("type"),
      (float) info.getDouble("usedCapacity"),
      (float) info.getDouble("capacity"),
      (float) info.getDouble("maxCapacity"), info.getString("queueName"));

  JSONArray arr = info.getJSONObject("queues").getJSONArray("queue");
  assertEquals("incorrect number of elements", 2, arr.length());

  // test subqueues
  for (int i = 0; i < arr.length(); i++) {
    JSONObject obj = arr.getJSONObject(i);
    String q = CapacitySchedulerConfiguration.ROOT + "." + obj.getString("queueName");
    verifySubQueue(obj, q, 100, 100);
  }
}
 
Example 7
Source File: TestAMWebServicesJobs.java    From big-c with Apache License 2.0 5 votes vote down vote up
public void verifyAMJobCounters(JSONObject info, Job job)
    throws JSONException {

  assertEquals("incorrect number of elements", 2, info.length());

  WebServicesTestUtils.checkStringMatch("id", MRApps.toString(job.getID()),
      info.getString("id"));
  // just do simple verification of fields - not data is correct
  // in the fields
  JSONArray counterGroups = info.getJSONArray("counterGroup");
  for (int i = 0; i < counterGroups.length(); i++) {
    JSONObject counterGroup = counterGroups.getJSONObject(i);
    String name = counterGroup.getString("counterGroupName");
    assertTrue("name not set", (name != null && !name.isEmpty()));
    JSONArray counters = counterGroup.getJSONArray("counter");
    for (int j = 0; j < counters.length(); j++) {
      JSONObject counter = counters.getJSONObject(j);
      String counterName = counter.getString("name");
      assertTrue("counter name not set",
          (counterName != null && !counterName.isEmpty()));

      long mapValue = counter.getLong("mapCounterValue");
      assertTrue("mapCounterValue  >= 0", mapValue >= 0);

      long reduceValue = counter.getLong("reduceCounterValue");
      assertTrue("reduceCounterValue  >= 0", reduceValue >= 0);

      long totalValue = counter.getLong("totalCounterValue");
      assertTrue("totalCounterValue  >= 0", totalValue >= 0);

    }
  }
}
 
Example 8
Source File: TargetHostsBuilderHelperCms.java    From parallec with Apache License 2.0 5 votes vote down vote up
/**
 * 20141022.
 *
 * @param jObj
 *            the j obj
 * @param projectionStr
 *            the projection str
 * @return the FQDN value list cms
 * @throws JSONException
 *             the JSON exception
 */
static List<String> getFQDNValueListCMS(JSONObject jObj,
        String projectionStr) throws JSONException {
    final List<String> labelList = new ArrayList<String>();

    if (!jObj.has("result")) {
        logger.error("!!CMS_ERROR! result key is not in jOBJ in getFQDNValueListCMS!!: \njObj:"
                + PcStringUtils.renderJson(jObj));

        return labelList;
    }
    JSONArray jArr = (JSONArray) jObj.get("result");
    if (jArr == null || jArr.length() == 0) {
        return labelList;
    }
    for (int i = 0; i < jArr.length(); ++i) {
        JSONObject agentObj = jArr.getJSONObject(i);
        // properties can be null

        if (!agentObj.has(projectionStr)) {
            continue;
        }
        String label = (String) agentObj.get(projectionStr);

        if (label != null && !label.trim().isEmpty()) {
            labelList.add(label);
        }
    }

    return labelList;
}
 
Example 9
Source File: TestRMWebServicesNodes.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testNodesQueryStateLost() throws JSONException, Exception {
  WebResource r = resource();
  MockNM nm1 = rm.registerNode("h1:1234", 5120);
  MockNM nm2 = rm.registerNode("h2:1234", 5120);
  rm.sendNodeStarted(nm1);
  rm.sendNodeStarted(nm2);
  rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING);
  rm.NMwaitForState(nm2.getNodeId(), NodeState.RUNNING);
  rm.sendNodeLost(nm1);
  rm.sendNodeLost(nm2);

  ClientResponse response = r.path("ws").path("v1").path("cluster")
      .path("nodes").queryParam("states", NodeState.LOST.toString())
      .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

  assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  JSONObject json = response.getEntity(JSONObject.class);
  JSONObject nodes = json.getJSONObject("nodes");
  assertEquals("incorrect number of elements", 1, nodes.length());
  JSONArray nodeArray = nodes.getJSONArray("node");
  assertEquals("incorrect number of elements", 2, nodeArray.length());
  for (int i = 0; i < nodeArray.length(); ++i) {
    JSONObject info = nodeArray.getJSONObject(i);
    String host = info.get("id").toString().split(":")[0];
    RMNode rmNode = rm.getRMContext().getInactiveRMNodes().get(host);
    WebServicesTestUtils.checkStringMatch("nodeHTTPAddress", "",
        info.getString("nodeHTTPAddress"));
    WebServicesTestUtils.checkStringMatch("state", rmNode.getState()
        .toString(), info.getString("state"));
  }
}
 
Example 10
Source File: TaskBoardHelperTest.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get all stories and tasks, no filter
 * 
 * @throws JSONException
 */
@Test
public void testGetTaskBoardStoryTaskListText_ALL() throws JSONException {
	String response = mTaskBoardHelper.getTaskBoardStoryTaskListText("ALL").toString();
	JSONObject json = new JSONObject(response);

	JSONArray stories = json.getJSONArray("Stories");
	assertEquals(3, stories.length());
	for (int i = 0; i < stories.length(); i++) {
		String storyIndex = String.valueOf(i + 1);
		
		JSONObject story = stories.getJSONObject(i);
		assertEquals(storyIndex, story.getString("Id"));
		assertEquals("TEST_STORY_" + storyIndex, story.getString("Name"));
		assertEquals("TEST_STORY_NOTE_" + storyIndex, story.getString("Notes"));
		assertEquals("TEST_STORY_DEMO_" + storyIndex, story.getString("HowToDemo"));
		assertEquals("50", story.getString("Value"));
		assertEquals("5", story.getString("Estimate"));
		assertEquals("100", story.getString("Importance"));
		assertEquals("1", story.getString("Sprint"));
		assertEquals(0, story.getJSONArray("AttachFileList").length());

		JSONArray tasks = story.getJSONArray("Tasks");
		for (int j = 0; j < tasks.length(); j++) {
			String taskIndex = String.valueOf(i * 3 + j + 1);
			JSONObject task = tasks.getJSONObject(j);
			assertEquals(taskIndex, task.getString("Id"));
			assertEquals("TEST_TASK_" + String.valueOf(j + 1), task.getString("Name"));
			assertEquals("TEST_TASK_NOTES_" + String.valueOf(j + 1), task.getString("Notes"));
			assertEquals("", task.getString("HandlerUserName"));
			assertEquals("", task.getString("Partners"));
			assertEquals("8", task.getString("Estimate"));
			assertEquals("0", task.getString("Actual"));
			assertEquals("8", task.getString("RemainHours"));
			assertEquals("new", task.getString("Status"));
			assertEquals(0, task.getJSONArray("AttachFileList").length());
		}
	}
}
 
Example 11
Source File: RestIT.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testScenario() throws JSONException {
  // get process definitions for default engine
  log.info("Checking " + APP_BASE_PATH + PROCESS_DEFINITION_PATH);
  WebResource resource = client.resource(APP_BASE_PATH + PROCESS_DEFINITION_PATH);
  ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

  assertEquals(200, response.getStatus());

  JSONArray definitionsJson = response.getEntity(JSONArray.class);
  response.close();

  // invoice example
  assertEquals(3, definitionsJson.length());

  // order of results is not consistent between database types
  for (int i = 0; i < definitionsJson.length(); i++) {
    JSONObject definitionJson = definitionsJson.getJSONObject(i);
    assertTrue(definitionJson.isNull("description"));
    assertFalse(definitionJson.getBoolean("suspended"));
    if (definitionJson.getString("key").equals("ReviewInvoice")) {
      assertEquals("http://bpmn.io/schema/bpmn", definitionJson.getString("category"));
      assertEquals("Review Invoice", definitionJson.getString("name"));
      assertTrue(definitionJson.getString("resource").equals("reviewInvoice.bpmn"));
    } else if (definitionJson.getString("key").equals("invoice")) {
      assertEquals("http://www.omg.org/spec/BPMN/20100524/MODEL", definitionJson.getString("category"));
      assertEquals("Invoice Receipt", definitionJson.getString("name"));
      assertTrue(definitionJson.getString("resource").matches("invoice\\.v[1,2]\\.bpmn"));
    } else {
      fail("Unexpected definition key in response JSON.");
    }
  }
}
 
Example 12
Source File: SnapshotSchema.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * This is a helper method to initialize the schema.
 * @throws JSONException This exception is thrown if there is an error parsing the JSON
 * which specified this schema.
 */
private void initialize() throws JSONException
{
  schema = new JSONObject(schemaJSON);

  if (schemaKeys != null) {
    schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
  }

  valueToType = Maps.newLinkedHashMap();

  JSONArray values = schema.getJSONArray(FIELD_VALUES);

  Preconditions.checkState(values.length() > 0,
      "The schema does not specify any values.");

  for (int index = 0; index < values.length(); index++) {
    JSONObject value = values.getJSONObject(index);
    String name = value.getString(FIELD_VALUES_NAME);
    String typeName = value.getString(FIELD_VALUES_TYPE);

    Type type = Type.NAME_TO_TYPE.get(typeName);
    valueToType.put(name, type);

    Preconditions.checkArgument(type != null, typeName + " is not a valid type.");
  }

  valueToType = Collections.unmodifiableMap(valueToType);
  valuesDescriptor = new FieldsDescriptor(valueToType);

  try {
    schema.put(FIELD_SCHEMA_TYPE, SCHEMA_TYPE);
    schema.put(FIELD_SCHEMA_VERSION, SCHEMA_VERSION);
  } catch (JSONException e) {
    throw new RuntimeException(e);
  }

  schemaJSON = this.schema.toString();
}
 
Example 13
Source File: TestAMWebServicesJobs.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testJobsDefault() throws JSONException, Exception {
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("mapreduce")
      .path("jobs").get(ClientResponse.class);
  assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  JSONObject json = response.getEntity(JSONObject.class);
  assertEquals("incorrect number of elements", 1, json.length());
  JSONObject jobs = json.getJSONObject("jobs");
  JSONArray arr = jobs.getJSONArray("job");
  JSONObject info = arr.getJSONObject(0);
  Job job = appContext.getJob(MRApps.toJobID(info.getString("id")));
  verifyAMJob(info, job);

}
 
Example 14
Source File: JobHistoryFileParserHadoop2.java    From hraven with Apache License 2.0 4 votes vote down vote up
/**
 * understand the schema so that we can parse the rest of the file
 * @throws JSONException
 */
private void understandSchema(String schema) throws JSONException {

  JSONObject j1 = new JSONObject(schema);
  JSONArray fields = j1.getJSONArray(FIELDS);

  String fieldName;
  String fieldTypeValue;
  Object recName;

  for (int k = 0; k < fields.length(); k++) {
    if (fields.get(k) == null) {
      continue;
    }
    JSONObject allEvents = new JSONObject(fields.get(k).toString());
    Object name = allEvents.get(NAME);
    if (name != null) {
      if (name.toString().equalsIgnoreCase(EVENT)) {
        JSONArray allTypeDetails = allEvents.getJSONArray(TYPE);
        for (int i = 0; i < allTypeDetails.length(); i++) {
          JSONObject actual = (JSONObject) allTypeDetails.get(i);
          JSONArray types = actual.getJSONArray(FIELDS);
          Map<String, String> typeDetails = new HashMap<String, String>();
          for (int j = 0; j < types.length(); j++) {
            if (types.getJSONObject(j) == null ) {
              continue;
            }
            fieldName = types.getJSONObject(j).getString(NAME);
            fieldTypeValue = types.getJSONObject(j).getString(TYPE);
            if ((fieldName != null) && (fieldTypeValue != null)) {
              typeDetails.put(fieldName, fieldTypeValue);
            }
          }

          recName = actual.get(NAME);
          if (recName != null) {
            /* the next statement may throw an IllegalArgumentException if
             * it finds a new string that's not part of the Hadoop2RecordType enum
             * that way we know what types of events we are parsing
             */
            fieldTypes.put(Hadoop2RecordType.valueOf(recName.toString()), typeDetails);
          }
        }
      }
    }
  }
}
 
Example 15
Source File: AdsDimensionsDemoTest.java    From examples with Apache License 2.0 4 votes vote down vote up
@Test
public void applicationTest() throws Exception
{
  String gatewayConnectAddress = "localhost:9090";
  URI uri = URI.create("ws://" + gatewayConnectAddress + "/pubsub");

  AdsDimensionsDemo adsDemo = new AdsDimensionsDemo();
  adsDemo.inputOperator = new MockGenerator();

  Configuration conf = new Configuration(false);
  conf.addResource("META-INF/properties.xml");
  conf.set("dt.attr.GATEWAY_CONNECT_ADDRESS", gatewayConnectAddress);
  conf.set("dt.application.AdsDimensionsDemoGeneric.operator.InputGenerator.attr.PARTITIONER",
      "com.datatorrent.common.partitioner.StatelessPartitioner:1");
  conf.set("dt.application.AdsDimensionsDemoGeneric.operator.Store.fileStore.basePathPrefix",
      testMeta.getDir());

  LocalMode lma = LocalMode.newInstance();
  lma.prepareDAG(adsDemo, conf);
  lma.cloneDAG();
  LocalMode.Controller lc = lma.getController();
  lc.setHeartbeatMonitoringEnabled(false);
  lc.runAsync();

  String query = SchemaUtils.jarResourceFileToString("adsquery.json");

  PubSubWebSocketAppDataQuery pubSubInput = new PubSubWebSocketAppDataQuery();

  CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
  TestUtils.setSink(pubSubInput.outputPort, sink);

  pubSubInput.setTopic("AdsQueryGenericResultDemo.1");
  pubSubInput.setUri(uri);
  pubSubInput.setup(null);
  pubSubInput.activate(null);

  PubSubWebSocketOutputOperator<String> pubSubOutput = new PubSubWebSocketOutputOperator<String>();
  pubSubOutput.setTopic("AdsQueryGenericDemo");
  pubSubOutput.setUri(uri);
  pubSubOutput.setup(null);

  pubSubOutput.beginWindow(0);
  pubSubInput.beginWindow(0);

  Thread.sleep(5000);

  pubSubOutput.input.put(query);

  Thread.sleep(5000);

  pubSubInput.outputPort.flush(Integer.MAX_VALUE);

  Assert.assertEquals(1, sink.collectedTuples.size());
  String resultJSON = sink.collectedTuples.get(0).toString();
  JSONObject result = new JSONObject(resultJSON);
  JSONArray array = result.getJSONArray("data");
  JSONObject val = array.getJSONObject(0);
  Assert.assertEquals(1, array.length());

  Assert.assertEquals("3.00", val.get("revenue:SUM"));
  Assert.assertEquals("5.00", val.get("cost:SUM"));
  Assert.assertEquals("10", val.get("clicks:SUM"));
  Assert.assertEquals("5", val.get("impressions:SUM"));

  pubSubInput.deactivate();

  pubSubOutput.teardown();
  pubSubInput.teardown();
}
 
Example 16
Source File: DagInfo.java    From tez with Apache License 2.0 4 votes vote down vote up
private void parseBasicVertexInfo(JSONArray verticesInfo) throws JSONException {
  if (verticesInfo == null) {
    LOG.info("No vertices available.");
    return;
  }

  //Parse basic information available in DAG for vertex and edges
  for (int i = 0; i < verticesInfo.length(); i++) {
    BasicVertexInfo basicVertexInfo = new BasicVertexInfo();

    JSONObject vJson = verticesInfo.getJSONObject(i);
    basicVertexInfo.vertexName =
        vJson.optString(Constants.VERTEX_NAME);
    JSONArray inEdges = vJson.optJSONArray(Constants.IN_EDGE_IDS);
    if (inEdges != null) {
      String[] inEdgeIds = new String[inEdges.length()];
      for (int j = 0; j < inEdges.length(); j++) {
        inEdgeIds[j] = inEdges.get(j).toString();
      }
      basicVertexInfo.inEdgeIds = inEdgeIds;
    }

    JSONArray outEdges = vJson.optJSONArray(Constants.OUT_EDGE_IDS);
    if (outEdges != null) {
      String[] outEdgeIds = new String[outEdges.length()];
      for (int j = 0; j < outEdges.length(); j++) {
        outEdgeIds[j] = outEdges.get(j).toString();
      }
      basicVertexInfo.outEdgeIds = outEdgeIds;
    }

    JSONArray addInputsJson =
        vJson.optJSONArray(Constants.ADDITIONAL_INPUTS);
    basicVertexInfo.additionalInputs = parseAdditionalDetailsForVertex(addInputsJson);

    JSONArray addOutputsJson =
        vJson.optJSONArray(Constants.ADDITIONAL_OUTPUTS);
    basicVertexInfo.additionalOutputs = parseAdditionalDetailsForVertex(addOutputsJson);

    basicVertexInfoMap.put(basicVertexInfo.vertexName, basicVertexInfo);
  }
}
 
Example 17
Source File: SolrFulltextSearchImpl.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a list of fulltext search results from a Solr json result
 * string.
 * 
 * @param solrResultsJSON
 * @param query
 * @return
 * @throws JSONException
 */
private FulltextSearchHit[] constructFulltextSearchList(
      JSONObject solrResultsJSON, FulltextSearchQuery query)
      throws JSONException {

   String searchColumn = query.getSearchField();
   String snippetColumn = query.getSnippetField();
   String scoreColumn = query.getScoreField();
   
   JSONObject resp = solrResultsJSON.getJSONObject("response");
   JSONArray docs = resp.getJSONArray("docs");

   /**
    * Collect results from JSON
    */
   List<FulltextSearchHit> searchHits =
         new ArrayList<FulltextSearchHit>(docs.length());
   for (int i = 0; i < docs.length(); i++) {

      JSONObject result = docs.getJSONObject(i);

      String search = null;
      if (searchColumn!=null && !searchColumn.isEmpty()
            && result.has(searchColumn)) {
         
         search = flattenJsonResult(result.get(searchColumn));
               
      } else {
         
         throw new FulltextSearchException(
               "Search field undefined, empty, or does not exist.");

      }

            
      String snippet = null;
      if (snippetColumn!=null && !snippetColumn.isEmpty()) {
         
         snippet = result.has(snippetColumn) ?
               flattenJsonResult(result.get(snippetColumn)) : null;
         
      }
       
      String score = null;
      if (scoreColumn!=null && !scoreColumn.isEmpty()) {

         score = result.has(scoreColumn) ?
               flattenJsonResult(result.get(scoreColumn)) : null;
         
      }
         
      Double scoreAsDouble = null;
      if (score!=null) {

         try {
            scoreAsDouble = Double.valueOf(score);
         } catch (NumberFormatException e) {
            
            if (log.isInfoEnabled()) {
               log.info("Could not cast score to double: " + score);
            }
         }
         
      }

      if (search!=null && !search.isEmpty()) {
         FulltextSearchHit currentHit = 
               new FulltextSearchHit(search, scoreAsDouble, snippet,
               query.getIncomingBindings(), query.getSearchResultType());

         searchHits.add(currentHit);
         
      }

   }

   return searchHits.toArray(new FulltextSearchHit[searchHits.size()]);
}
 
Example 18
Source File: TestRMWebServicesCapacitySched.java    From big-c with Apache License 2.0 4 votes vote down vote up
private void verifySubQueue(JSONObject info, String q, 
    float parentAbsCapacity, float parentAbsMaxCapacity)
    throws JSONException, Exception {
  int numExpectedElements = 13;
  boolean isParentQueue = true;
  if (!info.has("queues")) {
    numExpectedElements = 25;
    isParentQueue = false;
  }
  assertEquals("incorrect number of elements", numExpectedElements, info.length());

  QueueInfo qi = isParentQueue ? new QueueInfo() : new LeafQueueInfo();
  qi.capacity = (float) info.getDouble("capacity");
  qi.usedCapacity = (float) info.getDouble("usedCapacity");
  qi.maxCapacity = (float) info.getDouble("maxCapacity");
  qi.absoluteCapacity = (float) info.getDouble("absoluteCapacity");
  qi.absoluteMaxCapacity = (float) info.getDouble("absoluteMaxCapacity");
  qi.absoluteUsedCapacity = (float) info.getDouble("absoluteUsedCapacity");
  qi.numApplications = info.getInt("numApplications");
  qi.queueName = info.getString("queueName");
  qi.state = info.getString("state");

  verifySubQueueGeneric(q, qi, parentAbsCapacity, parentAbsMaxCapacity);

  if (isParentQueue) {
    JSONArray arr = info.getJSONObject("queues").getJSONArray("queue");
    // test subqueues
    for (int i = 0; i < arr.length(); i++) {
      JSONObject obj = arr.getJSONObject(i);
      String q2 = q + "." + obj.getString("queueName");
      verifySubQueue(obj, q2, qi.absoluteCapacity, qi.absoluteMaxCapacity);
    }
  } else {
    LeafQueueInfo lqi = (LeafQueueInfo) qi;
    lqi.numActiveApplications = info.getInt("numActiveApplications");
    lqi.numPendingApplications = info.getInt("numPendingApplications");
    lqi.numContainers = info.getInt("numContainers");
    lqi.maxApplications = info.getInt("maxApplications");
    lqi.maxApplicationsPerUser = info.getInt("maxApplicationsPerUser");
    lqi.userLimit = info.getInt("userLimit");
    lqi.userLimitFactor = (float) info.getDouble("userLimitFactor");
    verifyLeafQueueGeneric(q, lqi);
    // resourcesUsed and users (per-user resources used) are checked in
    // testPerUserResource()
  }
}
 
Example 19
Source File: MRJobStatusOperator.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
/**
 * This method gets the latest status of the tasks for a job from the Task Manager for jobs submitted on hadoop 1.x
 * version
 *
 * @param statusObj
 * @param type
 */
private void getJsonsForLegacyTasks(MRStatusObject statusObj, String type)
{
  try {
    JSONObject jobJson = statusObj.getJsonObject();
    int totalTasks = ((JSONObject)((JSONObject)jobJson.get(type + "TaskSummary")).get("taskStats")).getInt("numTotalTasks");
    Map<String, TaskObject> taskMap;
    if (type.equalsIgnoreCase("map")) {
      taskMap = statusObj.getMapJsonObject();
    } else {
      taskMap = statusObj.getReduceJsonObject();
    }

    int totalPagenums = (totalTasks / Constants.MAX_TASKS) + 1;
    String baseUrl = "http://" + statusObj.getUri() + ":" + statusObj.getRmPort() + "/jobtasks.jsp?type=" + type + "&format=json&jobid=job_" + statusObj.getJobId() + "&pagenum=";

    for (int pagenum = 1; pagenum <= totalPagenums; pagenum++) {

      String url = baseUrl + pagenum;
      String responseBody = MRUtil.getJsonForURL(url);

      JSONObject jsonObj = MRUtil.getJsonObject(responseBody);
      if (jsonObj == null) {
        return;
      }

      JSONArray taskJsonArray = jsonObj.getJSONArray("tasksInfo");

      for (int i = 0; i < taskJsonArray.length(); i++) {
        JSONObject taskObj = taskJsonArray.getJSONObject(i);
        {
          if (taskMap.get(taskObj.getString(Constants.LEAGACY_TASK_ID)) != null) {
            TaskObject tempReduceObj = taskMap.get(taskObj.getString(Constants.LEAGACY_TASK_ID));
            if (tempReduceObj.getJsonString().equals(taskObj.toString())) {
              // tempReduceObj.setModified(false);
              // taskMap.put(taskObj.getString(Constants.TASK_ID), tempReduceObj);
              continue;
            }
            tempReduceObj.setJson(taskObj);
            tempReduceObj.setModified(true);
            taskMap.put(taskObj.getString(Constants.TASK_ID), tempReduceObj);
            continue;

          }
          taskMap.put(taskObj.getString(Constants.LEAGACY_TASK_ID), new TaskObject(taskObj));
        }
      }
    }

    if (type.equalsIgnoreCase("map")) {
      statusObj.setMapJsonObject(taskMap);
    } else {
      statusObj.setReduceJsonObject(taskMap);
    }
  } catch (Exception e) {
    LOG.info(e.getMessage());
  }

}
 
Example 20
Source File: LogicalPlanSerializer.java    From Bats with 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;
}