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

参考: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);

     }

} 
View Code

运行步骤:

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中内容合并去重

原文地址:https://www.cnblogs.com/-wangjiannan/p/3590324.html