繁体   English   中英

执行mapreduce程序时出错

[英]Error while executing the mapreduce program

我是java和mapreduce的新手。 我已经编写了mapreduce程序来执行字数统计。 我正面临以下错误。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at mapreduce.mrunit.Wordcount.main(Wordcount.java:63)

63行代码是:

FileInputFormat.setInputPaths(job, new Path(args[0]));

以下是我编写的代码:

package mapreduce.mrunit;
import java.util.StringTokenizer;
import java.io.IOException;
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.output.FileOutputFormat;

public class Wordcount {
    public static class Map extends
            Mapper<LongWritable, Text, Text, IntWritable> {
        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<Text, IntWritable, Text, IntWritable> {
        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 Exception {
        Configuration conf = new Configuration();

        @SuppressWarnings("deprecation")
        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.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);
    }
}

我无法解决该错误。 请帮我解决此错误。

您是如何运行的? 该错误表明您在运行作业时未放入参数。 您必须在以下参数中插入输入和输出路径:

hadoop jar MyProgram.jar /path/to/input /path/to/output

错误在main()方法的以下行:

FileInputFormat.setInputPaths(job, new Path(args[0]));

Javadoc中 ,在以下情况时抛出此异常

抛出该错误指示数组已使用非法索引访问。 索引为负或大于或等于数组的大小。

这意味着main()方法的数组args参数的长度缺少元素。

根据您的程序,您期望它包含2个元素,其中

第一个元素args[0]是输入路径。

第二个元素args[1]是输出路径。

请创建一个输入目录,并在文本文件中添加一些行。 请注意,您不应创建输出目录(可以创建上级父目录)。 MapReduce将自动创建它。

因此,假设您的路径是

inputPath = /user/cloudera/wordcount/input
outputPath = /user/cloudera/wordcount

然后像这样执行程序

hadoop jar wordcount.jar mapreduce.mrunit.Wordcount /user/cloudera/wordcount/input /user/cloudera/wordcount/output

请注意,我在程序的第二个参数中添加了output文件夹,以遵守输出路径不应存在的限制,该路径将由程序在运行时创建。

最后,我建议您遵循本教程该教程具有执行WordCount程序的分步指令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM