List of usage examples for org.json.simple JSONObject keySet
Set<K> keySet();
From source file:org.apache.metron.enrichment.bolt.GenericEnrichmentBolt.java
@SuppressWarnings("unchecked") @Override/*from w w w . ja v a2s.c om*/ public void execute(Tuple tuple) { String key = tuple.getStringByField("key"); JSONObject rawMessage = (JSONObject) tuple.getValueByField("message"); String subGroup = ""; JSONObject enrichedMessage = new JSONObject(); enrichedMessage.put("adapter." + adapter.getClass().getSimpleName().toLowerCase() + ".begin.ts", "" + System.currentTimeMillis()); try { if (rawMessage == null || rawMessage.isEmpty()) throw new Exception("Could not parse binary stream to JSON"); if (key == null) throw new Exception("Key is not valid"); String sourceType = null; if (rawMessage.containsKey(Constants.SENSOR_TYPE)) { sourceType = rawMessage.get(Constants.SENSOR_TYPE).toString(); } else { throw new RuntimeException( "Source type is missing from enrichment fragment: " + rawMessage.toJSONString()); } boolean error = false; String prefix = null; for (Object o : rawMessage.keySet()) { String field = (String) o; Object value = rawMessage.get(field); if (field.equals(Constants.SENSOR_TYPE)) { enrichedMessage.put(Constants.SENSOR_TYPE, value); } else { JSONObject enrichedField = new JSONObject(); if (value != null) { SensorEnrichmentConfig config = getConfigurations().getSensorEnrichmentConfig(sourceType); if (config == null) { LOG.error("Unable to find SensorEnrichmentConfig for sourceType: " + sourceType); error = true; continue; } config.getConfiguration().putIfAbsent(STELLAR_CONTEXT_CONF, stellarContext); CacheKey cacheKey = new CacheKey(field, value, config); try { adapter.logAccess(cacheKey); prefix = adapter.getOutputPrefix(cacheKey); subGroup = adapter.getStreamSubGroup(enrichmentType, field); enrichedField = cache.getUnchecked(cacheKey); if (enrichedField == null) throw new Exception("[Metron] Could not enrich string: " + value); } catch (Exception e) { LOG.error(e.getMessage(), e); error = true; MetronError metronError = new MetronError() .withErrorType(Constants.ErrorType.ENRICHMENT_ERROR).withThrowable(e) .withErrorFields(new HashSet() { { add(field); } }).addRawMessage(rawMessage); ErrorUtils.handleError(collector, metronError); continue; } } if (!enrichedField.isEmpty()) { for (Object enrichedKey : enrichedField.keySet()) { if (!StringUtils.isEmpty(prefix)) { enrichedMessage.put(field + "." + enrichedKey, enrichedField.get(enrichedKey)); } else { enrichedMessage.put(enrichedKey, enrichedField.get(enrichedKey)); } } } } } enrichedMessage.put("adapter." + adapter.getClass().getSimpleName().toLowerCase() + ".end.ts", "" + System.currentTimeMillis()); if (error) { throw new Exception("Unable to enrich " + rawMessage + " check logs for specifics."); } if (!enrichedMessage.isEmpty()) { collector.emit(enrichmentType, new Values(key, enrichedMessage, subGroup)); } } catch (Exception e) { handleError(key, rawMessage, subGroup, enrichedMessage, e); } }
From source file:org.apache.metron.enrichment.utils.EnrichmentUtils.java
public static JSONObject adjustKeys(JSONObject enrichedMessage, JSONObject enrichedField, String field, String prefix) {/* w ww . j a v a2 s .co m*/ if (!enrichedField.isEmpty()) { for (Object enrichedKey : enrichedField.keySet()) { if (!StringUtils.isEmpty(prefix)) { enrichedMessage.put(field + "." + enrichedKey, enrichedField.get(enrichedKey)); } else { enrichedMessage.put(enrichedKey, enrichedField.get(enrichedKey)); } } } return enrichedMessage; }
From source file:org.apache.metron.parsing.parsers.PcapParser.java
@Override public boolean validate(JSONObject message) { List<String> requiredFields = Arrays.asList("ip_src_addr", "ip_dst_addr", "ip_protocol", "ip_src_port", "ip_dst_port"); return message.keySet().containsAll(requiredFields); }
From source file:org.apache.metron.solr.writer.SolrWriter.java
public Collection<SolrInputDocument> toDocs(Iterable<JSONObject> messages) { Collection<SolrInputDocument> ret = new ArrayList<>(); for (JSONObject message : messages) { SolrInputDocument document = new SolrInputDocument(); for (Object key : message.keySet()) { Object value = message.get(key); if (value instanceof Iterable) { for (Object v : (Iterable) value) { document.addField("" + key, v); }// w w w . j ava2 s . c o m } else { document.addField("" + key, value); } } if (!document.containsKey(Constants.GUID)) { document.addField(Constants.GUID, UUID.randomUUID().toString()); } ret.add(document); } return ret; }
From source file:org.apache.sling.sli.subcommands.List.java
@Override protected void computeJSON(Object o) { JSONObject jsonObject = (JSONObject) o; for (Object key : jsonObject.keySet()) { Object child = jsonObject.get(key); if (child instanceof JSONObject) { JSONObject node = (JSONObject) child; StringBuilder line = new StringBuilder((String) key); if (node.containsKey(TYPE)) { line.append("\t\t").append(node.get(TYPE)); }/*w w w .j a va 2 s. c o m*/ System.out.println(line.toString()); } } }
From source file:org.apache.sqoop.connector.idf.JSONIntermediateDataFormat.java
@SuppressWarnings("unchecked") private Object[] toObject(JSONObject json) { if (json == null) { return null; }//from w w w . j av a 2 s . c o m Column[] columns = schema.getColumnsArray(); Object[] object = new Object[columns.length]; Set<String> jsonKeyNames = json.keySet(); for (String name : jsonKeyNames) { Integer nameIndex = schema.getColumnNameIndex(name); Column column = columns[nameIndex]; Object obj = json.get(name); // null is a possible value if (obj == null && !column.isNullable()) { throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0005, column.getName() + " does not support null values"); } if (obj == null) { object[nameIndex] = null; continue; } switch (column.getType()) { case ARRAY: case SET: object[nameIndex] = toList((JSONArray) obj).toArray(); break; case MAP: object[nameIndex] = toMap((JSONObject) obj); break; case ENUM: case TEXT: object[nameIndex] = toText(obj.toString()); break; case BINARY: case UNKNOWN: // JSON spec is to store byte array as base64 encoded object[nameIndex] = Base64.decodeBase64(obj.toString()); break; case FIXED_POINT: object[nameIndex] = toFixedPoint(obj.toString(), column); break; case FLOATING_POINT: object[nameIndex] = toFloatingPoint(obj.toString(), column); break; case DECIMAL: object[nameIndex] = toDecimal(obj.toString(), column); break; case DATE: object[nameIndex] = toDate(obj.toString(), column); break; case TIME: object[nameIndex] = toTime(obj.toString(), column); break; case DATE_TIME: object[nameIndex] = toDateTime(obj.toString(), column); break; case BIT: object[nameIndex] = toBit(obj.toString()); break; default: throw new SqoopException(IntermediateDataFormatError.INTERMEDIATE_DATA_FORMAT_0001, "Column type from schema was not recognized for " + column.getType()); } } return object; }
From source file:org.apache.sqoop.model.ConfigUtils.java
/** * Parse given input JSON string and move it's values to given configuration * object.// w w w. j a va 2 s. c om * * @param json JSON representation of the configuration object * @param configuration ConfigurationGroup object to be filled */ @SuppressWarnings("unchecked") public static void fillValues(String json, Object configuration) { Class<?> klass = configuration.getClass(); Set<String> configNames = new HashSet<String>(); JSONObject jsonConfigs = (JSONObject) JSONValue.parse(json); for (Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); String configName = configField.getName(); // We're processing only config validations Config configAnnotation = configField.getAnnotation(Config.class); if (configAnnotation == null) { continue; } configName = getConfigName(configField, configAnnotation, configNames); try { configField.set(configuration, configField.getType().newInstance()); } catch (Exception e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } JSONObject jsonInputs = (JSONObject) jsonConfigs.get(configField.getName()); if (jsonInputs == null) { continue; } Object configValue; try { configValue = configField.get(configuration); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName, e); } for (Field inputField : configField.getType().getDeclaredFields()) { inputField.setAccessible(true); String inputName = inputField.getName(); Input inputAnnotation = inputField.getAnnotation(Input.class); if (inputAnnotation == null || jsonInputs.get(inputName) == null) { try { inputField.set(configValue, null); } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } continue; } Class<?> type = inputField.getType(); try { if (type == String.class) { inputField.set(configValue, jsonInputs.get(inputName)); } else if (type.isAssignableFrom(Map.class)) { Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = (JSONObject) jsonInputs.get(inputName); for (Object key : jsonObject.keySet()) { map.put((String) key, (String) jsonObject.get(key)); } inputField.set(configValue, map); } else if (type == Integer.class) { inputField.set(configValue, ((Long) jsonInputs.get(inputName)).intValue()); } else if (type.isEnum()) { inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName))); } else if (type == Boolean.class) { inputField.set(configValue, (Boolean) jsonInputs.get(inputName)); } else { throw new SqoopException(ModelError.MODEL_004, "Unsupported type " + type.getName() + " for input " + configName + "." + inputName); } } catch (IllegalAccessException e) { throw new SqoopException(ModelError.MODEL_005, "Issue with field " + configName + "." + inputName, e); } } } }
From source file:org.apache.tika.dl.imagerec.DL4JInceptionV3Net.java
/** * Loads the class to//from ww w. j a va 2 s . c om * * @param stream label index stream * @return Map of integer -> label name * @throws IOException when the stream breaks unexpectedly * @throws ParseException when the input doesn't contain a valid JSON map */ public Map<Integer, String> loadClassIndex(InputStream stream) throws IOException, ParseException { String content = IOUtils.toString(stream); JSONObject jIndex = (JSONObject) new JSONParser().parse(content); Map<Integer, String> classMap = new HashMap<>(); for (Object key : jIndex.keySet()) { JSONArray names = (JSONArray) jIndex.get(key); classMap.put(Integer.parseInt(key.toString()), names.get(names.size() - 1).toString()); } return classMap; }
From source file:org.apache.tika.parser.geo.topic.GeoParser.java
public HashMap<String, ArrayList<String>> searchGeoNames(ArrayList<String> locationNameEntities) throws ExecuteException, IOException { CommandLine cmdLine = new CommandLine("lucene-geo-gazetteer"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); cmdLine.addArgument("-s"); for (String name : locationNameEntities) { cmdLine.addArgument(name);/* w w w. j a v a 2 s. co m*/ } LOG.fine("Executing: " + cmdLine); DefaultExecutor exec = new DefaultExecutor(); exec.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); exec.setWatchdog(watchdog); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); int exitValue = exec.execute(cmdLine, EnvironmentUtils.getProcEnvironment()); String outputJson = outputStream.toString("UTF-8"); JSONArray json = (JSONArray) JSONValue.parse(outputJson); HashMap<String, ArrayList<String>> returnHash = new HashMap<String, ArrayList<String>>(); for (int i = 0; i < json.size(); i++) { JSONObject obj = (JSONObject) json.get(i); for (Object key : obj.keySet()) { String theKey = (String) key; JSONArray vals = (JSONArray) obj.get(theKey); ArrayList<String> stringVals = new ArrayList<String>(vals.size()); for (int j = 0; j < vals.size(); j++) { String val = (String) vals.get(j); stringVals.add(val); } returnHash.put(theKey, stringVals); } } return returnHash; }
From source file:org.bungeni.ext.integration.bungeniportal.BungeniAppConnector.java
private HashMap<String, String> parseJSonStream(String responseBody) { JSONObject jsonObject = (JSONObject) JSONValue.parse(responseBody); HashMap<String, String> jsonTokens = new HashMap<String, String>(); Set<String> keys = jsonObject.keySet(); for (String key : keys) { jsonTokens.put(key, jsonObject.get(key).toString()); }/*from w ww. ja v a 2 s . c om*/ jsonTokens.put("authorization_time", getCurrentDateTime()); return jsonTokens; }