Java Code Examples for org.codehaus.jackson.JsonNode#getElements()

The following examples show how to use org.codehaus.jackson.JsonNode#getElements() . 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: TopTweet.java    From flink-examples with MIT License 6 votes vote down vote up
@Override
public void flatMap(String tweetJsonStr, Collector<Tuple2<String, Integer>> collector) throws Exception {
    JsonNode tweetJson = mapper.readTree(tweetJsonStr);
    JsonNode entities = tweetJson.get("entities");
    if (entities == null) return;

    JsonNode hashtags = entities.get("hashtags");
    if (hashtags == null) return;

    for (Iterator<JsonNode> iter = hashtags.getElements(); iter.hasNext();) {
        JsonNode node = iter.next();
        String hashtag = node.get("text").getTextValue();

        if (hashtag.matches("\\w+")) {
            collector.collect(new Tuple2<>(hashtag, 1));
        }
    }
}
 
Example 2
Source File: JsonRestSourceHandler.java    From ingestion with Apache License 2.0 6 votes vote down vote up
@Override public List<Event> getEvents(String body, Map<String, String> headers) {

        List<Event> events = new ArrayList<Event>(0);
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode jsonNode = mapper.readTree(body);
            if (jsonNode.isObject()) {
                events.add(buildSingleEvent(headers, findValue(jsonNode, path)));
            }
            if (jsonNode.isArray()) {
                final Iterator<JsonNode> elements = jsonNode.getElements();
                JsonNode element;
                while (elements.hasNext()) {
                    element = elements.next();
                    events.add(buildSingleEvent(headers, findValue(element, path)));
                }
                
            }

        } catch (Exception e) {
            LOG.warn("An error ocurred while response parsing. Then content is not valid: " + body);
        }

        return events;

    }
 
Example 3
Source File: CommerceTestBase.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the PID of a service based on the factory PID.
 * 
 * @param osgiClient
 * @return The PID of the first configuration found for factory PID.
 * @throws ClientException
 */
private static String getConfigurationPid(OsgiConsoleClient osgiClient, String factoryPID) throws ClientException {
    SlingHttpResponse resp = osgiClient.doGet(CONFIGURATION_CONSOLE_URL + "/*.json");
    JsonNode json = JsonUtils.getJsonNodeFromString(resp.getContent());
    Iterator<JsonNode> it = json.getElements();
    while (it.hasNext()) {
        JsonNode config = it.next();
        JsonNode factoryId = config.get("factoryPid");
        if (factoryId != null && factoryPID.equals(factoryId.getTextValue())) {
            return config.get("pid").getTextValue();
        }
    }
    return null;
}
 
Example 4
Source File: RuleJsonDeserializer.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public List<Rule> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
	ObjectCodec oc = jp.getCodec();
       JsonNode jsonNode = oc.readTree(jp);
       Iterator<JsonNode> childrenNodesIter=jsonNode.getElements();
       List<Rule> rules=new ArrayList<Rule>();
       while(childrenNodesIter.hasNext()){
       	JsonNode childNode=childrenNodesIter.next();
       	rules.add(parseRule(jp,childNode));
       }
	return rules;
}
 
Example 5
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
public void requireThatUDFCorrectlyGeneratesAddTensorOperation() throws Exception {

    Schema schema = new Schema();
    Tuple tuple = TupleFactory.getInstance().newTuple();

    // Please refer to the tensor format documentation

    Map<String, Double> tensor = new HashMap<String, Double>() {{
        put("x:label1,y:label2,z:label4", 2.0);
        put("x:label3", 3.0);
    }};

    addToTuple("id", DataType.CHARARRAY, "123", schema, tuple);
    addToTuple("tensor", DataType.MAP, tensor, schema, tuple);

    VespaDocumentOperation docOp = new VespaDocumentOperation("docid=empty", "update-tensor-fields=tensor","operation=update");
    docOp.setInputSchema(schema);
    String json = docOp.exec(tuple);

    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.get("fields");
    JsonNode tensorValue = fields.get("tensor");
    JsonNode add = tensorValue.get("add");
    JsonNode cells = add.get("cells");
    Iterator<JsonNode> cellsIterator = cells.getElements();

    JsonNode element = cellsIterator.next();
    assertEquals("label1", element.get("address").get("x").getTextValue());
    assertEquals("label2", element.get("address").get("y").getTextValue());
    assertEquals("label4", element.get("address").get("z").getTextValue());
    assertEquals("2.0", element.get("value").toString());

    element = cellsIterator.next();
    assertEquals("label3", element.get("address").get("x").getTextValue());
    assertEquals("3.0", element.get("value").toString());
}
 
