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. C#文件夹和文件操作

    File.Exist(string path)//文件读写FileStream fs=new FileStream(filename, FileMode.Create);BinaryWriter bw ...

  2. Server.Transfer方式(或称HttpContext方式)传值实例

    public class QueryPage : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtStaDate ...

  3. Sightseeing(poj 3463)

    题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...

  4. 数独挑战(codevs 2924)

    2924 数独挑战  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description “芬兰数学家因卡拉,花费3 ...

  5. 字典树(codevs 4189)

    4189 字典  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 最经,skyzhong得到了 ...

  6. cutpFTP设置步骤

    cutpFTP设置步骤 平常时为了方便两台电脑之间传送数据,我们可以使用cutpftp这个工具实现,而且cutpftp还具有定时传送的功能,非常方便使用.以下是使用该工具的“同步文件夹”功能同步两台电 ...

  7. C#的LINQ to Object

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  8. VS2010 自动跳过代码现象

    今日,发现在Release的调试模式下,VS2010会“莫名其妙”的跳过一些代码不执行. 经实验,初步判断可能的原因:预处理器指令或编译器/连接器优化. 20150703更新: 部分代码: m_pTa ...

  9. python类中的super,原理如何?MRO是什么东东?

    下面这个URL解释得比较清楚. http://python.jobbole.com/86787/?utm_source=group.jobbole.com&utm_medium=related ...

  10. WIN10 新建ORACLE实例

    1 管理员身份进入CMD环境,执行DBCA命令,在弹出窗口的引导中,完成实例创建 2 如果在创建过程中没有选择适当的字符集(最好采用默认字符集),如下图所示,在进入PLSQL DEVELOPER的时候 ...