經過Elasticsearch-java的API獲取到的數據格式爲json格式,以下所示java
若是獲取sum,avg值等格式會有所變化json
JSONObject obj = JSON.parseObject(esResult.getString()); // 圖一 List<Map<String ,Object>> list = new ArrayList<Map<String ,Object>>(); try { List<Map> hits = (List<Map>) PropertyUtils.getNestedProperty(obj, "hits.hits"); if(hits != null){ for(Map json : hits){ Map<String ,Object> span = new HashMap<String ,Object>(); Map _sc = (Map) json.get("_source"); span.put("T_deviceIp", _sc.get("T_deviceIp")); span.put("cpuPercent", _sc.get("cpuPercent")); list.add(span); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //圖2 JSONObject obj = JSON.parseObject(esResult.getString()); List<Map<String ,Object>> list = new ArrayList<Map<String ,Object>>(); try { List<Map> buckets = (List<Map>)PropertyUtils.getNestedProperty(obj, "aggregations.ip.buckets"); for(Map json : buckets){ Map<String ,Object> span = new HashMap<String ,Object>(); span.put("ip", json.get("key")); }