网络日志数据session案例

此图为每个字段的含义
此图为题目需求
此图为分析

SessionBean属性   getset方法   toString方法
private String sessionId ;
    private String ip ;
    private Date date ;
    private String url ;
    private int order ;
工具类
public class MyUtils {

    public static void main(String[] args) {
        String str = getSessionId();
        System.out.println(str);
    }

    /**
     * 根据正则表达式获取指定规则的数据
     */
    public static String getStrByRex(String line, String rex) {
        String res = null;
        Pattern pattern = Pattern.compile(rex);
        Matcher m = pattern.matcher(line);
        while (m.find()) {
            res = m.group();
        }
        return res;
    }
    /**
     * 将时间格式的字符串数据转换成时间对象
     */
    public static Date parseDate(String dateStr) throws ParseException{
        Date d  = null ;
        if(dateStr!=null){
            String str = dateStr.substring(1, dateStr.length()-1);
            //将字符串  转换成时间对象
            SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss" ,Locale.US) ;
             d = format.parse(str);
        }
        return d;
    }

    /**
     * 对List中的Bean按照时间先后顺序排序
     */
    public static void sortMap(Map<String,List<SessionBean>>map){
        Set<Entry<String,List<SessionBean>>> entrySet = map.entrySet();
        for (Entry<String, List<SessionBean>> entry : entrySet) {
            String ip = entry.getKey();
            List<SessionBean> list = entry.getValue();
            //对list排序
            if(list!=null && list.size()>1){
                Collections.sort(list, new Comparator<SessionBean>() {
                    @Override
                    public int compare(SessionBean o1, SessionBean o2) {
                        // TODO Auto-generated method stub
                        return o1.getDate().before(o2.getDate())?-1:1;
                    }
                });
            }
        }

    }
    /**
     * 生成sessionID
     *   获取不重复的字符串
     */
    public static String getSessionId(){
        UUID uid = UUID.randomUUID();
        String idstr = uid.toString();
        String sessionID = idstr.replace("-", "");
        return sessionID;
    }
     /**
      * 判断两个sessionBean是否是同一个session
      */
    public  static boolean isSameSession(SessionBean preSession , SessionBean afterSession){
        boolean flag = false ;

        Date d1 = preSession.getDate();
        Date d2 = afterSession.getDate();
        //判断两个时间对象的间隔是否在30分钟以内
        long res = d2.getTime() - d1.getTime();
        if(res>= 0 && res<=(30*60*1000)){
            flag = true ;
        }
        return flag;

    }



}
测试类
public class TestMain {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("d:/data/access.log.fensi"));) {
            Map<String, List<SessionBean>> map = new HashMap<>();
            String ipRex = "(\\d{1,3}+\\.){3}\\d{1,3}";
            String timeRex = "\\[.+\\d\\]";
            String urlRegex = "(GET|POST){1}\\s(\\S)*\\s";
            String line = null;
            while ((line = br.readLine()) != null) {
                String ip = MyUtils.getStrByRex(line, ipRex);
                String dateStr = MyUtils.getStrByRex(line, timeRex);
                String url = MyUtils.getStrByRex(line, urlRegex);
                // 将数据存储在map中 key(ip) value( List<SessionBena> )
                List<SessionBean> list = map.getOrDefault(ip, new ArrayList<>());
                // 创建sessionBean 【sessionId order】
                SessionBean bean = new SessionBean();
                bean.setIp(ip);
                bean.setUrl(url);
                // 将时间字符串转成对象
                Date date = MyUtils.parseDate(dateStr);
                bean.setDate(date);
                // 将Bean添加到map中的list中去
                list.add(bean);
                // 更新map数据
                map.put(ip, list);
            }
            // 对map中的list按照时间的先后顺序排序
            MyUtils.sortMap(map);
            // 生成sessionId 和 序号
            createSessionId(map);
            Set<Entry<String,List<SessionBean>>> entrySet = map.entrySet();
            for (Entry<String, List<SessionBean>> entry : entrySet) {
                String ip = entry.getKey();
                List<SessionBean> value = entry.getValue();
                for (SessionBean sessionBean : value) {

                    System.out.println(ip+" "+sessionBean.getSessionId()+" "+sessionBean.getDate()+" "+sessionBean.getOrder());
                }

            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 生成sessionid和 序号
     * 
     * @param map
     */
    private static void createSessionId(Map<String, List<SessionBean>> map) {
        Set<Entry<String, List<SessionBean>>> entrySet = map.entrySet();
        for (Entry<String, List<SessionBean>> entry : entrySet) {
            String ip = entry.getKey();
            List<SessionBean> list = entry.getValue(); 
            //  1 list中只有一个数据    sessionid  1 
            //  2 list中有多条数据    前一个和后一个时间比较 来判断是否是同一个session
            if(list.size()==1){
                SessionBean bean = list.get(0);
                bean.setSessionId(MyUtils.getSessionId());//sessionId 不可重复
                bean.setOrder(1);
            }
            if(list.size()>1){
                for (int i = 0; i < list.size()-1; i++) {
                    //两条相邻的数据   判断这个数据是否是同一个session  时间
                    SessionBean preSession = list.get(i);
                    SessionBean afterSession = list.get(i+1);
                    //判断
                    if(MyUtils.isSameSession(preSession, afterSession)){//是
                        //给两条数据设置sessionid和编号
                        //前面的数据没有sessionId
                        if(preSession.getSessionId()==null){
                            preSession.setSessionId(MyUtils.getSessionId());
                            //afterSession.setSessionId(preSession.getSessionId());
                        }
                        afterSession.setSessionId(preSession.getSessionId());
                        if(preSession.getOrder()==0){
                            preSession.setOrder(1);
                        }
                        afterSession.setOrder(preSession.getOrder()+1);

                    }else{//不是同一个session  
                        if(preSession.getSessionId() == null){
                            preSession.setSessionId(MyUtils.getSessionId());
                        }
                        afterSession.setSessionId(MyUtils.getSessionId());

                        if(preSession.getOrder()==0){
                            preSession.setOrder(1);
                        }
                        afterSession.setOrder(1);
                    }

                }


            }

        }

    }

}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值