package mapreduce.webpv;

 import java.io.IOException;
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.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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class WebPvMapReduce extends Configured implements Tool { // step 1: Mapper
public static class WebPvMapper extends
Mapper<LongWritable, Text, IntWritable, IntWritable> {
private IntWritable mapOutputKey = new IntWritable();
private IntWritable mapOutputValue = new IntWritable(1); @Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { // line value
String lineValue = value.toString(); // spilt
String[] values = lineValue.split("\t"); // url
String urlValue = values[1]; if (StringUtils.isBlank(urlValue)) {
// conuter
context.getCounter("WEBPVMAPPER_CUUNTERS", "URL_BLANK")
.increment(1L);
return;
} if (30 > values.length) { // conuter
context.getCounter("WEBPVMAPPER_CUUNTERS", "LENGTH_LT_30")
.increment(1L); return;
} // province id
String provinceIdValue = values[23]; if (StringUtils.isBlank(provinceIdValue)) {
// conuter
context.getCounter("WEBPVMAPPER_CUUNTERS", "PROVINCEID_BLANK")
.increment(1L);
return;
} Integer provinceId = Integer.MAX_VALUE;
try {
provinceId = Integer.valueOf(provinceIdValue);
} catch (Exception e) {
// conuter
context.getCounter("WEBPVMAPPER_CUUNTERS",
"PROVINCEID_NOT_NUMBER").increment(1L);
return;
} // map outpu key
mapOutputKey.set(provinceId); context.write(mapOutputKey, mapOutputValue);
}
} // step 2: Reducer
public static class WebPvReducer extends
Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
private IntWritable outputValue = new IntWritable(); @Override
protected void reduce(IntWritable key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
// temp sum
int sum = 0; // iterator
for (IntWritable value : values) {
sum += value.get();
} // set output
outputValue.set(sum); context.write(key, outputValue);
}
} // step 3: Driver
public int run(String[] args) throws Exception { Configuration configuration = this.getConf(); Job job = Job.getInstance(configuration, this.getClass()
.getSimpleName());
job.setJarByClass(WebPvMapReduce.class); // set job
// 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(WebPvMapper.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class); // Reducer
job.setReducerClass(WebPvReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class); // submit job -> YARN
boolean isSuccess = job.waitForCompletion(true);
return isSuccess ? 0 : 1;
} public static void main(String[] args) throws Exception { Configuration configuration = new Configuration(); args = new String[] {
"hdfs://beifeng01:8020//user/beifeng01/mapreduce/input/testdata/2015082818",
"hdfs://beifeng01:8020//user/beifeng01/mapreduce/output1" }; int status = ToolRunner.run(configuration, new WebPvMapReduce(), args); // exit program
System.exit(status);
}
}

查看结果

 $ bin/hdfs dfs -text /user/beifeng01/mapreduce/output1/pa*
1 3527
2 1672
3 511
4 325
5 776
6 661
7 95
8 80
9 183
10 93
11 135
12 289
13 264
14 374
15 163
16 419
17 306
18 272
19 226
20 2861
21 124
22 38
23 96
24 100
25 20
26 157
27 49
28 21
29 85
30 42
32 173

MapReduce清洗日志数据统计PV量的更多相关文章

  1. 利用mapreduce清洗日志内存不足问题

    package com.libc; import java.io.IOException; import java.io.UnsupportedEncodingException; import ja ...

  2. nginx日志分析及其统计PV、UV、IP

    一.nginx日志结构 nginx中access.log 的日志结构: $remote_addr 客户端地址 211.28.65.253 $remote_user 客户端用户名称 -- $time_l ...

  3. 基于WebForm+EasyUI的业务管理系统形成之旅 -- 数据统计(Ⅳ)

    上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 首页快捷方式>,主要介绍通过添加首页快捷方式,快速进入各个应用菜单功能. 将常用的菜单功能作为快捷方式,避免由于寻找诸多 ...

  4. Git 常用命令和统计代码量

    摘要 分享Git日常操作中常用的命令,分享如何统计在项目中贡献的代码量. 下面列出Git bash常用命令. 1. git clone **(项目地址) 克隆一个git项目到本地,将git项目拉取到本 ...

  5. 使用mapreduce对日志进行清洗

    网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:当前页面 网站日志分析项目案例 ...

  6. 有关“数据统计”的一些概念 -- PV UV VV IP跳出率等

    有关"数据统计"的一些概念 -- PV UV VV IP跳出率等 版权声明:本文为博主原创文章,未经博主允许不得转载. 此文是本人工作中碰到的,随时记下来的零散概念,特此整理一下. ...

  7. 视频网站数据MapReduce清洗及Hive数据分析

    一.需求描述 利用MapReduce清洗视频网站的原数据,用Hive统计出各种TopN常规指标: 视频观看数 Top10 视频类别热度 Top10 视频观看数 Top20 所属类别包含这 Top20 ...

  8. 登录日志的访问日志的 统计 MapReduce

    登录日志的访问日志的 统计    MapReduce <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-commo ...

  9. mapreduce清洗数据

    继上篇 MapReduce清洗数据 package mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Confi ...

随机推荐

  1. 【系统】在windows中追加/删除虚拟打印机

    由于项目需要在windwos系统中添加多台虚拟打印机(能够正常打印出纸),查找了一下系统函数. 使用 rundll32 printui.dll,PrintUIEntry,在CMD中运行,在弹出框中得到 ...

  2. web.config如何实现301跳转

    .htaccess的301定向非常简单,那么web.config的301定向又应该怎么实现呢? 先来看下,web.config中的301格式 <?xml version="1.0&qu ...

  3. 设计模式:备忘录(Memento)模式

    设计模式:备忘录(Memento)模式 一.前言   备忘录模式用于保存和恢复对象的状态,相信大家看过我前面的拙作就会想到原型模式也能保存一个对象在某一个时刻的状态,那么两者有何不同的呢?原型模式保存 ...

  4. GoAccess分析Nginx日志详解

    一.为了提高 GoAccess 分析准确度,建议配置 nginx.conf 的 log_format 项如下: log_format main '$remote_addr - $remote_user ...

  5. 019sys模块

    为了和python解释器交互,控制台执行脚本后面添加变量 import  sysprint(sys.argv) def  post():    print('upload')def  download ...

  6. Python之条件判断和循环(入门4)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407755.html 本文出自:[Edwin博客园] Python之条件判断和循环 1. Python之if ...

  7. IntelliJ IDEA 运行你的第一个Java应用程序

    IntelliJ IDEA 运行你的第一个Java应用程序创建项目让我们创建一个简单的Java Hello World项目. 单击创建新的项目. 打开新建项目向导.你应该注意的主要是项目的SDK.SD ...

  8. jemter多种方式查看结果树及正则的使用

    最近才发现jemter结果是有一种正则表达式匹配的显示方式,以前直接在TEXT下显示和来匹配正则,真是费时间,使用方式如下: 默认使用TEXT方式显示: 显示方式有以下几种: RegExp Teste ...

  9. IBM MQ介绍

    转自:http://hi.baidu.com/lubezhang/blog/item/bd308b3b7ecce3ec14cecb4f.html IBM MQ(IBM Message Queue)是I ...

  10. Handler的简单使用介绍

    Handler在android程序开发中使用的非常频繁.我们知道android是不允许在子线程中更新UI的,这就需要借助Handler来实现,那么你是否想过为什么一定要这个这样子做呢?而且Handle ...