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

The following examples show how to use org.codehaus.jackson.JsonNode#has() . 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: MyqDeviceData.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Constructor of the MyqDeviceData.
 * 
 * @param rootNode
 *            The Json node returned from the myq website.
 */
public MyqDeviceData(JsonNode rootNode) throws IOException {
    if (rootNode.has("Devices")) {
        JsonNode node = rootNode.get("Devices");
        if (node.isArray()) {
            logger.debug("Chamberlain MyQ Devices:");
            int arraysize = node.size();
            for (int i = 0; i < arraysize; i++) {
                int deviceId = node.get(i).get("MyQDeviceId").asInt();
                String deviceType = node.get(i).get("MyQDeviceTypeName").asText();
                int deviceTypeId = node.get(i).get("MyQDeviceTypeId").asInt();

                // GarageDoorOpener have MyQDeviceTypeId of 2,5,7,17
                if (deviceTypeId == 2 || deviceTypeId == 5 || deviceTypeId == 7 || deviceTypeId == 17) {
                    devices.add(new GarageDoorDevice(deviceId, deviceType, deviceTypeId, node.get(i)));
                } else if (deviceTypeId == 3) { // Light have MyQDeviceTypeId of 3
                    devices.add(new LampDevice(deviceId, deviceType, deviceTypeId, node.get(i)));
                }
            }
        }
    }
}
 
Example 2
Source File: TextTupleCreator.java    From Cubert with Apache License 2.0 6 votes vote down vote up
@Override
public void setup(JsonNode json) throws IOException
{
    if (json.has("params"))
    {
        JsonNode params = json.get("params");
        if (params.has("separator"))
        {
            String str = params.get("separator").getTextValue();
            str = StringEscapeUtils.unescapeJava(str);
            byte[] bytes = str.getBytes("UTF-8");
            separator = new String(bytes);
        }
    }

    schema = new BlockSchema(json.get("schema"));
    typeArray = new DataType[schema.getNumColumns()];
    for (int i = 0; i < schema.getNumColumns(); i++)
        typeArray[i] = schema.getType(i);

    tuple = TupleFactory.getInstance().newTuple(schema.getNumColumns());
}
 
Example 3
Source File: HuobiExchange.java    From libdynticker with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	if(!pairs.contains(pair))
		throw new IOException("Invalid pair: " + pair);
	JsonNode node;
	if(pair.getExchange().equals("CNY"))
		// http://api.huobi.com/staticmarket/detail_btc_json.js
		node = readJsonFromUrl(
				"http://api.huobi.com/staticmarket/detail_" + pair.getCoin().toLowerCase() + "_json.js");
	else
		node = readJsonFromUrl(
				"http://api.huobi.com/usdmarket/detail_" + pair.getCoin().toLowerCase() + "_json.js");
	if(node.has("error"))
		throw new IOException(node.get("error").asText());
	else
		return parseTicker(node, pair);
}
 
Example 4
Source File: JsonTreeRowRecordReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
private JsonNode getChildNode(final JsonNode jsonNode, final RecordField field) {
    if (jsonNode.has(field.getFieldName())) {
        return jsonNode.get(field.getFieldName());
    }

    for (final String alias : field.getAliases()) {
        if (jsonNode.has(alias)) {
            return jsonNode.get(alias);
        }
    }

    return null;
}
 
Example 5
Source File: ItBitExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	// https://api.itbit.com/v1/markets/XBTUSD/ticker
	JsonNode node = readJsonFromUrl("https://api.itbit.com/v1/markets/" +
			pair.getCoin() + pair.getExchange() + "/ticker");
	if(node.has("error"))
		throw new IOException(node.get("error").get("message").asText());
	return parseTicker(node, pair);
}
 
Example 6
Source File: ColumnType.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public ColumnType(JsonNode json)
{
    name = getText(json, "name");
    type = DataType.valueOf(getText(json, "type").toUpperCase());
    if (json.has("schema"))
        columnSchema = new BlockSchema(json.get("schema"));
}
 
