对web日志文件实现按照人员、行为分类

日志格式:

method,time,name

in,2015-05-06 17:37:46,Jenny1
out,2015-05-06 17:37:46,Judith1
in,2015-05-06 17:37:46,Lee1
in,2015-05-06 17:37:59,Crystal1
out,2015-05-06 17:37:59,Dale1
out,2015-05-06 17:37:59,Delia1
in,2015-05-06 17:38:00,Nelly1
out,2015-05-06 17:38:00,Olga1
out,2015-05-06 17:38:00,Penny1
in,2015-05-06 17:38:00,Florence1
out,2015-05-06 17:38:00,Giselle1
out,2015-05-06 17:38:00,Edwina1
out,2015-05-06 17:38:00,Elsa1
in,2015-05-06 17:38:00,Prima1
out,2015-05-06 17:38:00,Queena1
in,2015-05-06 17:38:00,Regina1
in,2015-05-06 17:38:00,Renee1
out,2015-05-06 17:38:00,Honey1
out,2015-05-06 17:38:00,Ingrid1
out,2015-05-06 17:38:00,Esther1
in,2015-05-06 17:38:00,Ethel1
out,2015-05-06 17:38:20,Flora1
in,2015-05-06 17:38:21,Freda1
out,2015-05-06 17:38:00,Gloria1
in,2015-05-06 17:38:00,Sabrina1
in,2015-05-06 17:38:00,Thera1
out,2015-05-06 17:38:00,Tiffany1
out,2015-05-06 17:38:00,Jodie1
in,2015-05-06 17:38:00,Judy1

要求:

计算每分钟内用户的在线人数与离线人数

思路:

1、2015-05-06 17:37:59,这样的时间可以定义为2015-05-06 17:37:00至2015-05-06 17:37:59,只要是这个区间的都将视为是17:37:00的行为

2、将时间、人员、行为 作为key,这个短时间的人员作为value,存储在Map中

处理时间类:

public static String TimeFrom(String str) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result=null;
try {
long now=df.parse(str).getTime();
long from=(now/60000)*60000;
Calendar c = Calendar.getInstance();
c.setTimeInMillis(from);

result=df.format(c.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}

读取文件,并存储key value

File file = new File("e://aa.txt");
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), "utf-8");
BufferedReader bufferread = new BufferedReader(read);
String linetxt = null;
HashMap<String, ArrayList> hashname = null;

hashname = new HashMap<String, ArrayList>();
ArrayList a=null;
while ((linetxt = bufferread.readLine()) != null) {
String[] array = linetxt.split(",");
String value = TimeUtils.TimeFrom(array[1])+":"+array[2]+":"+array[0];
if (!hashname.containsKey(value)) {
a=new ArrayList();
a.add(array[2]);
hashname.put(value, a);
} else {
a.add(array[2]);
hashname.put(value, a);
}
}
Set set =hashname.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey()+"-->"+((ArrayList) me.getValue()).size());
}
} catch (Exception e) {
e.printStackTrace();
}

原文地址:https://www.cnblogs.com/ggbond1988/p/4490934.html