Example 6
Source File: FlattenBagOperator.java    From Cubert with Apache License 2.0 5 votes vote down vote up
private void init(JsonNode operatorJson, BlockSchema inputSchema)
{
    JsonNode exprListNode = operatorJson.get("genExpressions");
    Iterator<JsonNode> it = exprListNode.getElements();
    while (it.hasNext())
    {
        JsonNode e = it.next();
        String colName = e.get("col").getTextValue();
        int columnId = inputSchema.getIndex(colName);
        flattenColumnNameSet.add(colName);
        columnIndexArray.add(columnId);

        JsonNode flattenNode = e.get("flatten");
        if (flattenNode != null)
        {
            String ftypestr = flattenNode.getTextValue();

            flattenPositions.put(inputSchema.getIndex(colName),
                                 FlattenType.fromString(ftypestr));
            // System.out.println("Flatten column  =" + colName + " position = " +
            // inputSchema.getIndex(colName) + " type= " + ftypestr);

            odometerIterators.add(null);

            // out put column definitions:
            List<ColumnType> outputColumTypeList = new ArrayList<ColumnType>();
            outputColumnTypeMap.put(colName, outputColumTypeList);
            JsonNode outputNode = e.get("output");
            for (JsonNode j : outputNode)
            {
                String outColName = j.get("col").getTextValue();
                DataType outType = DataType.valueOf(j.get("type").getTextValue());
                outputColumTypeList.add(new ColumnType(outColName, outType));
            }

        }
    }

    this.outSchema = generateOutSchema(inputSchema);
}
 
Example 7
Source File: VirWoxExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public String parseTicker(JsonNode node, Pair pair) throws IOException {
	JsonNode data = node.get("result").get("data");
	if(data.asText().equals("null")) {
		throw new NoMarketDataException(pair);
	} else {
		Iterator<JsonNode> elements = data.getElements();
		JsonNode last = null;
		while(elements.hasNext())
			last = elements.next();
		return last.get("price").asText();
	}
}
 
Example 8
Source File: BitcoindeExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public String parseTicker(JsonNode node, Pair pair) throws IOException {
	Iterator<JsonNode> elements = node.getElements();
	if(elements.hasNext()) {
		JsonNode lastValueNode = elements.next();
		while(elements.hasNext()) {
			lastValueNode = elements.next();
		}
		return lastValueNode.get("price").asText();
	} else {
		throw new IOException("Empty trade history.");
	}
}
 
Example 9
Source File: CoinutExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected List<Pair> getPairsFromAPI() throws IOException {
	List<Pair> pairs = new ArrayList<Pair>();
	JsonNode node = readJsonFromUrl("https://coinut.com/api/assets");
	Iterator<JsonNode> elements = node.getElements();
	while(elements.hasNext()){
		String key = elements.next().asText();
		pairs.add(new Pair(key.substring(0, 3), key.substring(3, 6)));
	}
	return pairs;
}
 
