一:准备

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分析的更多相关文章

  1. 关于网站的UV分析

    一:准备 1.统计的维度 guid tracktime provice 2.key与value的设定 key:date+provice_guid value:NullWritable 3.案例分析 表 ...

  2. 对石家庄铁道大学网站UI的分析

         作为我们团队的PM,老师对我们提出了一些额外的要求,所以我发表这篇博客来谈一下对石家庄铁道大学网站UI的分析.      首先,PM 对项目所有功能的把握, 特别是UI.最差的UI, 体现了 ...

  3. 基于React的PC网站前端架构分析

    代码地址如下:http://www.demodashi.com/demo/12252.html 本文适合对象 有过一定开发经验的初级前端工程师: 有过完整项目的开发经验,不论大小: 对node有所了解 ...

  4. 【转】LAMP网站架构方案分析【精辟】

    [转]LAMP网站架构方案分析[精辟] http://www.cnblogs.com/mo-beifeng/archive/2011/09/13/2175197.html Xubuntu下LAMP环境 ...

  5. WEB网站前端性能分析相关

    现在的网站分析工具,数据源主要有两种,分别是服务器日志(WebServer Log)和采集日志 买 优化要根据具体的业务和场景来分析 埋点:监控用户点击的每一步

  6. Web 网站 故障常用分析命令

    系统连接状态篇: 1.查看TCP连接状态 netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ { ...

  7. 对TSNU网站的初步分析

    这是一个教育网的网站,其中网站的教务子系统,html页面是用表单嵌套来格式化显示样式的,另外还有<div>.<iframe>等等 .在这个上有个form表单,提交的参数有如下几 ...

  8. hadoop实例-网站用户行为分析

    一.数据集 网站用户购物行为数据集2030万条,包括raw_user.csv(2000万条)和small_user.csv(30万条,适合新手) 字段说明: user_id 用户编号,item_id ...

  9. 网站jcms流程分析

    本实例大致流程:基于jsp页面,通过servlet传递数据调用方法,利用service更改数据库.本文重点分析的是其中的两个小方法add()和delete(),来反映出反射机制的一个具体作用:减少Se ...

随机推荐

  1. JavaScript之12306自动刷新车票[待完善]

    function refresh(){ var search_btn = document.getElementById("query_ticket"); var result_t ...

  2. luogu P1344 [USACO4.4]追查坏牛奶Pollutant Control

    传送门 要求断掉某些边使得两个点不连通,显然是最小割 但是要求选的边数尽量少,,, 可以考虑修改边权(容量),即把边权\(c\)改成\(c*(m+1)+1\) 没了 // luogu-judger-e ...

  3. 第14月第23天 uitextfield文字下移

    1. http://www.jianshu.com/p/641a0cbcabb0

  4. rem,em

    任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: 1em=16px.那么12px=0.75em,10px=0.625em.为了简化font-size的换算,需要在css中的body选择 ...

  5. iOS视频开发经验

    iOS视频开发经验 手机比PC的优势除了便携外,我认为最重要的就是可以快速方便的创作多媒体作品.照片分享,语音输入,视频录制,地理位置.一个成功的手机APP从产品形态上都有这其中的一项或多项,比如in ...

  6. ubuntu14.04 安装Kdevelop 进行ROS开发

    1. 安装gcc sudo apt-get build-dep gcc sudo apt-get install build-essential  2. 安装Kdevelop sudo apt-get ...

  7. elasticsearch5.0.1安装 marvel 插件

    elasticsearch5.0.1安装 marvel 插件 1.在elasticsearch上安装x-pach插件 在elasticsearch的根目录(每个节点),运行 bin/elasticse ...

  8. 非root用户执行java进程报错:fork: retry:资源暂时不可用

    vim /etc/security/limits.conf # End of file *           soft   nproc        65535 *           hard   ...

  9. 转载:磁盘目录(1.3.3)《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19614.html 要使用Nginx,还需要在Linux文件系统上准备以下目录. (1)Nginx源代码存放目录 该目录用于放置从官网 ...

  10. python-pandas 高级功能(通过学习kaggle案例总结)

    方法.iterrows()遍历循环df中的元素. for index,row in df.iterrows(): pass 更改df一个元素中的变量值. data1.set_value(index,' ...