Hadoop Mapreduce字数统计程序

时间:2017-10-31 07:14:58

标签: java hadoop mapreduce

我是Hadoop的新手。以下是我的代码。当我运行Jar时,我收到以下错误消息。

输入文件(wordcount.txt)=>此文件存储在“/home/cloudera/SK_JAR/jsonFile/wordcount.txt”路径中

Hello Hadoop,Goodbye Hadoop。

   package com.main;

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.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

    public static class Map extends Mapper {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

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

     public static class Reduce extends Reducer {

        public void reduce(Text key, Iterable<IntWritable> values, Context context) 
          throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            context.write(key, new IntWritable(sum));
        }
     }


    public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
        // TODO Auto-generated method stub

Configuration conf = new Configuration();


        Job job = new Job(conf, "wordcount");

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);

    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));


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


    }

}

以下是错误消息。有人可以帮我这个吗?

如果你们需要更多细节,请告诉我们。

hadoop jar Wordcount.jar WordCount '/home/cloudera/SK_JAR/jsonFile/wordcount.txt'  output

Error in laoding args file.java.io.FileNotFoundException: WordCount (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at com.main.mainClass.main(mainClass.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:136)

2 个答案:

答案 0 :(得分:0)

  

hadoop jar Wordcount.jar WordCount

您的主要课程是课程com.main的一部分,因此开始上课需要com.main.WordCount

您可以将JAR文件打开为ZIP文件,以验证您是否可以在其中找到com/main/WordCount$Map.class。如果它不存在,那么Eclipse正在构建您的JAR错误。

我可能会建议您学习Maven,Gradle或SBT来构建JAR而不是IDE。在生产中,这些是常用于捆绑Hadoop JAR文件的工具。

答案 1 :(得分:-1)

看来你的runnable jar文件中没有提到主类文件。

如果您使用Eclipse创建jar文件,请按照以下步骤创建Wordcount.jar。

  

右键点击Project - &gt;导出JAR文件 - &gt;单击下一步 - &gt;取消选中   所有其他资源。然后提供导出.jar文件的路径 - &gt;   单击下一步 - &gt;保持选项 - &gt;单击下一步并完成。 reference