MapReduce就是一个利用分而治之的思想做计算的框架,所谓分,就是将数据打散,分成可以计算的小份,治就是将数据合并,相同键的数据合并成一个集合。MapReduce并不能解决所有的问题,因为他的数据类型是键值对,只能解决特定范围的问题。
数据去重算法,其实就是词频统计的一个变种,词频统计是统计文本中的单词出现的次数,如果一个单词出现两次,就算重复,去掉重复的办法就是保留一个副本,键值对表示的话,我们只需要保留键的值就可以了。其实MapReduce最适合干的工作就是数据去重,因为在reduce阶段,所有的数据的键都是唯一的,正好满足数据去重的要求。
在map阶段,我们将value作为key输出,将new Text()作为value就是空输出,这样我们就把所有的数据遍历了一遍,这里面,我们几乎不用什么复杂的算法。
在shuffle阶段,这些值相同的key会被合并到一块,value-list是一个new Text()的集合,如果有重复的记录,那么这个集合的长度会大于1,而且这些key还会进行默认的排序。
在reduce阶段,我们的输入会变为<key , <value-list>>,我们只需要将key一一输出即可,这样就达到了数据去重的目的。思路其实很简单。
算法代码:
package com.xxx.hadoop.mapred;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 DeDuplicationApp {
public static class Map extends Mapper<Object, Text, Text, Text>{
protected void map(Object key, Text value,
Context context) throws IOException ,InterruptedException {
context.write(value, new Text());
};
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
protected void reduce(Text key, java.lang.Iterable<Text> values,
Context context) throws IOException ,InterruptedException {
context.write(key, new Text());
};
}
public static void main(String[] args) throws Exception {
String input = "/user/root/deduplication/input",
output = "/user/root/deduplication/output";
System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.56.202:9000");
Job job = Job.getInstance(conf);
job.setJarByClass(DeDuplicationApp.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
FileInputFormat.addInputPath(job, new Path(input));
FileOutputFormat.setOutputPath(job, new Path(output));
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
System.exit(job.waitForCompletion(true)?0:1);
}
}
原始数据:
运行程序,控制台打印信息如下:
2019-08-31 14:33:56 [INFO ] [main] [org.apache.hadoop.conf.Configuration.deprecation] session.id is deprecated. Instead, use dfs.metrics.session-id
2019-08-31 14:33:56 [INFO ] [main] [org.apache.hadoop.metrics.jvm.JvmMetrics] Initializing JVM Metrics with processName=JobTracker, sessionId=
2019-08-31 14:33:57 [WARN ] [main] [org.apache.hadoop.mapreduce.JobResourceUploader] Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
2019-08-31 14:33:57 [WARN ] [main] [org.apache.hadoop.mapreduce.JobResourceUploader] No job jar file set. User classes may not be found. See Job or Job#setJar(String).
2019-08-31 14:33:57 [INFO ] [main] [org.apache.hadoop.mapreduce.lib.input.FileInputFormat] Total input paths to process : 3
2019-08-31 14:33:57 [INFO ] [main] [org.apache.hadoop.mapreduce.JobSubmitter] number of splits:3
2019-08-31 14:33:57 [INFO ] [main] [org.apache.hadoop.mapreduce.JobSubmitter] Submitting tokens for job: job_local1661418487_0001
2019-08-31 14:33:58 [INFO ] [main] [org.apache.hadoop.mapreduce.Job] The url to track the job: http://localhost:8080/
2019-08-31 14:33:58 [INFO ] [main] [org.apache.hadoop.mapreduce.Job] Running job: job_local1661418487_0001
2019-08-31 14:33:58 [INFO ] [Thread-3] [org.apache.hadoop.mapred.LocalJobRunner] OutputCommitter set in config null
2019-08-31 14:33:58 [INFO ] [Thread-3] [org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter] File Output Committer Algorithm version is 1
2019-08-31 14:33:58 [INFO ] [Thread-3] [org.apache.hadoop.mapred.LocalJobRunner] OutputCommitter is org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter
2019-08-31 14:33:58 [INFO ] [Thread-3] [org