Hadoop学习-编程实现单词计数

概述

本问讲述在win10下,使用eclipse开发统计单词的hadoop程序。

创建工程

创建map/reduce工程wordcount

过程略。

新建测试类MyWordCount

package hdp.test;

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

    public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> {
        private final IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            StringTokenizer stn = new StringTokenizer(value.toString());
            while (stn.hasMoreTokens()) {
                word.set(stn.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class WordCountReducer 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 IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        String[] cliArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (cliArgs.length != 2) {
            System.err.println("Usage: mywordcount <in> <out>");
            System.exit(2);
        }

        Job myJob = Job.getInstance(conf, "My first job");
        myJob.setJarByClass(MyWordCount.class);
        myJob.setMapperClass(WordCountMapper.class);
        myJob.setReducerClass(WordCountReducer.class);
        myJob.setCombinerClass(WordCountReducer.class);
        myJob.setOutputKeyClass(Text.class);
        myJob.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(myJob, new Path(cliArgs[0]));
        FileOutputFormat.setOutputPath(myJob, new Path(cliArgs[1]));

        boolean isSucced = myJob.waitForCompletion(true);
        System.out.println("result:" + isSucced);
        System.exit(isSucced ? 0: 1);
    }

}

代码说明:
数组String[] cliArgs其实是配置的运行参数:

[hdfs://localhost:9000/testdir/input, hdfs://localhost:9000/testdir/out]  

设置WordCount运行参数

右键wordcount工程,Run As->Run Configurations,填入参数:
hdfs://localhost:9000/testdir/input
hdfs://localhost:9000/testdir/out
如下图:
这里写图片描述
配置运行时的input和output两个参数,我这里把本地文件上传到了input目录(已存在),hdfs目录作为输出,其中out目录在hdfs中不存在,如果已经存在则先删除,或使用其他名字,否则运行时会报错。

运行程序

运行方法同运行一般Java程序,运行成功会打印出:result:true,结果如下图示:
这里写图片描述

查看out目录下的part-r-00000文件:
这里写图片描述

结果分析:
因为input目录下有一个文件,其内容为:

 hello hadoop   hello hadoop

wordcount程序会统计这个文件里的所有单词。

遇到的错误

错误1:

Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://localhost:9000/testdir/out already exists
    at org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.checkOutputSpecs(FileOutputFormat.java:146)
    at org.apache.hadoop.mapreduce.JobSubmitter.checkSpecs(JobSubmitter.java:266)
    at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:139)
    at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
    at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Unknown Source)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1758)
    at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
    at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
    at hdp.test.MyWordCount.main(MyWordCount.java:66)

程序执行成功后,再次执行,会报错。
原因:out目录已存在。
解决:删除out目录,重新执行程序:

D:\develop\hadoop-2.7.6\bin>hadoop fs -rm -r hdfs://localhost:9000/testdir/out
18/07/07 19:10:37 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes.
Deleted hdfs://localhost:9000/testdir/out

错误2:

Exception in thread "main" org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: hdfs://localhost:9000/testdir/input
    at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:323)
    at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.listStatus(FileInputFormat.java:265)
    at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.getSplits(FileInputFormat.java:387)
    at org.apache.hadoop.mapreduce.JobSubmitter.writeNewSplits(JobSubmitter.java:301)
    at org.apache.hadoop.mapreduce.JobSubmitter.writeSplits(JobSubmitter.java:318)
    at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:196)
    at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
    at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Unknown Source)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1758)
    at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
    at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
    at hdp.test.MyWordCount.main(MyWordCount.java:66)

原因:input目录不存在导致。
解决方法: 创建input目录

D:\develop\hadoop-2.7.6\bin>hadoop fs -mkdir hdfs://localhost:9000/testdir/input  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oyezitan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值