使用hadoop 进行页面的pv uv 计算
不推荐的做法
将访问者的cookie放在hashmap中进行去重计算uv,因为当访问量大时,会将hashmap撑爆,报出
推荐做法:
java.lang.OutOfMemoryError: Java heap space
使用textpair 将cookie作为second key 进行排序,在reduce中进行判断,如果上一个cookie和当前cookie不同则uv计数器加1
reduce 部分代码:
public void reduce(TextPair key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Iterator<Text> it = values.iterator();
Long pv_wap =0L;
Long uv_wap=0L;
String tempvalue="";
String cookie_id="";
String last_wap_cookie="";
while (it.hasNext()) {
it.next();
cookie_id = key.getSecond().toString();
pv_wap++;
if(!cookie_id.equals(last_wap_cookie)){
uv_wap++;
last_wap_cookie=cookie_id;
}
}