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


    返回的串

    {"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 } ]

  • 相关阅读:
    对拍源码QwQ
    BZOJ-3875: [Ahoi2014&Jsoi2014]骑士游戏(SPFA+DP)
    2017年10月19日23:31:57
    BZOJ-1064: [Noi2008]假面舞会 (综合性图论题)
    BZOJ-1002: [FJOI2007]轮状病毒(打表找规律or递推 + 高精度)
    BZOJ1397 Ural 1486 Equal squares
    BZOJ3417 Poi2013 Tales of seafaring
    BZOJ2286 [Sdoi2011消耗战
    BZOJ1370 [Baltic2003]Gang团伙
    BZOJ2530 [Poi2011]Party
  • 原文地址:https://www.cnblogs.com/-flq/p/9981449.html
Copyright © 2020-2023  润新知