036 关于网站的UV分析
一:准备
1.统计的维度
guid
tracktime
provice
2.key与value的设定
key:date+provice_guid
value:NullWritable
3.案例分析
表示某天某个省份的某个人无论访问网站多少次,仅仅记做一次访问统计
UV:统计页面访问的总人数---》userID对于用户进行去重
二:程序
1.map程序
2.reduce程序
3.结果
4.理解点
1)怎么去重
数据key的形式:date+provice_guid。
当guid是相同的时候,在shuffle的group分组时,key被分组,一起的放在一起,而value则是nullwritable,没有使用value。
所以到达reduce的时候,数据已经被去重了。
2)NullWritable.get()
使用反射,获得NullWritable的对象。
5.完整程序
package com.senior.network; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class WebUvCount extends Configured implements Tool{
//Mapper
public static class WebUvCountMapper extends Mapper<LongWritable,Text,Text,NullWritable>{
private Text mapoutputkey=new Text();
@Override
protected void cleanup(Context context) throws IOException,InterruptedException { }
@Override
protected void setup(Context context) throws IOException,InterruptedException { } @Override
protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {
String lineValue=value.toString();
String[] strs=lineValue.split("\t");
if(30>strs.length){
context.getCounter("webPvMapper_counter", "length_LT_30").increment(1L);
return;
}
String guidValue=strs[5]; //
if(StringUtils.isEmpty(guidValue)){
return;
}
String trackTimeValue=strs[17];
if(StringUtils.isEmpty(trackTimeValue)){
return;
}
String dateVAlue=trackTimeValue.substring(0,13);//
String priviceIdValue=strs[23]; Integer priviceId=Integer.MAX_VALUE;
try{
priviceId = Integer.valueOf(priviceIdValue); //
}catch(Exception e){
return;
} mapoutputkey.set(dateVAlue+"\t"+priviceIdValue+"_"+guidValue);
context.write(mapoutputkey,NullWritable.get());
} } //Reducer
public static class WebUvCountReducer extends Reducer<Text,NullWritable,Text,IntWritable>{
private Text outputkey=new Text();
private Map<String,Integer> dateMap;
private IntWritable outputvalue=new IntWritable(); @Override
protected void setup(Context context)throws IOException, InterruptedException {
dateMap=new HashMap<String,Integer>();
} @Override
protected void reduce(Text key, Iterable<NullWritable> values,Context context)throws IOException, InterruptedException {
String date=key.toString().split("_")[0];
if(dateMap.containsKey(date)){
Integer previousUV=dateMap.get(date);
Integer uv=previousUV+1;
dateMap.put(date, uv);
}else{
dateMap.put(date, 1);
}
} @Override
protected void cleanup(Context context)throws IOException, InterruptedException {
Set<String> dateSet=dateMap.keySet();
for(String date:dateSet){
Integer uv=dateMap.get(date);
outputkey.set(date);
outputvalue.set(uv);
context.write(outputkey, outputvalue);
}
} } //Driver
public int run(String[] args)throws Exception{
Configuration conf=this.getConf();
Job job=Job.getInstance(conf,this.getClass().getSimpleName());
job.setJarByClass(WebUvCount.class);
//input
Path inpath=new Path(args[0]);
FileInputFormat.addInputPath(job, inpath); //output
Path outpath=new Path(args[1]);
FileOutputFormat.setOutputPath(job, outpath); //map
job.setMapperClass(WebUvCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class); //shuffle //reduce
job.setReducerClass(WebUvCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //submit
boolean isSucess=job.waitForCompletion(true);
return isSucess?0:1;
} //main
public static void main(String[] args)throws Exception{
Configuration conf=new Configuration();
//compress
conf.set("mapreduce.map.output.compress", "true");
conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");
args=new String[]{
"hdfs://linux-hadoop01.ibeifeng.com:8020/user/beifeng/mapreduce/wordcount/inputWebData",
"hdfs://linux-hadoop01.ibeifeng.com:8020/user/beifeng/mapreduce/wordcount/outputWebData6"
};
int status=ToolRunner.run(new WebUvCount(), args);
System.exit(status);
} }
036 关于网站的UV分析的更多相关文章
- 关于网站的UV分析
一:准备 1.统计的维度 guid tracktime provice 2.key与value的设定 key:date+provice_guid value:NullWritable 3.案例分析 表 ...
- 对石家庄铁道大学网站UI的分析
作为我们团队的PM,老师对我们提出了一些额外的要求,所以我发表这篇博客来谈一下对石家庄铁道大学网站UI的分析. 首先,PM 对项目所有功能的把握, 特别是UI.最差的UI, 体现了 ...
- 基于React的PC网站前端架构分析
代码地址如下:http://www.demodashi.com/demo/12252.html 本文适合对象 有过一定开发经验的初级前端工程师: 有过完整项目的开发经验,不论大小: 对node有所了解 ...
- 【转】LAMP网站架构方案分析【精辟】
[转]LAMP网站架构方案分析[精辟] http://www.cnblogs.com/mo-beifeng/archive/2011/09/13/2175197.html Xubuntu下LAMP环境 ...
- WEB网站前端性能分析相关
现在的网站分析工具,数据源主要有两种,分别是服务器日志(WebServer Log)和采集日志 买 优化要根据具体的业务和场景来分析 埋点:监控用户点击的每一步
- Web 网站 故障常用分析命令
系统连接状态篇: 1.查看TCP连接状态 netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ { ...
- 对TSNU网站的初步分析
这是一个教育网的网站,其中网站的教务子系统,html页面是用表单嵌套来格式化显示样式的,另外还有<div>.<iframe>等等 .在这个上有个form表单,提交的参数有如下几 ...
- hadoop实例-网站用户行为分析
一.数据集 网站用户购物行为数据集2030万条,包括raw_user.csv(2000万条)和small_user.csv(30万条,适合新手) 字段说明: user_id 用户编号,item_id ...
- 网站jcms流程分析
本实例大致流程:基于jsp页面,通过servlet传递数据调用方法,利用service更改数据库.本文重点分析的是其中的两个小方法add()和delete(),来反映出反射机制的一个具体作用:减少Se ...
随机推荐
- flash GC
所有应用程序都要管理内存.应用程序的内存管理包括用于确定何时分配内存,分配多少内存,何时将内容放入回收站,以及何时清空回收站的准则.MMgc是 Flash Player用于几乎所有内存分配工作的通用内 ...
- node版本管理工具nvm安装以及使用
curl命令安装 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash 或者 使用wg ...
- 【文件】java生成PDF文件
package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...
- python 错误--UnboundLocalError: local variable '**' referenced before assignment
val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...
- 关于《汇编语言(王爽)》程序6.3使用16个dw 0的问题
在学习王爽老师<汇编语言>的第6.2节时,在程序6.3代码中,给出了如下的代码: assume cs:code code segment dw 0123h, 0456h, 0789h, 0 ...
- python - 计算器 程序练习
v1.0 计算器(数据内不含括号方式:) import re def jisuan(a,b,c): sun_count = 0 if c =="+": sun_count = st ...
- Faster rcnn代码理解(1)
这段时间看了不少论文,回头看看,感觉还是有必要将Faster rcnn的源码理解一下,毕竟后来很多方法都和它有相近之处,同时理解该框架也有助于以后自己修改和编写自己的框架.好的开始吧- 这里我们跟着F ...
- /etc/fstab文件详解【转】
******************************************************************************* 有很多人经常修改/etc/fstab文件 ...
- eclipse引入系统类库
引入系统类库1.第一步:项目,右键,build path,remove报错的类库 2.第二步:项目,右键,build path→Add Library→JRE System Library,Next3 ...
- 用javascript判断当前是安卓平台还是ios平台
通常判断运行环境都是通过navigator.userAgent if (/android/gi.test(navigator.userAgent)){ // todo : android} if (/ ...