Java Code Examples for org.codehaus.jackson.JsonNode#getFields()
The following examples show how to use
org.codehaus.jackson.JsonNode#getFields() .
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: JsonCredentialStore.java From incubator-gobblin with Apache License 2.0 | 6 votes |
/** * Instantiate a new keystore using the file at the provided path */ public JsonCredentialStore(Path path, KeyToStringCodec codec) throws IOException { credentials = new HashMap<>(); FileSystem fs = path.getFileSystem(new Configuration()); try (InputStream in = fs.open(path)) { ObjectMapper jsonParser = defaultMapper; JsonNode tree = jsonParser.readTree(in); if (!tree.isObject()) { throw new IllegalArgumentException("Json in " + path.toString() + " is not an object!"); } Iterator<Map.Entry<String, JsonNode>> it = tree.getFields(); while (it.hasNext()) { Map.Entry<String, JsonNode> field = it.next(); String keyId = field.getKey(); byte[] key = codec.decodeKey(field.getValue().getTextValue()); credentials.put(keyId, key); } } log.info("Initialized keystore from {} with {} keys", path.toString(), credentials.size()); }
Example 2
Source File: GeoJsonParser.java From arcgis-runtime-demo-java with Apache License 2.0 | 6 votes |
private Map<String, Object> parseProperties(JsonNode node) { Map<String, Object> properties = new HashMap<String, Object>(); Iterator<Map.Entry<String, JsonNode>> propertyInterator = node.getFields(); while (propertyInterator.hasNext()) { Map.Entry<String, JsonNode> property = propertyInterator.next(); JsonNode jsonValue = property.getValue(); if (jsonValue.isInt()) { properties.put(property.getKey(), property.getValue().asInt()); } else if (jsonValue.isDouble()) { properties.put(property.getKey(), property.getValue().asDouble()); } else if (jsonValue.isTextual()) { properties.put(property.getKey(), property.getValue().asText()); } } return properties; }
Example 3
Source File: QANARY.java From chatbot with Apache License 2.0 | 5 votes |
public QAService.Data search(String question) throws Exception { QAService.Data data = new QAService.Data(); String response = makeRequest(question); if(response != null) { ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(response); JsonNode answers = mapper.readTree(rootNode.findValue("questions").get(0).get("question").get("answers").getTextValue()); if (answers != null) { JsonNode bindings = answers.get("results").get("bindings"); for(JsonNode binding : bindings) { Iterator<Map.Entry<String, JsonNode>> nodes = binding.getFields(); while (nodes.hasNext()) { Map.Entry<String, JsonNode> entry = nodes.next(); JsonNode value = entry.getValue(); switch(value.get("type").getTextValue()) { case "uri": data.addURI(value.get("value").getTextValue()); break; case "typed-literal": data.addLiteral(value.get("value").getTextValue()); break; } } } } } return data; }
Example 4
Source File: GenericRecordLogAppender.java From lsmtree with Apache License 2.0 | 5 votes |
private static Map<String, String> readMetadata(File metadataPath, ObjectMapper mapper) throws IOException { if (metadataPath.exists()) { Map<String, String> ret = Maps.newLinkedHashMap(); JsonNode node = mapper.readTree(Files.toString(metadataPath, Charsets.UTF_8)); Iterator<Map.Entry<String,JsonNode>> iterator = node.getFields(); while (iterator.hasNext()) { Map.Entry<String, JsonNode> entry = iterator.next(); ret.put(entry.getKey(), entry.getValue().getTextValue()); } return ret; } return null; }
Example 5
Source File: JsonSchemaInference.java From nifi with Apache License 2.0 | 5 votes |
@Override protected void forEachFieldInRecord(final JsonNode rawRecord, final BiConsumer<String, JsonNode> fieldConsumer) { final Iterator<Map.Entry<String, JsonNode>> itr = rawRecord.getFields(); while (itr.hasNext()) { final Map.Entry<String, JsonNode> entry = itr.next(); final String fieldName = entry.getKey(); final JsonNode value = entry.getValue(); fieldConsumer.accept(fieldName, value); } }
Example 6
Source File: QueryElasticsearchHttp.java From localization_nifi with Apache License 2.0 | 4 votes |
private int getPage(final Response getResponse, final URL url, final ProcessContext context, final ProcessSession session, FlowFile flowFile, final ComponentLog logger, final long startNanos, boolean targetIsContent) throws IOException { List<FlowFile> page = new ArrayList<>(); final int statusCode = getResponse.code(); if (isSuccess(statusCode)) { ResponseBody body = getResponse.body(); final byte[] bodyBytes = body.bytes(); JsonNode responseJson = parseJsonResponse(new ByteArrayInputStream(bodyBytes)); JsonNode hits = responseJson.get("hits").get("hits"); for(int i = 0; i < hits.size(); i++) { JsonNode hit = hits.get(i); String retrievedId = hit.get("_id").asText(); String retrievedIndex = hit.get("_index").asText(); String retrievedType = hit.get("_type").asText(); FlowFile documentFlowFile = null; if (flowFile != null) { documentFlowFile = targetIsContent ? session.create(flowFile) : session.clone(flowFile); } else { documentFlowFile = session.create(); } JsonNode source = hit.get("_source"); documentFlowFile = session.putAttribute(documentFlowFile, "es.id", retrievedId); documentFlowFile = session.putAttribute(documentFlowFile, "es.index", retrievedIndex); documentFlowFile = session.putAttribute(documentFlowFile, "es.type", retrievedType); if (targetIsContent) { documentFlowFile = session.putAttribute(documentFlowFile, "filename", retrievedId); documentFlowFile = session.putAttribute(documentFlowFile, "mime.type", "application/json"); documentFlowFile = session.write(documentFlowFile, out -> { out.write(source.toString().getBytes()); }); } else { Map<String, String> attributes = new HashMap<>(); for(Iterator<Entry<String, JsonNode>> it = source.getFields(); it.hasNext(); ) { Entry<String, JsonNode> entry = it.next(); attributes.put(ATTRIBUTE_PREFIX + entry.getKey(), entry.getValue().asText()); } documentFlowFile = session.putAllAttributes(documentFlowFile, attributes); } page.add(documentFlowFile); } logger.debug("Elasticsearch retrieved " + responseJson.size() + " documents, routing to success"); session.transfer(page, REL_SUCCESS); } else { try { // 5xx -> RETRY, but a server error might last a while, so yield if (statusCode / 100 == 5) { throw new RetryableException(String.format("Elasticsearch returned code %s with message %s, transferring flow file to retry. This is likely a server problem, yielding...", statusCode, getResponse.message())); } else if (context.hasIncomingConnection()) { // 1xx, 3xx, 4xx -> NO RETRY throw new UnretryableException(String.format("Elasticsearch returned code %s with message %s, transferring flow file to failure", statusCode, getResponse.message())); } else { logger.warn("Elasticsearch returned code {} with message {}", new Object[]{statusCode, getResponse.message()}); } } finally { if (!page.isEmpty()) { session.remove(page); page.clear(); } } } // emit provenance event final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); if (!page.isEmpty()) { if (context.hasNonLoopConnection()) { page.forEach(f -> session.getProvenanceReporter().fetch(f, url.toExternalForm(), millis)); } else { page.forEach(f -> session.getProvenanceReporter().receive(f, url.toExternalForm(), millis)); } } return page.size(); }
Example 7
Source File: AutoJsonDeserializer.java From kafka-metrics with Apache License 2.0 | 4 votes |
public List<MeasurementV1> fromJsonTree(JsonNode node) { List<MeasurementV1> result = new LinkedList<MeasurementV1>(); JsonNode header = node.get("header"); String version = header.get("version").getTextValue(); if (version.equals("0.0.1")) { Long timestamp = header.get("time").getLongValue(); Map<String, String> commonTags = new HashMap<String, String>(); Iterator<Map.Entry<String, JsonNode>> tagFields = header.getFields(); while (tagFields.hasNext()) { Map.Entry<String, JsonNode> tagField = tagFields.next(); String tagKey = tagField.getKey(); if (tagKey.equals("time")) continue; if (tagKey.equals("reset-time")) continue; if (tagKey.equals("version")) continue; commonTags.put(tagField.getKey(), tagField.getValue().getTextValue()); } Iterator<Map.Entry<String, JsonNode>> metricFields = node.get("metrics").getFields(); while (metricFields.hasNext()) { Map.Entry<String, JsonNode> metricField = metricFields.next(); String name = metricField.getKey(); //group by identical tags Map<Map<String, String>, Map<String, Double>> points = new HashMap<Map<String, String>, Map<String, Double>>(); Iterator<Map.Entry<String, JsonNode>> fieldValues = metricField.getValue().getFields(); while (fieldValues.hasNext()) { Map.Entry<String, JsonNode> field = fieldValues.next(); Double value = formatter.anyValueToDouble(field.getValue().getNumberValue()); Map<String, String> tags = new HashMap<String, String>(commonTags); if (value != null) { String fieldName = tagSamzaMetricField(name, field.getKey(), tags); if (fieldName != null) { Map<String, Double> fields = points.get(tags); if (fields == null) { fields = new HashMap<String, Double>(); points.put(tags, fields); } fields.put(fieldName, value); } } } for(Map.Entry<Map<String, String>, Map<String, Double>> e: points.entrySet()) { MeasurementV1 measurement = new MeasurementV1(); measurement.setName(name); measurement.setTimestamp(timestamp); measurement.setTags(e.getKey()); measurement.setFields(e.getValue()); result.add(measurement); } } } else { log.warn("Unsupported Samza Metrics JSON Format Version " + version); } return result; }
Example 8
Source File: ActivityJsonToAvroInterceptor.java From big-data-lite with MIT License | 4 votes |
private Activity getActivityRecord(byte[] eventBody) throws IOException, JsonProcessingException { // Create an Activity object and populate Activity activity = new Activity(); // Parse the activity node and update the record JsonNode jsonNode = objectMapper.readTree(new String(eventBody)); Map.Entry<String,JsonNode> nodeMap = null; Iterator <Map.Entry<String,JsonNode>> iterator = jsonNode.getFields(); while (iterator.hasNext()) { nodeMap = iterator.next(); // Populate Activity record from the Json source switch (nodeMap.getKey().toLowerCase()) { case "activity": activity.setActivity(nodeMap.getValue().getValueAsInt()); break; case "custid": activity.setCustid(nodeMap.getValue().getValueAsInt()); break; case "movieid": activity.setMovieid(nodeMap.getValue().getValueAsInt()); break; case "genreid": activity.setGenreid(nodeMap.getValue().getValueAsInt()); break; case "position": activity.setPosition(nodeMap.getValue().getValueAsInt()); break; case "price": activity.setPrice(nodeMap.getValue().getValueAsDouble()); break; case "rating": activity.setRating(nodeMap.getValue().getValueAsInt()); break; case "recommended": activity.setRecommended(nodeMap.getValue().getValueAsText()); break; case "time": activity.setTime(nodeMap.getValue().getValueAsText()); break; } } // while loop return activity; }
Example 9
Source File: GenericServiceAPIResponseEntityDeserializer.java From Eagle with Apache License 2.0 | 4 votes |
@Override public GenericServiceAPIResponseEntity deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { GenericServiceAPIResponseEntity entity = new GenericServiceAPIResponseEntity(); ObjectCodec objectCodec = jp.getCodec(); JsonNode rootNode = jp.getCodec().readTree(jp); if(rootNode.isObject()){ Iterator<Map.Entry<String,JsonNode>> fields = rootNode.getFields(); JsonNode objNode = null; while(fields.hasNext()){ Map.Entry<String,JsonNode> field = fields.next(); if (META_FIELD.equals(field.getKey()) && field.getValue() != null) entity.setMeta(objectCodec.readValue(field.getValue().traverse(), Map.class)); else if(SUCCESS_FIELD.equals(field.getKey()) && field.getValue() != null){ entity.setSuccess(field.getValue().getValueAsBoolean(false)); }else if(EXCEPTION_FIELD.equals(field.getKey()) && field.getValue() != null){ entity.setException(field.getValue().getTextValue()); }else if(TYPE_FIELD.endsWith(field.getKey()) && field.getValue() != null){ try { entity.setType(Class.forName(field.getValue().getTextValue())); } catch (ClassNotFoundException e) { throw new IOException(e); } }else if(OBJ_FIELD.equals(field.getKey()) && field.getValue() != null){ objNode = field.getValue(); } } if(objNode!=null) { JavaType collectionType=null; if (entity.getType() != null) { collectionType = TypeFactory.defaultInstance().constructCollectionType(LinkedList.class, entity.getType()); }else{ collectionType = TypeFactory.defaultInstance().constructCollectionType(LinkedList.class, Map.class); } List obj = objectCodec.readValue(objNode.traverse(), collectionType); entity.setObj(obj); } }else{ throw new IOException("root node is not object"); } return entity; }
Example 10
Source File: DerpTest.java From torrenttunes-client with GNU General Public License v3.0 | 4 votes |
public void testDerp7() { Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>(); String json = Tools.readFile(DataSources.STRINGS_FR_LOCATION()); JsonNode node = Tools.jsonToNode(json); Map<String, String> innerMap = new HashMap<String, String>(); // Iterate over all the string fields JsonNode s = node.get("strings"); Iterator<Entry<String, JsonNode>> sIt = s.getFields(); while (sIt.hasNext()) { Entry<String, JsonNode> e = sIt.next(); innerMap.put(e.getKey(), e.getValue().asText()); log.info(e.getValue().asText()); } map.put("strings", innerMap); }