参考:http://eric-gcm.iteye.com/blog/1807468

例子1:

概要:数据去重

描述:将file1.txt、file2.txt中的数据合并到一个文件中的同时去掉重复的内容

file1:

2012-3-1 a
2012-3-2 b
2012-3-3 c
2012-3-4 d
2012-3-5 a
2012-3-6 b
2012-3-7 c
2012-3-3 c

file2:

2012-3-1 b
2012-3-2 a
2012-3-3 b
2012-3-4 d
2012-3-5 a
2012-3-6 c
2012-3-7 d
2012-3-3 c

代码:

Dedup.java

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;
import org.apache.hadoop.util.GenericOptionsParser; public class Dedup { //map将输入中的value复制到输出数据的key上,并直接输出
public static class Map extends Mapper<Object,Text,Text,Text>{ private static Text line=new Text();//每行数据 //实现map函数
public void map(Object key,Text value,Context context) throws IOException,InterruptedException{ line=value;
context.write(line, new Text(""));
}
} //reduce将输入中的key复制到输出数据的key上,并直接输出
public static class Reduce extends Reducer<Text,Text,Text,Text>{ //实现reduce函数
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException{ context.write(key, new Text(""));
}
} public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); //这句话很关键,IP(172.16.11.74)需要根据实际情况改变
conf.set("mapred.job.tracker", "172.16.11.74:9001");
String[] ioArgs=new String[]{"dedup_in","dedup_out"}; String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs(); if (otherArgs.length != 2) {
System.err.println("Usage: Data Deduplication <in> <out>");
System.exit(2);
} Job job = new Job(conf, "Data Deduplication");
job.setJarByClass(Dedup.class); //设置Map、Combine和Reduce处理类
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class); //设置输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); //设置输入和输出目录
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1); } }

运行步骤:

1. eclipse中建一个JAVA工程,导入Dedup.java,及对应的jar包(从hadoop-0.20.2-cdh3u6中拷贝过来)

2. 编译通过后,导出jar包Dedup.jar(需要带上依赖的包,及选择Main Class)

3. 拷贝Dedup.jar到Linux中(/home/hadoop/tmp/)

4. 启动hadoop

  (命令:start-all.sh)

5. 查看NameNode、DataNode是否都启动了

(命令:jps)

6. hadoop文件系统中建目录:/user/hadoop/dedup_in/  (命令: hadoop fs -mkdir dedup_in)(dfs默认目录为/user/hadoop)

7. 查看:

  (命令:hadoop dfs -ls )

8. 拷贝file1.txt、file2.txt到/user/hadoop/dedup_in/下

(命令:hadoop fs -put file1.txt dedup_in/file1.txt

hadoop fs -put file2.txt dedup_in/file2.txt)

9. 查看file1.txt、file2.txt

  (命令:hadoop fs -text dedup_in/file1.txt

hadoop fs -text dedup_in/file2.txt)

10 运行jar包

(命令:hadoop jar /home/hadoop/tmp/Dedup.jar)

遇到异常:

1. Error opening job jar:....

原因:

  之前把Dedup.jar拷贝到了hadoop文件系统中了,所以找不到Dedup.jar,hadoop jar 后面的jar应该是Linux文件系统中的

2. Mkdirs failed to created /home/tmp

原因:

配置文件/usr/hadoop-0.20.2-cdh3u6/conf/core-site.xml 中的写成了“hadoop.tmp.dir”对应的目录写成了/home/tmp,而实际Linux中木有这个目录

(是之前删了,改成/home/hadoop/tmp),命令(hadoop jar Dedup.jar )执行过程中需要“hadoop.tmp.dir”目录进行临时存储,当找到这个目录时,

会去创建,但是hadoop用户没有权限,所以抛了这个异常

运行结果:

1. 多了一个目录

执行命令(hadoop dfs -ls)

发现多了一个目录:/user/hadoop/dedup_out

2. 新生成的目录下:

查看目录下的文件,命令(hadoop dfs -lsr /user/hadoop/dedup_out/)

/user/hadoop/dedup_out/_SUCCESS

/user/hadoop/dedup_out/_logs

/user/hadoop/dedup_out/_logs/history

