字符串截取,并且计算分组数量

返回的串

{"took":278,"timed_out":false,"_shards":{"total":21,"successful":21,"failed":0},"hits":{"total":3,"max_score":6.685612,"hits":[{"_index":"logstash-winlogbeat-2018.11.19","_type":"wineventlog","_id":"AWcpk45QxK0dR2SF4KxD","_score":6.685612,"_source":{"event_data":{"param20":"http://learn.open.com.cn/ /"}}},{"_index":"logstash-winlogbeat-2018.11.19","_type":"wineventlog","_id":"AWcpkTsTPMIFKb5e39TL","_score":6.685612,"_source":{"event_data":{"param20":"http://oldlearn.open.com.cn/LearningCenter/OnLineJob/OnLineJobScore.aspx"}}},{"_index":"logstash-winlogbeat-2018.11.19","_type":"wineventlog","_id":"AWcpk-7f2sDYtBOvrcV0","_score":6.635641,"_source":{"event_data":{"param20":"http://learn.open.com.cn/StudentCenter/OnlineJob/GetWrongQuestions?bust=1542590887467&courseid=&courseExerciseId=-999&_=1542590886727"}}}]}}

// 遍历返回的字段
List<String> list =new ArrayList<String>();
for (SearchHit hit : searchHits) {
// 注意这里和2.x不同的是使用getSource函数
Object object =hit.getSource().get("event_data");
String valueOf = String.valueOf(object);

if(valueOf.indexOf("?")!=-1){
String substring2 = valueOf.substring(9,valueOf.indexOf("?") );
list.add(substring2);
}else{
String substring = valueOf.substring(9,valueOf.length()-1 );
list.add(substring);
}

}

Map<String, Integer> map = new HashMap<String, Integer>();
for(String item: list){
//判断是否包含指定的键值
if(map.containsKey(item)){
map.put(item, map.get(item).intValue() + 1);
}else{
map.put(item, new Integer(1));
}
}
////有了Set集合就可以获取迭代器 ,先获取map集合的所有键的Set集合,keySet方法
Iterator<String> keys = map.keySet().iterator();
HashMap hashMap = new HashMap();
while(keys.hasNext()){
String key = keys.next();
hashMap.put(key, map.get(key).intValue());

}

输出:

[ { "http://learn.open.com.cn/ /": 1, "http://oldlearn.open.com.cn/LearningCenter/OnLineJob/OnLineJobScore.aspx": 1, "http://learn.open.com.cn/StudentCenter/OnlineJob/GetWrongQuestions": 1 } ]

猜你喜欢

转载自www.cnblogs.com/-flq/p/9981449.html