1. 设计思路

  去重,重点就是无论某个数据在文件中出现多少次,最后只是输出一次就可以。 根据这一点,我们联想到在reduce阶段数据输入形式是 <key, value list>,只要是key相同的,在shuffle阶段都会聚合在一起,所以只要在map阶段将要去重的数据作为key值就可以达到目的。

2. 具体实现

  

package moverepeat;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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; /**
*
* @author Amei 去除重复的数据项
*/ public class Remove {
public static class Map extends Mapper<LongWritable, Text, Text, Text> {
protected void map(LongWritable key, Text value, Context output)
throws java.io.IOException, InterruptedException {
//将每一行的数据作为map输出的key
output.write(value, new Text(""));
};
} public static class Reduce extends Reducer<Text, Text, Text, Text> {
protected void reduce(Text key, Iterable<Text> values, Context output)
throws java.io.IOException, InterruptedException {
//经过shuffle阶段后,Reduce的输入数据格式为<key, value list>,此时key没有相同的值
output.write(key, new Text(""));
};
} public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException {
Configuration configuration = new Configuration();
Job job = new Job(configuration, "remove");
job.setJarByClass(Remove.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(
"/user/hadoop_admin/removein"));
FileOutputFormat.setOutputPath(job, new Path(
"/user/hadoop_admin/removeout"));
System.exit((job.waitForCompletion(true) ? 0 : 1));
}
}

  测试用例

  file01

wangkun
wangkun
wangkun
amei
amei

  file02

  

wangkun
wangkun
wangkun
amei
amei

 去重结果:

  

amei
amei
wangkun
wangkun
wangkun
wangkun
wangkun

MapReduce应用案例--简单的数据去重的更多相关文章

  1. MapReduce应用案例--简单排序

    1. 设计思路 在MapReduce过程中自带有排序,可以使用这个默认的排序达到我们的目的. MapReduce 是按照key值进行排序的,我们在Map过程中将读入的数据转化成IntWritable类 ...

  2. MapReduce编程系列 — 3:数据去重

    1.项目名称: 2.程序代码: package com.dedup; import java.io.IOException; import org.apache.hadoop.conf.Configu ...

  3. [Hadoop]-从数据去重认识MapReduce

    这学期刚好开了一门大数据的课,就是完完全全简简单单的介绍的那种,然后就接触到这里面最被人熟知的Hadoop了.看了官网的教程[吐槽一下,果然英语还是很重要!],嗯啊,一知半解地搭建了本地和伪分布式的, ...

  4. MapReduce实例(数据去重)

    数据去重: 原理(理解):Mapreduce程序首先应该确认<k3,v3>,根据<k3,v3>确定<k2,v2>,原始数据中出现次数超过一次的数据在输出文件中只出现 ...

  5. 利用MapReduce实现数据去重

    数据去重主要是为了利用并行化的思想对数据进行有意义的筛选. 统计大数据集上的数据种类个数.从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重. 示例文件内容: 此处应有示例文件 设计思路 数据 ...

  6. hadoop mapreduce实现数据去重

    实现原理分析: map函数数将输入的文本按照行读取,   并将Key--每一行的内容   输出    value--空. reduce  会自动统计所有的key,我们让reduce输出key-> ...

  7. hadoop —— MapReduce例子 (数据去重)

    参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...

  8. js 对 只包含简单类型数据的对象 为元素 组成的数组 进行去重

    /** * 对于由简单类型数据组成的对象为元素组成的数组进行去重操作 * @params {Array} 需要去重的对象数组 * @returns {Array} 去重后的对象数组 */ functi ...

  9. 大数据笔记(十)——Shuffle与MapReduce编程案例(A)

    一.什么是Shuffle yarn-site.xml文件配置的时候有这个参数:yarn.nodemanage.aux-services:mapreduce_shuffle 因为mapreduce程序运 ...

随机推荐

  1. 由浅入深剖析.htaccess

    转自:http://blog.csdn.net/21aspnet/article/details/6908025 [-] htaccess文件使用前提 htaccess基本语法介绍 现学现用学习正则表 ...

  2. Qt 安装一个Service

    QString command = "sc create YourServiceName binPath= \""+application_path+"\&qu ...

  3. [Android Pro] http请求中传输base64出现加号变空格的解决办法

    reference to : http://blog.csdn.net/jsjwbxzy/article/details/45970231 try { des = URLEncoder.encode( ...

  4. [Android Pro] 使用apktool工具遇到could not decode arsc file的解决办法

    转:http://www.cnblogs.com/sage-blog/p/4323049.html 最近使用APKtool工具反编译APK老是提示不成功,错误如下: Exception in thre ...

  5. SQL语句删除重复数据

    1.如表中没有主键,先添加自动增长主键 alter table 表名 add 列名 int identity (1,1) primary key 2.删除重复数据 delete from 表名 whe ...

  6. java 设置允许ajax XMLHttpRequest 请求跨域访问

    怎样才能算跨域?协议,域名,端口都必须相同,才算在同一个域. 方案1: 使用XMLHttpRequest...  异步请求不能跨域访问,除非要访问的网页响应头信息设置为允许跨域访问. 将网页设置为允许 ...

  7. codevs 2530大质数

    链接:http://codevs.cn/problem/1530/ 解题思路: 这个题最关键的剪枝还是 因子小于平方根,但不是像原来那样用. 逆转思维,与其说判断哪些是质数,不如说判断哪些不是质数,更 ...

  8. 与你相遇好幸运,My Toolkit of Nodejs

    >测试:restler.mocha.assert.request.request-promise >安装:nrm >运维:pm2.node-gyp >开发:nodemon.in ...

  9. ASP.NET Web API中使用GZIP 或 Deflate压缩

    对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式. 那么如何实现对ASP.NET Web API 进行压缩呢,我将使用非常流行的库用于压缩/解压缩称为DotNetZip库.这个库可以使用Nu ...

  10. async/await 异步编程(转载)

    转载地址:http://www.cnblogs.com/teroy/p/4015461.html 前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入 ...