Hadoop.2.x_WebUV示例
一、网站基本指标(即针对于网站用户行为而产生的日志中进行统计分析)
1. PV:网页浏览量(Page View页面浏览次数,只要进入该网页就产生一条记录,不限IP,统计点每天(较多)/每周/每月/..)
2. UV:独立访客数(Unique Vistor,以Cookie为依据,同一天内一个用户多次访问,只记为一个)
3. VV:访客的访问次数(Visit View,以Session为依据,访客访问网站到关掉该网站所有页面即记为一次访问)
4. IP:独立IP数(即记录不同IP,同一IP访问多次算作一次)
5. 通常网站流量(traffic)是指网站的访问量,是用来描述访问一个网站的用户数量以及用户所浏览的网页数量等指标
对于虚拟空间商来说流量是指:用户在访问网站过程中,产生的数据量大小
二、UV统计示例(也就是每天每个省份有多少人访问了该网站)
1. 分析需求
1> 我们得到的是怎样的数据,找出共同点,会议map,shuffle,reduce都做了什么事
2> 我们想要怎样的数据,列举出来
2. 实施计划注意的地方
1> 数据以什么分隔,是否我们需要自定义数据类型
2> 大致上我们需要过滤掉无效的记录
使用自定义数据类型将我们需要的字段组合起来
然后根据省份对记录累加(去重阶段)
3> 数据类型可以不定义,使用Text将字段值组合即可
然后转到reduce方法,建立hashMap对于数据根据时间和省份递增存储
在cleanup中组合成想要的输出数据即可
三、UV统计代码示例
WebUvMr.java
============
package com.bigdata_senior.WebUvMr;
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.fs.Path;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WebUvMr { //Mapper Class
private static class WordCountMapper extends Mapper<LongWritable, Text, Text, NullWritable>{ private Text mapOutKey = new Text();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { String lineValue = value.toString();
String [] strValue = lineValue.split("\t");
if(30 > strValue.length){
return;
}
String guidIdValue = strValue[5];
if(StringUtils.isBlank(guidIdValue)){
return;
}
String trackTimeValue = strValue[17];
if(StringUtils.isBlank(trackTimeValue)){
return;
}
String dateValue = trackTimeValue.substring(0,10);
Integer proviceIdValue = Integer.MAX_VALUE;
try{
if(StringUtils.isBlank(strValue[23])){
return;
}
proviceIdValue = Integer.valueOf(strValue[23]);
}catch(Exception e){
return;
} mapOutKey.set(dateValue+"\t"+proviceIdValue+"_"+guidIdValue);
//System.out.println("key--> "+mapOutKey+" value--> "+NullWritable.get());
context.write(mapOutKey, NullWritable.get());
}
} //Reduce Class
private static class WordCountReduce extends Reducer<Text, NullWritable, Text, LongWritable>{ private Map<String,Integer> dateMap;
private Text outputKey = new Text();
private LongWritable outputvalue = new LongWritable(); @Override
protected void setup(Context context) throws IOException,
InterruptedException {
dateMap = new HashMap<String,Integer>();
} @Override
public void reduce(Text key, Iterable<NullWritable> values,Context context)
throws IOException, InterruptedException { String date = key.toString().split("_")[0];
if(dateMap.containsKey(date)){ Integer preUv = dateMap.get(date);
//System.out.println("====-> "+preUv);
Integer uv = preUv + 1;
dateMap.put(date, uv);
}else{
dateMap.put(date, 1);
}
//System.out.println(dateMap.toString());
} @Override
protected void cleanup(Context context) throws IOException,
InterruptedException { Set<String> dateSet = dateMap.keySet();
//System.out.println(dateSet.toString());
for(String date : dateSet){
Integer uv = dateMap.get(date);
outputKey.set(date);
outputvalue.set(uv);
System.out.println("result:-->key "+outputKey+" value-->"+outputvalue);
context.write(outputKey, outputvalue);
}
}
} //Driver
public int run(String[] args) throws Exception { Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration, this.getClass().getSimpleName());
job.setJarByClass(this.getClass()); //input
Path inPath = new Path(args[0]);
FileInputFormat.addInputPath(job,inPath); //output
Path outPath = new Path(args[1]);
FileOutputFormat.setOutputPath(job, outPath); //mapper
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class); //Reduce
job.setReducerClass(WordCountReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class); //submit job
boolean isSuccess = job.waitForCompletion(true); return isSuccess ? 0 : 1;
} public static void main(String[] args) throws Exception { args = new String[]{
"hdfs://hadoop09-linux-01.ibeifeng.com:8020/user/liuwl/tmp/webuv/input",
"hdfs://hadoop09-linux-01.ibeifeng.com:8020/user/liuwl/tmp/webuv/output4"
};
//run job
int status = new WebUvMr().run(args);
System.exit(status);
}
}
Hadoop.2.x_WebUV示例的更多相关文章
- 【爬坑】运行 Hadoop 的 MapReduce 示例卡住了
1. 问题说明 在以伪分布式模式运行 Hadoop 自带的 MapReduce 示例,卡在了 Running job ,如图所示 2. 解决过程 查看日志没得到有用的信息 再次确认配置信息没有错误信息 ...
- Hadoop Map/Reduce 示例程序WordCount
#进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...
- hadoop: hdfs API示例
利用hdfs的api,可以实现向hdfs的文件.目录读写,利用这一套API可以设计一个简易的山寨版云盘,见下图: 为了方便操作,将常用的文件读写操作封装了一个工具类: import org.apach ...
- Hadoop - WordCount代码示例
文章来源:http://www.itnose.net/detail/6197823.html import java.io.IOException; import java.util.Iterator ...
- Hadoop应用程序示例2
- Hadoop应用程序示例
- Hadoop: MapReduce2的几个基本示例
1) WordCount 这个就不多说了,满大街都是,网上有几篇对WordCount的详细分析 http://www.sxt.cn/u/235/blog/5809 http://www.cnblogs ...
- Hadoop示例程序WordCount编译运行
首先确保Hadoop已正确安装及运行. 将WordCount.java拷贝出来 $ cp ./src/examples/org/apache/hadoop/examples/WordCount.jav ...
- 实训任务02:Hadoop基础操作
实训任务02:Hadoop基础操作 班级 学号 姓名 实训1:创建测试文件上传HDFS,并显示内容 需求说明: 在本地计算机上创建测试文件helloH ...
随机推荐
- HDU 5875 Function st + 二分
Function Problem Description The shorter, the simpler. With this problem, you should be convinced ...
- Excel动态合并行、合并列
背景: 在北京工作的时候,又一次同事问了我这样一个问题,说我要把从数据库获取到的数据直接通过NPOI进行导出,但是我对导出的格式要特殊的要求,如图: 冥思苦想,最终顺利帮同事解决问题,虽然有点瑕疵,但 ...
- How Kafka’s Storage Internals Work
In this post I'm going to help you understand how Kafka stores its data. I've found understanding th ...
- WINDOWS系统下四叶草CLOVER引导U盘制作
一.所需软件1.下载BootDiskUtility链接:http://pan.baidu.com/s/1bwBFyu 密码:glyk 2.下载Clover r3330 USB链接:http://pan ...
- 解决Eclipse建Maven项目module无法转换为2.3
Maven项目在Project Facets里面修改Dynamic web module为2.3的时候就会出现Cannot change version of project facet Dynami ...
- C语言补码作用
补码主要是为了cpu运算器在进行减法运算时避免借位而设立的. 在早期,cpu中的运算器部分,只要实现一个加法器就可以完成四由算术运算. 因为计算机中的数值编码是有限位数的,所以减法实际上相当于加上减数 ...
- 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)
引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...
- SpringMyBatis解析2-SqlSessionFactoryBean
通过分析整合示例中的配置文件,我们可以知道配置的bean其实是成树状结构的,而在树的最顶层是类型为org.mybatis.spring.SqlSessionFactoryBean的bean,它将其他相 ...
- poj 1273 最大流
题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...
- 解决ADT升级报错
方法一: 将https://dl-ssl.google.com/android/eclipse/ 改成 http://dl-ssl.google.com/android/eclipse/: 方法二: ...