欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序

程序员文章站 2022-06-05 16:43:44
...

一、WordCount练习

要在 Eclipse 上编译和运行 MapReduce 程序,需要安装 hadoop-eclipse-plugin,参见厦大网址。

1、查看 HDFS 中的文件列表
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
2、在 Eclipse 中创建 MapReduce 项目WordCount
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
3、新建类 Class,在 Package 处填写 org.apache.hadoop.examples;在 Name 处填写 WordCount,如图
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
4、 WordCount.java 这个文件的代码如下

    package org.apache.hadoop.examples;
     
    import java.io.IOException;
    import java.util.Iterator;
    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 WordCount {
        public WordCount() {
        }
     
        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> [<in>...] <out>");
                System.exit(2);
            }
     
            Job job = Job.getInstance(conf, "word count");
            job.setJarByClass(WordCount.class);
            job.setMapperClass(WordCount.TokenizerMapper.class);
            job.setCombinerClass(WordCount.IntSumReducer.class);
            job.setReducerClass(WordCount.IntSumReducer.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
     
            for(int i = 0; i < otherArgs.length - 1; ++i) {
                FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
            }
     
            FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
            System.exit(job.waitForCompletion(true)?0:1);
        }
     
        public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
            private IntWritable result = new IntWritable();
     
            public IntSumReducer() {
            }
     
            public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
                int sum = 0;
     
                IntWritable val;
                for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                    val = (IntWritable)i$.next();
                }
     
                this.result.set(sum);
                context.write(key, this.result);
            }
        }
     
        public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
            private static final IntWritable one = new IntWritable(1);
            private Text word = new Text();
     
            public TokenizerMapper() {
            }
     
            public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
                StringTokenizer itr = new StringTokenizer(value.toString());
     
                while(itr.hasMoreTokens()) {
                    this.word.set(itr.nextToken());
                    context.write(this.word, one);
                }
     
            }
        }
    }

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
5、复制配置文件解决参数设置问题

    cp /usr/local/hadoop/etc/hadoop/core-site.xml ~/workspace/WordCount/src
    cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml ~/workspace/WordCount/src
    cp /usr/local/hadoop/etc/hadoop/log4j.properties ~/workspace/WordCount/src

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
6、复制完成后,右键点击 WordCount 选择 refresh 进行刷新,可以看到文件结构如下所示
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
7、设置运行时的相关参数,如图所示
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
也可以直接在代码中设置好输入参数,如图所示
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
8、设定参数后,再次运行程序,可以看到运行成功的提示如下所示
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
9、做到这里,就可以使用 Eclipse 方便的进行 MapReduce程序的开发了

二、编译、打包 Hadoop MapReduce 程序

10、将 Hadoop 的 classhpath 信息添加到 CLASSPATH 变量,执行 source ~/.bashrc 使变量生效

vim  ~/.bashrc
source ~/.bashrc

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
11、通过 javac 命令编译 WordCount.java
(这里要到你的WordCount.java目录下运行!)

javac WordCount.java

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序

12、把 .class 文件打包成 jar
(这里也在相关目录下运行)

jar -cvf WordCount.jar ./WordCount*.class

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
如图(生成的jar包)
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
(我把它拷贝到了workspace/WordCount目录下,方便!)
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
13、打包完成后,运行,创建几个输入文件

    mkdir input
    echo "echo of the rainbow" > ./input/file0
    echo "the waiting game" > ./input/file1

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
14、查看input下的文件

ls ./input

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
15、把本地文件上传到伪分布式HDFS上

/usr/local/hadoop/bin/hadoop fs -put ./input input

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序

16、开始运行(代码中设置了package包名,这里要写全!)

/usr/local/hadoop/bin/hadoop jar WordCount.jar org/apache/hadoop/examples/WordCount input output

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
终端运行结果
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
17、Localhost:50070端口查看/user/hadoop/output/结果
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
终端查看伪分布式下/user/hadoop/output/结果

cd /usr/local/hadoop
./bin/hdfs dfs -ls /user/hadoop/output

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
18、查看伪分布式下/user/hadoop/output/part-r-00000结果

./bin/hdfs dfs -cat /user/hadoop/output/part-r-00000

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序

三、遇见的问题及解决办法

问题一:出现找不到类的错误
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序
解决办法:这是因为在代码中设置了package包名,这里也要写全(正确的命令:/usr/local/hadoop/bin/hadoop jar WordCount.jar org/apache/hadoop/examples/WordCount input output)
问题二:Exception in thread “main” org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://localhost:9000/user/hadoop/output already exists
Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序

解决办法:伪分布式下删除运行时自动创建的output

./bin/hdfs dfs -rm -r /user/hadoop/output

Linux下MapReduce编程WordCount练习——使用命令行编译打包运行MapReduce程序