zoukankan      html  css  js  c++  java
  • 字符串截取,并且计算分组数量

    返回的串

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

  • 相关阅读:
    Html-浅谈如何正确给table加边框
    如何在移动设备上调试html5开发的网页
    swiper嵌套小demo(移动端触摸滑动插件)
    移动端如何用swiper实现导航栏效果
    background-color:transparent
    点击按钮 发送短信验证码后60秒倒计时
    placeholder的样式设置
    linux 定时任务crontab
    laravel学习一
    centos 7安装jdk
  • 原文地址:https://www.cnblogs.com/-flq/p/9981449.html
Copyright © 2011-2022 走看看