【原创】快速编写和运行一个属于自己的MapReduce例子程序

如何快速地编写和运行一个属于自己的MapReduce例子程序

原文出处 :http://www.it165.net/pro/html/201403/11128.html

 

首先有两个前提:

1. 有一个已经可以运行的hadoop 集群(也可以是伪分布系统), 上面的hdfsmapreduce工作正常 (这个真的是最基本的了, 不再累述, 不会的请参考 http://hadoop.apache.org/docs/current/)

2. 集群上安装了JDK (编译运行时会用到)

正式开始

1. 首先登入hadoop 集群里面的一个节点, 创建一个java源文件, 偷懒起见, 基本盗用官方的word count (因为本文的目的是教会你如何快编写和运行一个MapReduce程序, 而不是如何写好一个功能齐全的MapReduce程序)

内容如下:

import java.io.IOException;

import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

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 myword {

  public static class TokenizerMapper

       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);

    private Text word = new Text();

    public void map(Object key, Text value, Context context

                    ) throws IOException, InterruptedException {

      StringTokenizer itr = new StringTokenizer(value.toString());

      while (itr.hasMoreTokens()) {

        word.set(itr.nextToken());

        context.write(word, one);

      }

    }

  }

  public static class IntSumReducer

       extends Reducer<Text,IntWritable,Text,IntWritable> {

    private IntWritable result = new IntWritable();

 

    public void reduce(Text key, Iterable<IntWritable> values,

                       Context context

                       ) throws IOException, InterruptedException {

      int sum = 0;

      for (IntWritable val : values) {

        sum += val.get();

      }

      result.set(sum);

      context.write(key, result);

    }

  }

  public static void main(String[] args) throws Exception {

    Configuration conf = new Configuration();

    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

    if (otherArgs.length != 2) {

      System.err.println("Usage: wordcount <in> <out>");

      System.exit(2);

    }

    Job job = new Job(conf, "word count");

    job.setJarByClass(myword.class);

    job.setMapperClass(TokenizerMapper.class);

    job.setCombinerClass(IntSumReducer.class);

    job.setReducerClass(IntSumReducer.class);

    job.setOutputKeyClass(Text.class);

    job.setOutputValueClass(IntWritable.class);

    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

    System.exit(job.waitForCompletion(true) ? 0 : 1);

  }

}

与官方版本相比, 主要做了两处修改

1) 为了简单起见,去掉了开头的 package org.apache.hadoop.examples; 

2) 将类名从 WordCount 改为 myword, 以体现是我们自己的工作成果 :)

2.  拿到hadoop 运行的class path, 主要为编译所用

在已安装好的Hadoopbin文件下运行命令 

./hadoop classpath    

保存打出的结果,

 

                                                     

3. 编译

    在/home/lt/WordCount/的终端中运行命令

    javac -classpath xxx ./myword.java

xxx部分就是上一步里面取到的class path

运行完此命令后, 当前/home/lt/WordCount/目录下会生成一些.class 文件, 例如:

myword.class  myword$IntSumReducer.class  myword$TokenizerMapper.class

4. class文件打包成.jar文件

    在/home/lt/WordCount/运行命令

jar -cvf myword.jar ./*.class

至此目标jar 文件成功生成

5. 准备一些文本文件, 上传到hdfs, 以做word countinput

例子:在/home/lt/WordCount/中新建文件夹mapred_test,并建立test文件输入一些单词。在hadoopbin文件夹下运行命令:

 ./hadoop fs -put  /home/lt/WordCount/mapred_test/test  /user/root

即将/home/lt.WordCouont/mapred_test/test文件上传到hdfs上的/user/root文件夹下。

6. 运行我们的程序

hadoopbin文件夹下运行命令:

 ./hadoop jar /home/lt/WordCount/myword.jar  myword  /user/root/test  output

顺利的话, 此命令会正常进行, 一个MapReduce job 会开始工作, 输出的结果会保存在 hdfs 当前用户根目录下的output 文件夹里面。

至此大功告成!如果还需要更多的功能, 我们可以修改前面的源文件以达到一个真正有用的MapReduce job。但是原理大同小异, 练手的话, 基本够了

7. 将output文件夹从hdfs上拷贝到本地指定的路径下

./hadoop fs -get /user/root/output  /home/lt/WordCount

 

 

原文地址:https://www.cnblogs.com/nanxin521/p/4111501.html