MapReduce编程开发之数据去重

    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值