Example 7
Source File: TextStorage.java    From Cubert with Apache License 2.0 5 votes vote down vote up
@Override
public PostCondition getPostCondition(Configuration conf, JsonNode json, Path path) throws IOException
{
    JsonNode params = json.get("params");
    if (params == null || params.isNull() || !params.has("schema")
            || params.get("schema").isNull())
        throw new PlanRewriteException("Cannot infer schema of TEXT input. Please specify using the 'schema' param.");

    BlockSchema schema =
            new BlockSchema(json.get("params").get("schema").getTextValue());
    return new PostCondition(schema, null, null);
}
 
Example 8
Source File: SurBTCExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	// https://www.surbtc.com/api/v2/markets/btc-clp/ticker
	JsonNode node = readJsonFromUrl(BASE_URL + pair.getCoin().toLowerCase() + "-" +
			pair.getExchange().toLowerCase() + "/ticker");
	if(node.has("ticker"))
		return parseTicker(node, pair);
	else
		throw new NoMarketDataException(pair);
}
 
Example 9
Source File: PoloniexExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	JsonNode node = readJsonFromUrl(API_URL);
	if(node.has(pair.getExchange() + "_" + pair.getCoin())) {
		return parseTicker(node, pair);
	} else {
		throw new NoMarketDataException(pair);
	}
}
 
Example 10
Source File: BTCEExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	String pairCode = pair.getCoin().toLowerCase() + "_" + pair.getExchange().toLowerCase();
	JsonNode node = readJsonFromUrl("https://btc-e.com/api/3/ticker/" + pairCode);
	if(node.has(pairCode)) {
		return parseTicker(node, pair);
	} else {
		throw new IOException(node.get("error").asText());
	}
}
 
Example 11
Source File: VariableNameUsed.java    From Cubert with Apache License 2.0 5 votes vote down vote up
@Override
public void visitOperator(JsonNode json, boolean isMapper)
{
    if (json.has("input"))
    {
        String[] inputs = asArray(json, "input");
        for (String input : inputs)
            usedNames.add(input);
    }

    usedNames.add(getText(json, "output"));
}
 
Example 12
Source File: QuadrigaCXExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	JsonNode node = readJsonFromUrl("https://api.quadrigacx.com/v2/ticker?book=" +
			pair.getCoin() + "_" + pair.getExchange());
	if(node.has("error")) {
		throw new IOException(node.get("error").get("message").asText());
	} else {
		return parseTicker(node, pair);
	}
}
 
Example 13
Source File: AutoJsonDeserializer.java    From kafka-metrics with Apache License 2.0 5 votes vote down vote up
public List<MeasurementV1> fromBytes(byte[] bytes) {
    try {
        JsonNode node = mapper.readTree(bytes);
        if (node.has("header") && node.has("metrics") && node.get("header").has("samza-version")) {
            return samzaDecoder.fromJsonTree(node);
        } else {
            throw new RuntimeJsonMappingException("Unrecoginzed JSON metric format: " + node.asText());
        }
    } catch (IOException e) {
        throw new RuntimeException("Error deserializing json object", e);
    }
}
 
Example 14
Source File: ANXExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	// https://anxpro.com/api/2/DOGEBTC/money/ticker
	JsonNode node = readJsonFromUrl("https://anxpro.com/api/2/"
			+ pair.getCoin() + pair.getExchange() + "/money/ticker");
	if(node.has("error"))
		throw new IOException(node.get("result").asText());
	return parseTicker(node, pair);
}
 
