实例理解mapreduce任务的串行运行过程
一、准备:
eclipse,hadoop集群
注意:为了方便测试和修改,我用的是 windows 连接hadoop集群,这样在windows 下直接就能够执行 mapreduce 任务,方便程序调试。在 windows 下执行 mapreduce 任务需要安装相关插件,可以参考
windows连接hadoop集群下执行MapReduce任务
数据下载地址:数据下载
密码:idnx
二、分析
本案例的数据来自某搜索引擎开放出来的部分搜索数据,数据格式如下:
20111230104334 966a6bf4c4ec1cc693b6e40702984235 X档案研究所TXT下载 4 2 http://www.readist.cn/book/dl024708.html
分别有6个字段:时间,id,关键词,该URL在返回结果中的排名,用户点击顺序号,点击的URL。每个字段之间用 \t 分隔。
现在需要统计独立 UID 的总数,也就是说要将 UID 去重,然后累加。用 mapreduce 编程思想可以看作先执行一个mapreduce 任务将 UID 去重,再执行一个 mapreduce 任务将去重后的数据累加。所以这里要执行两个mapreduce 任务,这两个mapreduce 任务是串行运行的。
三、实现
实现分两个部分,第一部分是对UID去重,第二部分是对去重后的UID累加。第一部分的输出结果作为第二部分的输入结果。
第一部分:
1.首先执行的是去重过程,去重的过程实际上就是wordcount 的过程,Map 函数先将输入的数据标记成 key-value 键值对,其中key 是行号,value 是这一行的值,然后设置我们要统计的列类,并对这个列进行标记,Map函数的输出结果类似于如下所示:
(966a6bf4c4ec1cc693b6e40702984232,1)
(966a6bf4c4ec1cc693b6e40702984235,1)
(966a6bf4c4ec1cc693b6e40702984235,1)...
2.Reduce 的过程是对 Map 过程的一个累加,将 value 累加在一起,得出结果。
public static class MyMapper1 extends Mapper<Object, Text, Text, IntWritable> {
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
IntWritable One = new IntWritable(1);
Text k1 = new Text("");
String line = value.toString();
String[] data = line.split("\t");
if (data != null || data.length == 6) {
String uid = data[1];
k1.set(uid);
context.write(k1, One);
}
}
}
public static class MyReduce1 extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> value, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable iw : value) {
sum += iw.get();
}
context.write(key, new IntWritable(sum));
}
}
输出结果是对UID的一个统计,这个过程实现了UID的去重,第一列就是去重后的UID,部分结果如下:
000048ad4cb133b2bb376f07356dde9e 6
00005c113b97c0977c768c13a6ffbb95 2
000064b4c0f12cfb69cb4646835c6544 1
第二部分:
1.在第一部分的中,得到了去重后的UID和数量,我们只需要第一列数据,即去重后的UID。
这部分的重点是统计去重后的UID的数量。
2.第一部分的结果输出作为第二部分的结果输入,所以在Map
过程中我们只需要将第二个字段的数据重置为 1 即可,这里定义了新的 key , key 的值是“独立uid的数量是:”,value 是 1 ,写成键值对的形式就是:(独立uid的数量是:,1)
Map 函数的输出结果是:
独立uid的数量是: 1
独立uid的数量是: 1
独立uid的数量是: 1
reduce的过程是对 Map 的输出结果的累加,即对 “1” 的累加,输出结果就是我们的最终结果:
独立uid的数量是: 13526
public static class MyMapper2 extends Mapper<Object, Text, Text, IntWritable> {
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
IntWritable One = new IntWritable(1);
Text key1=new Text("独立uid的数量是 :");
context.write(key1, One);
}
}
public static class MyReduce2 extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> value, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable iw : value) {
sum += iw.get();
}
context.write(key, new IntWritable(sum));
}
}
以上过程就是mapreduce的串行执行过程。完整程序如下:
package com.sosuo; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 UuidNumber {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job1 = new Job(conf, "dlUid");
job1.setJarByClass(UuidNumber.class);
job1.setMapperClass(MyMapper1.class);
job1.setReducerClass(MyReduce1.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job1, new Path(args[0]));
FileOutputFormat.setOutputPath(job1, new Path(args[1]));
job1.waitForCompletion(true); Job job2 = new Job(conf, "dlUid");
job2.setJarByClass(UuidNumber.class);
job2.setMapperClass(MyMapper2.class);
job2.setReducerClass(MyReduce2.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job2, new Path(args[1]));
FileOutputFormat.setOutputPath(job2, new Path(args[2]));
job2.waitForCompletion(true); } public static class MyMapper1 extends Mapper<Object, Text, Text, IntWritable> { @Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
IntWritable One = new IntWritable(1);
Text k1 = new Text("");
String line = value.toString();
String[] data = line.split("\t");
if (data != null || data.length == 6) {
String uid = data[1];
k1.set(uid);
context.write(k1, One);
}
}
} public static class MyReduce1 extends Reducer<Text, IntWritable, Text, IntWritable> { @Override
protected void reduce(Text key, Iterable<IntWritable> value, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable iw : value) {
sum += iw.get();
}
context.write(key, new IntWritable(sum)); }
} public static class MyMapper2 extends Mapper<Object, Text, Text, IntWritable> { @Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
IntWritable One = new IntWritable(1);
Text key1=new Text("独立uid的数量是:");
context.write(key1, One);
}
} public static class MyReduce2 extends Reducer<Text, IntWritable, Text, IntWritable> { @Override
protected void reduce(Text key, Iterable<IntWritable> value, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable iw : value) {
sum += iw.get();
}
context.write(key, new IntWritable(sum));
}
}
}
要注意的是输入路径和输出路径,有三个路径,其中第一个是输入路径,第二个是中间结果的保存路径,即第一个mapreduce任务的输出结果,第三个是最终结果的路径。keke~
实例理解mapreduce任务的串行运行过程的更多相关文章
- MFC【6】文件I/O和串行化
文件输入和输出(I/O)服务是所有操作系统的主要工作.Microsoft Windows提供了各种API函数用来读.写和操作磁盘文件.MFC将这些桉树和CFile类融合在面对对象的模型里.其中CFil ...
- servlet串行拦截器实现例子
至于串行过滤器有什么作用,我实在不知.我的理解是它只是说明 过滤器的串行运行方式 需求:当用户没有登录访问更新页面的时候,跳转到登录页面 1.登录页面:login.jsp <%@ page la ...
- Linux 串行终端,虚拟终端,伪终端,控制终端,控制台终端的理解
转自Linux 串行终端,虚拟终端,伪终端,控制终端,控制台终端的理解 终端:输入和输出设备(键盘 + 显示器). 串行终端:与机器的串口对应,每一个串口对应一个串行终端,串口对应的是物理终端. 虚拟 ...
- Hadoop日记Day16---命令行运行MapReduce程序
一.代码编写 1.1 单词统计 回顾我们以前单词统计的例子,如代码1.1所示. package counter; import java.net.URI; import org.apache.hado ...
- GCD网络多线程---同步运行,异步运行,串行队列,并行队列
总结:同步(无论是串行还是并行)----不又一次开辟子线程 异步(无论是串行还是并行)----开辟子线程 GCD: dispatch queue 主线程的main queue 并行队列 global ...
- iOS:GCD理解1(同步-异步、串行-并行)
1.并行-异步(ST1与ST2抢占资源) 1-1).获取 并行(全局)队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t queu ...
- iOS:GCD理解1(串行-并行、同步-异步)
1.获取并行.创建串行 队列 1-1).获取 并行(全局) 队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t global_qu ...
- iOS 多线程的简单理解(2) 队列 :串行 ,并行,MainQueue,GlobalQueue
多线程队列是装载线程任务的队形结构.(系统以先进先出的方式调度队列中的任务执行 FIFO).在GCD中有两种队列: 串行队列.并发队列. 队列 :串行队列.并发队列,全局主对列,全局并发队列 2.1. ...
- MapReduce的ReduceTask任务的运行源码级分析
MapReduce的MapTask任务的运行源码级分析 这篇文章好不容易恢复了...谢天谢地...这篇文章讲了MapTask的执行流程.咱们这一节讲解ReduceTask的执行流程.ReduceTas ...
随机推荐
- js转换字符串为数值的方法
在js读取文本框或者其他表单数据的时候获得的值是字符串类型的,比如两个文本框a和b,假设获得a的value值为11,b的value值为9 ,那么a.value要小于b.value,由于他们都是字符串形 ...
- xgboost安装指南(win10,win7 64位)
---恢复内容开始--- Win7 64位系统下安装XGBoost 1. 环境介绍 计算机系统:win7 64位 Xgboost版本:xgboost0.6 2. 依赖软件环境 1) python 64 ...
- 深入理解计算机系统chapter1
---恢复内容开始--- 预处理器+编译器+汇编器+链接器=编译系统 运行hello程序 操作系统: 无论是在单核还是多核系统中,一个CPU看上去都在并发的执行多个进程,这是通过处理器在进程间切换来实 ...
- Codeforces Round #410 (Div. 2)C. Mike and gcd problem
题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...
- Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of n ...
- Python自学笔记-关于切片(来自廖雪峰的官网Python3)
感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 切片 L[0: ...
- c#使用GDI+简单绘图(二)
// Create the in-memory bitmap where you will draw the image. // This bitmap is 300 pixels wide and ...
- jquery定时刷新数据
$(function () { setInterval("startRequest()", 3000); }); function startRequest() { $(" ...
- 实用的Jquery选项卡TAB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Windows下编译Python2.7源码
本文开始一个系列文章,深入理解Python源码,算是阅读<Python源码剖析>一书的读书笔记,是一项长期进行的工作.一共分三个部分:Python对象模型,Python虚拟机,Python ...