/user/hadoop/dedup_out/_logs/history/job_201403091709_0001_1394356210883_hadoop_Data+Deduplication

/user/hadoop/dedup_out/_logs/history/job_201403091709_0001_conf.xml

/user/hadoop/dedup_out/part-r-00000

3. 查看文件/user/hadoop/dedup_out/part-r-00000

命令(hadoop dfs -text /user/hadoop/dedup_out/part-r-00000)

2012-3-1 a
2012-3-1 b
2012-3-2 a
2012-3-2 b
2012-3-3 b
2012-3-3 c
2012-3-4 d
2012-3-5 a
2012-3-6 b
2012-3-6 c
2012-3-7 c
2012-3-7 d

结果证明:

Dedup.java成功地将file1.txt、file2.txt中内容合并去重

hadoop —— MapReduce例子 (数据去重)的更多相关文章

  1. hadoop mapreduce实现数据去重

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

  2. hadoop —— MapReduce例子 (数据排序)

    参考:http://eric-gcm.iteye.com/blog/1807468 file1.txt: 2 32 654 32 15 756 65223 file2.txt: 5956 22 650 ...

  3. Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货

    文章为作者原创,未经许可,禁止转载.    -Sun Yat-sen University 冯兴伟 一.    项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...

  4. MapReduce实例(数据去重)

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

  5. 利用MapReduce实现数据去重

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

  6. hadoop —— MapReduce例子 (求平均值)

    参考:http://eric-gcm.iteye.com/blog/1807468 math.txt: 张三 88 李四 99 王五 66 赵六 77 china.txt: 张三 78 李四 89 王 ...

  7. MapReduce实现数据去重

    一.原理分析 Mapreduce的处理过程,由于Mapreduce会在Map~reduce中,将重复的Key合并在一起,所以Mapreduce很容易就去除重复的行.Map无须做任何处理,设置Map中写 ...

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

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

  9. Hadoop MapReduce执行过程详解(带hadoop例子)

    https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...

随机推荐

  1. vue.js+koa2项目实战(一)创建项目和elementUI配置

    前端采用vuex+element-ui: 后端采用koa2+restfulAPI+sequlize: (一)项目介绍 宠物社区 1.社区 2.好友 3.说说 4.宠粮 5.健康 (二)项目框架 1.V ...

  2. Android开发之WebView具体解释

    概述: 一个显示网页的视图.这个类是你能够滚动自己的Web浏览器或在你的Activity中简单地显示一些在线内容的基础.它使用了WebKit渲染引擎来显示网页,包含向前和向后导航的方法(通过历史记录) ...

  3. HTTP基础(分析两个例子)

    两个例子(一个get,一个post)(一个是访问页面,一个是提交修改后的博文): preferences.aspx:(header)(文件) 1.     Remote Address:42.121. ...

  4. Ubuntu下载、zsync、安装、常见问题

    下载-镜像地址 http://mirrors.ustc.edu.cn/ubuntu-releases/ http://mirrors.163.com/ubuntu-releases/ Ubuntu 更 ...

  5. Oracle中没有 if exists(...)的解决方法

    http://blog.csdn.net/hollboy/article/details/7550171对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常 ...

  6. html的table使用div创建

    午休时间写了一个使用div创建table的案例 1.样式 <style> .table { display: table; } .tableRow { display: table-row ...

  7. 数组方式使用jQuery对象

    一. 使用jQuery选择器获取结果是一个jQuery对象.然而,jQuery类库会让你感觉你正在使用一个定义了索引和长度的数组.在性能方面,建议使用简单的for或者while循环来处理,而不是$.e ...

  8. scrapy递归抓取网页数据

    scrapy spider的parse方法能够返回两种值:BaseItem.或者Request.通过Request能够实现递归抓取. 假设要抓取的数据在当前页,能够直接解析返回item(代码中带**凝 ...

  9. Django--分页、session

    分页 分页的实现,是由我们自己写的后端代码组建而成,这段写的代码可以直接放在以后的任何分页结构中使用. 先来谈谈原始逻辑: 主页代码如下: <!DOCTYPE html> <html ...

  10. ckdeitor的使用方法

    CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...