Example 15
Source File: CubertCombiner.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public static void checkPostCondition(Map<String, PostCondition> preConditions,
                                      JsonNode json) throws PreconditionException
{
    PostCondition condition = preConditions.values().iterator().next();
    BlockSchema inputSchema = condition.getSchema();

    String[] keyColumns = JsonUtils.asArray(json, "pivotKeys");
    BlockSchema outputSchema = inputSchema.getSubset(keyColumns);

    if (json.has("aggregates"))
    {
        for (JsonNode aggregateJson : json.path("aggregates"))
        {
            AggregationType aggType =
                    AggregationType.valueOf(JsonUtils.getText(aggregateJson, "type"));
            AggregationFunction aggregator = null;
            aggregator = AggregationFunctions.get(aggType, aggregateJson);
            BlockSchema aggSchema =
                    aggregator.outputSchema(inputSchema, aggregateJson);
            outputSchema = outputSchema.append(aggSchema);
        }
    }

    if (!inputSchema.equals(outputSchema))
        throw new PreconditionException(PreconditionExceptionType.INVALID_SCHEMA,
                                        "The input and output schema for SHUFFLE must be identical."
                                                + "\n\tInput Schema: " + inputSchema
                                                + "\n\tOutputSchema: " + outputSchema);
}
 
Example 16
Source File: GroupByOperator.java    From Cubert with Apache License 2.0 4 votes vote down vote up
@Override
public PostCondition getPostCondition(Map<String, PostCondition> preConditions,
                                      JsonNode json) throws PreconditionException
{
    PostCondition condition = preConditions.values().iterator().next();
    BlockSchema inputSchema = condition.getSchema();
    String[] partitionKeys = condition.getPartitionKeys();
    String[] sortKeys = condition.getSortKeys();
    if (condition.getPivotKeys() != null)
        sortKeys = CommonUtils.concat(condition.getPivotKeys(), sortKeys);

    BlockSchema outputSchema;
    String[] groupByColumns = JsonUtils.asArray(json, GROUP_BY_COLUMNNAMES);

    // test that group by columns are present
    for (String groupByColumn : groupByColumns)
    {
        if (!inputSchema.hasIndex(groupByColumn))
            throw new PreconditionException(PreconditionExceptionType.COLUMN_NOT_PRESENT,
                                            "Column [" + groupByColumn
                                                    + "] not present.");
    }

    // test that block is sorted on group by columns
    if (groupByColumns.length > 0)
    {
        if (!CommonUtils.isPrefix(sortKeys, groupByColumns))
        {
            System.out.println("Input SortKeys = " + Arrays.toString(sortKeys));
            throw new PreconditionException(PreconditionExceptionType.INVALID_SORT_KEYS);
        }
    }
    // generate the output schema
    if (((ArrayNode) json.get(GROUP_BY_COLUMNNAMES)).size() > 0)
    {
        outputSchema = inputSchema.getSubset(groupByColumns);
    }
    else
    {
        outputSchema = new BlockSchema(new ColumnType[] {});
    }

    String[] fullExpectedSortKeys = groupByColumns;
    boolean countDistinctAggPresent = false;
    if (json.has("aggregates"))
    {
        for (JsonNode aggregateJson : json.path("aggregates"))
        {
            // BlockSchema aggOutputSchema;
            AggregationType aggType =
                    AggregationType.valueOf(JsonUtils.getText(aggregateJson, "type"));
            AggregationFunction aggregator = null;
            aggregator = AggregationFunctions.get(aggType, aggregateJson);
            if (aggregator == null)
                throw new PreconditionException(PreconditionExceptionType.INVALID_CONFIG,
                                                "Cannot instantiate aggregation operator for type "
                                                        + aggType);

            BlockSchema aggOutputSchema =
                    aggregator.outputSchema(inputSchema, aggregateJson);

            outputSchema = outputSchema.append(aggOutputSchema);

            // Check pre-condition for COUNT-DISTINCT

            String[] measureColumn = JsonUtils.asArray(aggregateJson.get("input"));

            if (aggType == AggregationType.COUNT_DISTINCT)
            {

                if (countDistinctAggPresent)
                    throw new PreconditionException(PreconditionExceptionType.INVALID_GROUPBY);
                countDistinctAggPresent = true;
                fullExpectedSortKeys =
                        CommonUtils.concat(groupByColumns, measureColumn);
                if (!CommonUtils.isPrefix(sortKeys, fullExpectedSortKeys))
                {
                    String errorMesg =
                            "Expecting sortkeys = "
                                    + CommonUtils.join(fullExpectedSortKeys, ",")
                                    + " actual = " + CommonUtils.join(sortKeys, ",");
                    throw new PreconditionException(PreconditionExceptionType.INVALID_SORT_KEYS,
                                                    errorMesg);
                }
            }

        }
    }

    return new PostCondition(outputSchema, partitionKeys, groupByColumns);
}
 
