MapReduce 应用实例

Hadoop 版本2.8.0

  • 前期准备工作:

1. 设置用户环境变量 PATH 和 CLASSPATH

方便执行 Hadoop 命令时不用转移到对应的目录下,shell 除了会在当前目录下还会到 PATH 指定位置寻找可执行文件。

使用 javac 命令编译 .java 文件时,如果没有指定 -classpath 选项,会到 CLASSPATH 下寻找程序里 import 的类。使用 echo $PATH 命令可察看对应的环境变量。

vi ~/.bash_profile

# set HADOOP ENVIRONMENT

HADOOP_HOME=~/hadoop-2.8.0

CLASSPATH=$CLASSPATH:$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar:$HADOOP_HOME/share/hadoop/common/hadoop-common-2.8.0.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.8.0.jar

export PATH=$PATH:$HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin

使用 source ~/.bash_profile 使修改生效。有时从系统环境变量中 /etc/profile 去除某一路径可能导致生效不及时,通过重新登陆一次,可以使其重新加载。上述导入的 CLASSPATH 是 MapReduce 函数常用的三个 jar 包,Hadoop-2.8.0 的资源包都在 hadoop-2.8.0/share/hadoop 路径下。

  • WordCount

1. 输入文件准备

  1. 新建输入文件 file1 和 file2。其中:

    file1 的文件内容是:

    hello world

    file2 的文件内容是:

    hello hadoop

    hello mapreduce

  2. 在 HDFS 文件系统中创建输入文件夹(hadoop 可执行文件是在 hadoop-2.8.0/bin 目录下,前面已经将其加入系统路径中,下面命令在 HDFS 根目录下创建文件夹 wordcount_input)

    hadoop fs -mkdir /wordcount_input

  3. 上传本地目录 ~/files 下的输入文件 file1 和 file2 文件到集群上的输入文件夹

    hadoop fs -put ~/files/* /wordcount_input

2. WordCount 代码

 1 package test;
 2 
 3 import java.io.IOException;
 4 import java.util.StringTokenizer;
 5 
 6 import org.apache.hadoop.conf.Configuration;
 7 import org.apache.hadoop.fs.Path;
 8 import org.apache.hadoop.io.IntWritable;
 9 import org.apache.hadoop.io.Text;
10 import org.apache.hadoop.mapreduce.Job;
11 import org.apache.hadoop.mapreduce.Mapper;
12 import org.apache.hadoop.mapreduce.Reducer;
13 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
14 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
15 import org.apache.hadoop.util.GenericOptionsParser;
16 
17 public class WordCount {
18         public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
19                 private final IntWritable one = new IntWritable(1);
20                 private Text word = new Text();
21 
22                 public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
23                         StringTokenizer itr = new StringTokenizer(value.toString());
24                         while (itr.hasMoreTokens()) {
25                                 word.set(itr.nextToken());
26                                 context.write(word, one);
27                         }
28                 }
29         }
30         public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
31                 private IntWritable result = new IntWritable();
32                 public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
33                         int sum = 0;
34                         for (IntWritable val : values) {
35                                 sum += val.get();
36                         }
37                         result.set(sum);
38                         context.write(key, result);
39                 }
40         }
41         public static void main (String[] args) throws Exception {
42                 Configuration conf = new Configuration();
43                 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
44                 if (otherArgs.length != 2) {
45                         System.err.println("Usage: wordcount <in> <out>");
46                         System.exit(2);
47                 }
48                 Job job = new Job(conf, "word count");
49                 job.setJarByClass(WordCount.class);
50                 job.setMapperClass(TokenizerMapper.class);
51                 job.setCombinerClass(IntSumReducer.class);
52                 job.setReducerClass(IntSumReducer.class);
53                 job.setOutputKeyClass(Text.class);
54                 job.setOutputValueClass(IntWritable.class);
55                 FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
56                 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
57                 System.exit(job.waitForCompletion(true) ? 0 : 1);
58         }
59 }
WordCount.java

3. 编译 WordCount.java 程序

  javac -d ~/files ~/files/WordCount.java

  上述命令将 ~/files/WordCount.java 的 java 文件编译后结果存放在 -d 选项指定的目录下,java 文件中指定的 package 打包命令会使编译生成的字节码 class 文件放置在自动创建的包目录下,比如在本例程序开头 package test 命令,会使在 ~/files 目录下创建 test 子目录,里面包含编译生成的文件。

4. 将编译结果打包成 Jar 包

  jar cvf wordcount.jar ~/files/test

  上述命令将之前生产的 package 下的 class 文件进行打包,并对生成的 jar 包进行命名。

5. 在集群上运行 WordCount 程序,命令行指定参数

  hadoop jar ~/files/wordcount.jar test.WordCount /wordcount_input /wordcount_output

  上述命令需要指定 Jar 包的路径,同时还需要指定包含 package 路径的类名。

6. 查看输出结果

  hadoop fs -cat /wordcount_output/part-r-00000

    [lb@host98 ~/files]$hadoop fs -cat /wordcount_output/part-r-00000

    17/06/28 15:49:09 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

    hadoop 1

    hello 3

    mapreduce 1

    world 1

原文地址:https://www.cnblogs.com/hopelee/p/7090510.html