首页 > 代码库 > java处理获取到的Elasticsearch数据

java处理获取到的Elasticsearch数据

通过Elasticsearch-java的API获取到的数据格式为json格式,如下所示

技术分享

 

如果获取sum,avg值等格式会有所变化

技术分享

        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"));

                }

 

java处理获取到的Elasticsearch数据