List of usage examples for org.json.simple JSONValue parseWithException
public static Object parseWithException(String s) throws ParseException
From source file:nl.uva.sne.disambiguators.Wikipedia.java
protected Set<String> getTitles(String lemma) throws ParseException, UnsupportedEncodingException, IOException, JWNLException { String URLquery = lemma.replaceAll("_", " "); URLquery = URLEncoder.encode(URLquery, "UTF-8"); //sroffset=10 URL url = new URL( page + "?action=query&format=json&redirects&list=search&srlimit=500&srsearch=" + URLquery); System.err.println(url);/*from w ww .ja v a 2 s .co m*/ String jsonString = IOUtils.toString(url); Set<String> titles = new TreeSet<>(); JSONObject jsonObj = (JSONObject) JSONValue.parseWithException(jsonString); JSONObject query = (JSONObject) jsonObj.get("query"); JSONArray search = (JSONArray) query.get("search"); if (search != null) { for (Object o : search) { JSONObject res = (JSONObject) o; String title = (String) res.get("title"); // System.err.println(title); if (title != null && !title.toLowerCase().contains("(disambiguation)")) { // if (title != null) { title = title.replaceAll("%(?![0-9a-fA-F]{2})", "%25"); title = title.replaceAll("\\+", "%2B"); title = java.net.URLDecoder.decode(title, "UTF-8"); title = title.replaceAll("_", " ").toLowerCase(); lemma = java.net.URLDecoder.decode(lemma, "UTF-8"); lemma = lemma.replaceAll("_", " "); // String stemTitle = SemanticUtils.stem(title); // String stemLema = SemanticUtils.stem(lemma); // String shorter, longer; // if (stemTitle.length() > stemLema.length()) { // shorter = stemLema; // longer = stemTitle; // } else { // shorter = stemTitle; // longer = stemLema; // } // int dist = edu.stanford.nlp.util.StringUtils.editDistance(stemLema, stemTitle); // if (longer.contains(shorter) && dist <= 10) { titles.add(title); // } } } } titles.add(lemma); return titles; }
From source file:nl.uva.sne.disambiguators.WikipediaOnline.java
private Set<Term> getCandidateTerms(String jsonString, String originalTerm) throws ParseException, IOException, MalformedURLException, InterruptedException, ExecutionException { Set<Term> terms = new HashSet<>(); Set<Term> termsToReturn = new HashSet<>(); JSONObject jsonObj = (JSONObject) JSONValue.parseWithException(jsonString); JSONObject query = (JSONObject) jsonObj.get("query"); JSONObject pages = (JSONObject) query.get("pages"); Set<String> keys = pages.keySet(); for (String key : keys) { JSONObject jsonpage = (JSONObject) pages.get(key); Term t = TermFactory.create(jsonpage, originalTerm); if (t != null) { terms.add(t);// w w w.j a v a 2 s. com } } if (terms.size() > 0) { Map<String, List<String>> cats = getCategories(terms); for (Term t : terms) { boolean add = true; List<String> cat = cats.get(t.getUID()); t.setCategories(cat); for (String g : t.getGlosses()) { if (g != null && g.contains("may refer to:")) { Set<Term> referToTerms = getReferToTerms(g, originalTerm); if (referToTerms != null) { for (Term rt : referToTerms) { String url = "https://en.wikipedia.org/?curid=" + rt.getUID(); rt.setUrl(url); termsToReturn.add(rt); } } add = false; break; } } if (add) { String url = "https://en.wikipedia.org/?curid=" + t.getUID(); t.setUrl(url); termsToReturn.add(t); } } } return termsToReturn; }
From source file:org.apache.cassandra.tools.SSTableImport.java
/** * Convert a JSON formatted file to an SSTable. * //from w w w . j a v a 2 s . com * @param jsonFile the file containing JSON formatted data * @param keyspace keyspace the data belongs to * @param cf column family the data belongs to * @param ssTablePath file to write the SSTable to * @throws IOException for errors reading/writing input/output * @throws ParseException for errors encountered parsing JSON input */ public static void importJson(String jsonFile, String keyspace, String cf, String ssTablePath) throws IOException, ParseException { ColumnFamily cfamily = ColumnFamily.create(keyspace, cf); String cfType = cfamily.type(); // Super or Standard IPartitioner<?> partitioner = DatabaseDescriptor.getPartitioner(); DataOutputBuffer dob = new DataOutputBuffer(); try { JSONObject json = (JSONObject) JSONValue.parseWithException(new FileReader(jsonFile)); SSTableWriter writer = new SSTableWriter(ssTablePath, json.size(), partitioner); List<DecoratedKey<?>> decoratedKeys = new ArrayList<DecoratedKey<?>>(); for (String key : (Set<String>) json.keySet()) decoratedKeys.add(partitioner.decorateKey(key)); Collections.sort(decoratedKeys); for (DecoratedKey<?> rowKey : decoratedKeys) { if (cfType.equals("Super")) addToSuperCF((JSONObject) json.get(rowKey.key), cfamily); else addToStandardCF((JSONArray) json.get(rowKey.key), cfamily); ColumnFamily.serializer().serializeWithIndexes(cfamily, dob); writer.append(rowKey, dob); dob.reset(); cfamily.clear(); } writer.closeAndOpenReader(); } catch (ClassCastException cce) { throw new RuntimeException("Invalid JSON input, or incorrect column family.", cce); } }
From source file:org.apache.felix.http.itest.SessionHandlingTest.java
private JSONObject getJSONResponse(final CloseableHttpClient client, final String path) throws IOException, ParseException { final HttpGet httpGet = new HttpGet(createURL(path).toExternalForm().toString()); CloseableHttpResponse response1 = client.execute(httpGet); try {/*from w w w.ja v a 2 s .c o m*/ HttpEntity entity1 = response1.getEntity(); final String content = EntityUtils.toString(entity1); return (JSONObject) JSONValue.parseWithException(content); } finally { response1.close(); } }
From source file:org.apache.storm.daemon.StormCommon.java
@SuppressWarnings("unchecked") public static Map<String, Object> componentConf(Object component) { try {/* ww w. jav a 2s . co m*/ Map<String, Object> conf = new HashMap<>(); ComponentCommon common = getComponentCommon(component); String jconf = common.get_json_conf(); if (jconf != null) { conf.putAll((Map<String, Object>) JSONValue.parseWithException(jconf)); } return conf; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.storm.kafka.monitor.KafkaOffsetLagUtil.java
private static Map<String, List<TopicPartition>> getLeadersAndTopicPartitions( OldKafkaSpoutOffsetQuery oldKafkaSpoutOffsetQuery) throws Exception { Map<String, List<TopicPartition>> result = new HashMap<>(); // this means that kafka spout was configured with StaticHosts hosts (leader for partition) if (oldKafkaSpoutOffsetQuery.getPartitions() != null) { String[] partitions = oldKafkaSpoutOffsetQuery.getPartitions().split(","); String[] leaders = oldKafkaSpoutOffsetQuery.getLeaders().split(","); for (int i = 0; i < leaders.length; ++i) { if (!result.containsKey(leaders[i])) { result.put(leaders[i], new ArrayList<TopicPartition>()); }/* w w w . j av a 2s .co m*/ result.get(leaders[i]).add( new TopicPartition(oldKafkaSpoutOffsetQuery.getTopic(), Integer.parseInt(partitions[i]))); } } else { // else use zk nodes to figure out partitions and leaders for topics i.e. ZkHosts CuratorFramework curatorFramework = null; try { String brokersZkNode = oldKafkaSpoutOffsetQuery.getBrokersZkPath(); if (!brokersZkNode.endsWith("/")) { brokersZkNode += "/"; } String topicsZkPath = brokersZkNode + "topics"; curatorFramework = CuratorFrameworkFactory.newClient(oldKafkaSpoutOffsetQuery.getZkServers(), 20000, 15000, new RetryOneTime(1000)); curatorFramework.start(); List<String> topics = new ArrayList<>(); if (oldKafkaSpoutOffsetQuery.isWildCardTopic()) { List<String> children = curatorFramework.getChildren().forPath(topicsZkPath); for (String child : children) { if (child.matches(oldKafkaSpoutOffsetQuery.getTopic())) { topics.add(child); } } } else { topics.add(oldKafkaSpoutOffsetQuery.getTopic()); } for (String topic : topics) { String partitionsPath = topicsZkPath + "/" + topic + "/partitions"; List<String> children = curatorFramework.getChildren().forPath(partitionsPath); for (int i = 0; i < children.size(); ++i) { byte[] leaderData = curatorFramework.getData().forPath(partitionsPath + "/" + i + "/state"); Map<Object, Object> value = (Map<Object, Object>) JSONValue .parseWithException(new String(leaderData, "UTF-8")); Integer leader = ((Number) value.get("leader")).intValue(); byte[] brokerData = curatorFramework.getData().forPath(brokersZkNode + "ids/" + leader); Map<Object, Object> broker = (Map<Object, Object>) JSONValue .parseWithException(new String(brokerData, "UTF-8")); String host = (String) broker.get("host"); Integer port = ((Long) broker.get("port")).intValue(); String leaderBroker = host + ":" + port; if (!result.containsKey(leaderBroker)) { result.put(leaderBroker, new ArrayList<TopicPartition>()); } result.get(leaderBroker).add(new TopicPartition(topic, i)); } } } finally { if (curatorFramework != null) { curatorFramework.close(); } } } return result; }
From source file:org.apache.storm.kafka.monitor.KafkaOffsetLagUtil.java
private static Map<String, Map<Integer, Long>> getOldConsumerOffsetsFromZk( Map<String, List<Integer>> topicPartitions, OldKafkaSpoutOffsetQuery oldKafkaSpoutOffsetQuery) throws Exception { Map<String, Map<Integer, Long>> result = new HashMap<>(); CuratorFramework curatorFramework = CuratorFrameworkFactory .newClient(oldKafkaSpoutOffsetQuery.getZkServers(), 20000, 15000, new RetryOneTime(1000)); curatorFramework.start();/* w w w . j a va2 s .co m*/ String partitionPrefix = "partition_"; String zkPath = oldKafkaSpoutOffsetQuery.getZkPath(); if (zkPath.endsWith("/")) { zkPath = zkPath.substring(0, zkPath.length() - 1); } if (curatorFramework.checkExists().forPath(zkPath) == null) { throw new IllegalArgumentException( OPTION_ZK_COMMITTED_NODE_LONG + " '" + zkPath + "' dose not exists."); } byte[] zkData; try { if (topicPartitions != null) { for (Map.Entry<String, List<Integer>> topicEntry : topicPartitions.entrySet()) { Map<Integer, Long> partitionOffsets = new HashMap<>(); for (Integer partition : topicEntry.getValue()) { String path = zkPath + "/" + (oldKafkaSpoutOffsetQuery.isWildCardTopic() ? topicEntry.getKey() + "/" : "") + partitionPrefix + partition; if (curatorFramework.checkExists().forPath(path) != null) { zkData = curatorFramework.getData().forPath(path); Map<Object, Object> offsetData = (Map<Object, Object>) JSONValue .parseWithException(new String(zkData, "UTF-8")); partitionOffsets.put(partition, (Long) offsetData.get("offset")); } } result.put(topicEntry.getKey(), partitionOffsets); } } } finally { if (curatorFramework != null) { curatorFramework.close(); } } return result; }
From source file:org.apache.storm.trident.drpc.ReturnResultsReducer.java
@Override public void complete(ReturnResultsState state, TridentCollector collector) { // only one of the multireducers will receive the tuples if (state.returnInfo != null) { String result = JSONValue.toJSONString(state.results); Map retMap = null;/*from ww w . j ava 2 s .c o m*/ try { retMap = (Map) JSONValue.parseWithException(state.returnInfo); } catch (ParseException e) { collector.reportError(e); return; } final String host = (String) retMap.get("host"); final int port = ObjectReader.getInt(retMap.get("port")); String id = (String) retMap.get("id"); DistributedRPCInvocations.Iface client; if (local) { client = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(host); } else { List server = new ArrayList() { { add(host); add(port); } }; if (!_clients.containsKey(server)) { try { _clients.put(server, new DRPCInvocationsClient(conf, host, port)); } catch (TTransportException ex) { throw new RuntimeException(ex); } } client = _clients.get(server); } try { client.result(id, result); } catch (AuthorizationException aze) { collector.reportError(aze); } catch (TException e) { collector.reportError(e); } } }
From source file:org.apache.storm.utils.Utils.java
public static Map<String, Object> readCommandLineOpts() { Map<String, Object> ret = new HashMap<>(); String commandOptions = System.getProperty("storm.options"); if (commandOptions != null) { /*//w w w . ja v a2 s. c o m Below regex uses negative lookahead to not split in the middle of json objects '{}' or json arrays '[]'. This is needed to parse valid json object/arrays passed as options via 'storm.cmd' in windows. This is not an issue while using 'storm.py' since it url-encodes the options and the below regex just does a split on the commas that separates each option. Note:- This regex handles only valid json strings and could produce invalid results if the options contain un-encoded invalid json or strings with unmatched '[, ], { or }'. We can replace below code with split(",") once 'storm.cmd' is fixed to send url-encoded options. */ String[] configs = commandOptions.split(",(?![^\\[\\]{}]*(]|}))"); for (String config : configs) { config = URLDecoder.decode(config); String[] options = config.split("=", 2); if (options.length == 2) { Object val = options[1]; try { val = JSONValue.parseWithException(options[1]); } catch (ParseException ignored) { //fall back to string, which is already set } ret.put(options[0], val); } } } return ret; }
From source file:org.apache.storm.utils.Utils.java
public static Map<String, Object> fromCompressedJsonConf(byte[] serialized) { try {/*w w w.j a v a 2s . c o m*/ ByteArrayInputStream bis = new ByteArrayInputStream(serialized); InputStreamReader in = new InputStreamReader(new GZIPInputStream(bis)); Object ret = JSONValue.parseWithException(in); in.close(); return (Map<String, Object>) ret; } catch (IOException | ParseException e) { throw new RuntimeException(e); } }