MR去重

发布 : 2016-02-12 分类 : 大数据 浏览 :

数据去重

1
2
3
4
5
"数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选。

统计大数据集上的数据种类个数、从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重。

下面就进入这个实例的MapReduce程序设计。

设计思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
数据去重的最终目标是让原始数据中出现次数超过一次的数据在输出文件中只出现一次。

我们自然而然会想到将同一个数据的所有记录都交给一台reduce机器,无论这个数据出现多少次,只要在最终结果中输出一次就可以了。

具体就是reduce的输入应该以数据作为key,而对value-list则没有要求。

当reduce接收到一个<key,value-list>时就直接将key复制到输出的key中,并将value设置成空值。

(reduce中的key表示的是我们要统计的数据例如2012-3-7 c,另外的value可以理解为一个序号,没有太大的作用,可理解为无意义数据)

在MapReduce流程中,map的输出<key,value>经过shuffle过程聚集成<key,value-list>后会交给reduce。

shuffle是MapReduce的关键,也是mapreduce的难点,明白了shuffle,mapreduce就没有什么内容了。

而这里的value-list则是shuffle的难点。value-list可以理解是用来标识有效数据的。但是其本身没有太大意义。


所以从设计好的reduce输入可以反推出map的输出key应为数据,value任意。

继续反推,map输出数据的key为数据,而在这个实例中每个数据代表输入文件中的一行内容,所以map阶段要完成的任务就是在采用Hadoop默认的作业输入方式之后,

将value设置为key,并直接输出(输出中的value任意)。

map中的结果经过shuffle过程之后交给reduce。reduce阶段不会管每个key有多少个value,它直接将输入的key复制为输出的key,并输出就可以了

(输出中的value被设置成空了)。


上面是不是有点难以理解那,这里以后在进行总结,输出中的value被设置成空了,初学者还是比较困惑的。

这里举个例子:

下面的1,2,3也就是被置为空的值,可能不是1,2,3....,也可能是其他值。这里只要明白key被置为空的key是什么意思就够了。

file.txt

1
[root@node1 hadoop-2.5.1]# cat file.txt

file2.txt

1
[root@node1 hadoop-2.5.1]# cat file2.txt

将file.txt,file2.txt上传到HDFS文件系统的指定目录上

1
[root@node1 hadoop-2.5.1]# hadoop fs -ls /usr/matrix/input/file

DisMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.matrix.distinct;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class DisMapper extends Mapper<Object, Text, Text, Text> {

// 每行数据
private static Text line = new Text();

@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
line = value;
context.write(line, new Text(""));
}

}

DisReducer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.matrix.distinct;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class DisReducer extends Reducer<Text, Text, Text, Text> {

// reducer将输入的key作为输出的key

@Override
protected void reduce(Text key, Iterable<Text> value, Context context) throws IOException, InterruptedException {
context.write(key, new Text(""));
}

}

DisTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.matrix.distinct;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class DisTest {

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://node5:8020");

FileSystem fs = FileSystem.get(conf);

Job job = Job.getInstance(conf);

job.setJarByClass(DisTest.class);

job.setMapperClass(DisMapper.class);
job.setCombinerClass(DisReducer.class);
job.setReducerClass(DisReducer.class);

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);

FileInputFormat.setInputPaths(job, "/usr/matrix/input/file");

Path outer = new Path("/usr/matrix/output/file/");

if (fs.exists(outer)) {
fs.delete(outer, true);
}

FileOutputFormat.setOutputPath(job, outer);

boolean f = job.waitForCompletion(true);

if (f) {
System.out.println("程序执行成功!");
}

}
}

运行结果:

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2016/02/12/MR去重/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