Example 17
Source File: JsonUtil.java    From tac2015-event-detection with GNU General Public License v3.0 4 votes vote down vote up
public static String getTextDefault(JsonNode ob, String keyname, String defaultValue) {
	return ob.has(keyname) ? ob.get(keyname).asText() : defaultValue;
}
 
Example 18
Source File: PNetGenerationCommand.java    From workcraft with MIT License 4 votes vote down vote up
public static void initParse(String args) throws IOException {
    JsonFactory f = new MappingJsonFactory();
    JsonParser jp = f.createJsonParser(new File(args));

    JsonToken current;

    current = jp.nextToken();
    if (current != JsonToken.START_OBJECT) {
        LogUtils.logError("Root should be object: quiting.");
        return;
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        // move from field name to field value
        current = jp.nextToken();

        if ("NETWORK".equals(fieldName)) {
            if (current == JsonToken.START_ARRAY) {
                // For each of the records in the array
                System.out.println("Generate CPNs");
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    JsonNode node = jp.readValueAsTree();
                    String idName = node.get("id").toString();
                    String idName1 = "";
                    String idName2 = "";
                    String idNamep = "";
                    String idNamep1 = "";
                    String idNamep2 = "";
                    String typeName = node.get("type").toString();
                    //System.out.println("id: " + idName + "type: " + typeName);
                    lst2.add(new Ids(idName, typeName));
                    JsonNode y = node.get("outs");
                    if (y != null) {
                        for (int i = 0; y.has(i); i++) {
                            if (y.get(i).has("id")) {
                                if (i == 0) {
                                    idName1 = y.get(i).get("id").toString();
                                    idNamep1 = y.get(i).get("in_port").toString();
                                    if ("xfork".equals(typeName)) {
                                        lst.add(new Info(idName1, idName, "b", idNamep1));
                                    } else if ("xswitch".equals(typeName)) {
                                        lst.add(new Info(idName1, idName, "a", idNamep1));
                                    } else {
                                        lst.add(new Info(idName1, idName, "", idNamep1));
                                    }
                                    if (idName1.contains("Sync")) {
                                        //System.out.println("id: " + idName + "sync: " + idName1);
                                        slsti.add(new Info(idName, idName1, "", idNamep1));   //swapped order slsti slsto
                                    }
                                    //add o based on order of i or reverse?
                                    if (idName.contains("Sync")) {
                                        slsto2.add(new Info(idName1, idName, "", idNamep1));
                                    }
                                } else if (i == 1) {
                                    idName2 = y.get(i).get("id").toString();
                                    idNamep2 = y.get(i).get("in_port").toString();
                                    if ("xfork".equals(typeName)) {
                                        lst.add(new Info(idName2, idName, "a", idNamep2));
                                    } else if ("xswitch".equals(typeName)) {
                                        lst.add(new Info(idName2, idName, "b", idNamep2));
                                    } else {
                                        lst.add(new Info(idName2, idName, "", idNamep2));
                                    }
                                    if (idName2.contains("Sync")) slsti.add(new Info(idName, idName2, "", idNamep2));
                                    if (idName.contains("Sync")) {
                                        slsto2.add(new Info(idName2, idName, "", idNamep2));
                                    }
                                } else {
                                    idName1 = y.get(i).get("id").toString();
                                    idNamep = y.get(i).get("in_port").toString();
                                    if (idName.contains("Sync")) {
                                        slsto2.add(new Info(idName, idName1, "", idNamep));
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                LogUtils.logError("Records should be an array: skipping.");
                jp.skipChildren();
            }
        } else {
            //System.out.println("Unprocessed property: " + fieldName);
            jp.skipChildren();
        }
    }
}
 
Example 19
Source File: DictionaryDecodeOperator.java    From Cubert with Apache License 2.0 4 votes vote down vote up
@Override
public void setInput(Map<String, Block> input, JsonNode json, BlockProperties props) throws IOException,
        InterruptedException
{
    // Get the dictionary
    Map<String, CodeDictionary> dictionaryMap = null;
    if (json.has("path"))
    {
        // load the dictionary from file
        String dictionaryName = json.get("path").getTextValue();
        String dictionaryPath = FileCache.get(dictionaryName);
        dictionaryPath = dictionaryPath + "/part-r-00000.avro";
        dictionaryMap =
                GenerateDictionary.loadDictionary(dictionaryPath, false, null);
    }
    else
    {
        // this is inline dictionary
        JsonNode dictionary = json.get("dictionary");

        Iterator<String> nameIterator = dictionary.getFieldNames();
        dictionaryMap = new HashMap<String, CodeDictionary>();
        while (nameIterator.hasNext())
        {
            String name = nameIterator.next();
            ArrayNode values = (ArrayNode) dictionary.get(name);
            CodeDictionary codeDictionary = new CodeDictionary();
            for (JsonNode value : values)
            {
                codeDictionary.addKey(value.getTextValue());
            }
            dictionaryMap.put(name, codeDictionary);
        }
    }

    dataBlock = input.values().iterator().next();
    BlockSchema inputSchema = dataBlock.getProperties().getSchema();
    numColumns = inputSchema.getNumColumns();

    decodedTuple = TupleFactory.getInstance().newTuple(numColumns);

    // create dictionary array
    dictionaries = new CodeDictionary[numColumns];

    for (int i = 0; i < numColumns; i++)
    {
        String colName = inputSchema.getName(i);

        if (dictionaryMap.containsKey(colName))
        {
            dictionaries[i] = dictionaryMap.get(colName);
        }
        else
        {
            dictionaries[i] = null;
        }
    }

    if (json.has("replaceUnknownCodes"))
    {
        replaceUnknownCodes = JsonUtils.getText(json, "replaceUnknownCodes");
    }
}
 
Example 20
Source File: CreateBlockOperator.java    From Cubert with Apache License 2.0 4 votes vote down vote up
@Override
public void setInput(Configuration conf, Map<String, Block> input, JsonNode json) throws IOException,
        InterruptedException
{
    if (input.size() == 0)
        throw new IllegalArgumentException("No input block is provided");

    if (input.size() != 1)
        throw new IllegalArgumentException("This operator operates on only one input block. ("
                + input.size() + " provided)");

    this.inputJson = json;
    Block block = input.values().iterator().next();

    if (block == null)
        throw new IllegalArgumentException("The specified block for ["
                + input.keySet().iterator().next() + "] is null");

    blockgenType =
            BlockgenType.valueOf(JsonUtils.getText(json, "blockgenType")
                                          .toUpperCase());
    if (json.has("blockgenValue"))
    {
        blockgenValue = json.get("blockgenValue").getLongValue();
    }
    partitionKeys = JsonUtils.asArray(json.get("partitionKeys"));
    // if (json.get("originalPartitionKeys") != null)
    // originalPartitionKeys = JsonUtils.asArray(json.get("originalPartitionKeys"));

    if (json.has("pivotKeys"))
        sortKeys = JsonUtils.asArray(json, "pivotKeys");

    // if sort keys are prefix of partitionkeys. or vice versa, there is no need to
    // sort the block
    if (CommonUtils.isPrefix(partitionKeys, sortKeys)
            || CommonUtils.isPrefix(sortKeys, partitionKeys))
        sortKeys = null;

    inputBlock = new PivotedBlock(block, partitionKeys);

    if (blockgenType == BlockgenType.BY_INDEX)
    {
        if (PhaseContext.isMapper())
        {
            throw new RuntimeException("Expecting Reduce Context while performing LOAD BLOCK");
        }
        nReducers = conf.getInt("mapred.reduce.tasks", -1);
        if (nReducers < 0)
            throw new RuntimeException("Unable to determine number of reducers.");
        reducerId =
                PhaseContext.getRedContext().getTaskAttemptID().getTaskID().getId();
        retrieveRelevantBlockIds(json);
    }
}