Example 10
Source File: GlobalMetadataTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
public void testToJson() throws IOException {
  GlobalMetadata m = new GlobalMetadata();
  m.addTransferEncoding("foo");
  m.addTransferEncoding("bar");

  byte[] utf8 = m.toJsonUtf8();
  String parsed = new String(utf8, StandardCharsets.UTF_8);

  JsonNode root = new ObjectMapper().readTree(parsed);
  Assert.assertTrue(root.isObject());
  Iterator<JsonNode> children = root.getElements();
  int numChildren = 0;
  while (children.hasNext()) {
    children.next();
    numChildren++;
  }

  Assert.assertEquals(numChildren, 3, "expected only 3 child nodes - file, dataset, id");
  Assert.assertEquals(root.get("file").size(), 0, "expected no children in file node");
  Assert.assertTrue(root.get("id").isTextual(), "expected ID to be textual");

  JsonNode transferEncoding = root.get("dataset").get("Transfer-Encoding");
  Assert.assertEquals(transferEncoding.size(), m.getTransferEncoding().size());
  for (int i = 0; i < m.getTransferEncoding().size(); i++) {
    Assert.assertEquals(transferEncoding.get(i).getTextValue(), m.getTransferEncoding().get(i));
  }
}
 
Example 11
Source File: VespaDocumentOperationTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Test
public void requireThatUDFCorrectlyGeneratesRemoveTensorOperation() throws Exception {

    Schema schema = new Schema();
    Tuple tuple = TupleFactory.getInstance().newTuple();

    // Please refer to the tensor format documentation

    Map<String, Double> tensor = new HashMap<String, Double>() {{
        put("x:label1,y:label2,z:label4", 2.0);
        put("x:label3", 3.0);
    }};

    addToTuple("id", DataType.CHARARRAY, "123", schema, tuple);
    addToTuple("tensor", DataType.MAP, tensor, schema, tuple);

    VespaDocumentOperation docOp = new VespaDocumentOperation("docid=empty", "remove-tensor-fields=tensor","operation=update");
    docOp.setInputSchema(schema);
    String json = docOp.exec(tuple);

    ObjectMapper m = new ObjectMapper();
    JsonNode root = m.readTree(json);
    JsonNode fields = root.get("fields");
    JsonNode tensorValue = fields.get("tensor");
    JsonNode remove = tensorValue.get("remove");
    JsonNode address = remove.get("addresses");

    Iterator<JsonNode> addressIterator = address.getElements();

    JsonNode element = addressIterator.next();
    assertEquals("label1", element.get("x").getTextValue());

    element = addressIterator.next();
    assertEquals("label2", element.get("y").getTextValue());

    element = addressIterator.next();
    assertEquals("label4", element.get("z").getTextValue());

    element = addressIterator.next();
    assertEquals("label3", element.get("x").getTextValue());
}
 
Example 12
Source File: FlattenOperator2.java    From Cubert with Apache License 2.0 4 votes vote down vote up
private void init(JsonNode operatorJson, BlockSchema inputSchema)
{
    JsonNode exprListNode = operatorJson.get("genExpressions");

    Iterator<JsonNode> it = exprListNode.getElements();
    while (it.hasNext())
    {
        JsonNode expr = it.next();
        String colName = expr.get("col").getTextValue();

        int columnId = inputSchema.getIndex(colName);

        flattenColumnNameSet.add(colName);
        columnIndexArray.add(columnId);

        if (expr.has("flatten"))
        {
            final String flattenTypeStr = expr.get("flatten").getTextValue();

            flattenPositions.put(inputSchema.getIndex(colName),
                                 FlattenType.fromString(flattenTypeStr));

            odometerIterators.add(null);

            // Extract output column definition from 'expr'
            List<ColumnType> outputColumTypeList = new ArrayList<ColumnType>();
            inputColumnIndexToOutputTypes.put(columnId, outputColumTypeList);

            JsonNode outputNode = expr.get("output");
            for (JsonNode j : outputNode)
            {
                String outColName = j.get("col").getTextValue();
                DataType outType = DataType.valueOf(j.get("type").getTextValue());
                outputColumTypeList.add(new ColumnType(outColName, outType));
            }

        }
    }

    this.outSchema = generateOutSchema(inputSchema);
}