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. 如何有效防止API的重放攻击(转自阿里云)

    API重放攻击(Replay Attacks)又称重播攻击.回放攻击,这种攻击会不断恶意或欺诈性地重复一个有效的API请求.攻击者利用网络监听或者其他方式盗取API请求,进行一定的处理后,再把它重新发 ...

  2. SSIS ->> 管理和维护SSISDB

    Comming soon!!! 参考文献: Setup and Performance Issues with the Integration Services (SSIS) 2012 Catalog ...

  3. 树的各种操作java

    package mystudy; import java.io.UnsupportedEncodingException; import java.util.LinkedList; import ja ...

  4. Java不带.classpath的svn项目下载,转成到eclipse中

    .classpath是Eclipse的工程文件,别人没有将工程的信息传到SVN库中,就检查不出.classpath文件,识别不了项目结构. 这种做法也是比较提倡的方法.SVN上只要有项目的源码信息就可 ...

  5. Can't create new folder in windows7

    First, please use System File Checker tool to troubleshoot(诊断) this issue. If the issue persists, im ...

  6. python csv写入数据,消除空行

    import csv rowlist=[{'first_name': 'mark', 'last_name': 'zhao','age':21}, {'first_name': 'tony', 'la ...

  7. webpack学习(五)配置详解

    配置详解 //使用插件html-webpack-plugin打包合并html //使用插件extract-text-webpack-plugin打包独立的css //使用UglifyJsPlugin压 ...

  8. 基于SAP Kyma的订单编排增强介绍

    尽管有一万个舍不得,2018年还是无可挽回地离我们远去了. 唯有SAP成都研究院的同事和我去年在网络上留下的这些痕迹,能证明2018年我们曾经很认真地去度过每一天: SAP成都研究院2018年总共87 ...

  9. xampp安装及使用时的问题总结

    本文主要介绍易错点,具体安装过程可参考链接1 1.首先要以管理员身份运行,否则报错. 2.如果你的网站首页名字不是index,那么你在访问的时候就必须输入你首页的全称. 3.htdocs就是网站的根目 ...

  10. Python re模块正则表达式