Hadoop Mapreduce 字数统计程序

Hadoop Mapreduce word count Program

我是 Hadoop 新手。下面是我的代码。我在 运行 Jar.

时收到以下错误消息

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

你好 Hadoop,再见 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)

您的可运行 jar 文件似乎未提及主 class 文件。

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

Right click on Project -> Export JAR file -> Click Next - > Uncheck all other resources. Then provide path for exporting .jar file -> Click Next -> Keep options selected -> Click Next and Finish. reference

hadoop jar Wordcount.jar WordCount

您的主要 class 是软件包 com.main 的一部分,因此需要 com.main.WordCount 来启动您的 class。

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

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