【问题标题】:MapReduce java.lang.ArrayIndexOutOfBoundsException: 0MapReduce java.lang.ArrayIndexOutOfBoundsException:0
【发布时间】:2016-10-11 20:32:04
【问题描述】:

我尝试在 java 中运行 mapreduce,但收到此错误。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at com.mapreduce.WordCount.run(WordCount.java:23)
        at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
        at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:84)
        at com.mapreduce.WordCount.main(WordCount.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

WordCount.java

package com.mapreduce;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;

public class WordCount extends Configured implements Tool {

    public static void main(String args[]) throws Exception {
        int res = ToolRunner.run(new WordCount(), args);
        System.exit(res);
    }

    public int run(String[] args) throws Exception {
        Path inputPath = new Path(args[0]);
        Path outputPath = new Path(args[1]);

        Configuration conf = getConf();
        Job job = new Job(conf, this.getClass().toString());

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);

        job.setJobName("WordCount");
        job.setJarByClass(WordCount.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

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

        return job.waitForCompletion(true) ? 0 : 1;
    }

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

        @Override
        public void map(LongWritable key, Text value,
                        Mapper.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> {

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

            context.write(key, new IntWritable(sum));
        }
    }

}

我不明白这意味着数组中没有元素,但是我需要做什么来运行它?我正在使用 Maven 导入所有必要的库。我在 Windows 上运行它。该代码是来自http://www.macalester.edu/~shoop/sc13/hadoop/html/hadoop/wc-detail.html的示例

【问题讨论】:

  • ArrayIndexOutOfBoundsException 提示 args[] 为空。
  • ArrayIndexOutOfBoundsException,是的,它非常自我解释。您没有得到预期的参数。
  • 我建议您先从 Java 书籍开始,然后再转向 hadoop。这样会比通过 Hadoop 学习 Java 更容易。

标签: java mapreduce


【解决方案1】:

您需要使用与输入路径和输出路径相对应的参数来启动程序,因此您的启动命令应该是以下格式:

java -cp <my-classpath-here> com.mapreduce.WordCount <input-path> <output-path>

【讨论】:

  • 如果我用的是windows怎么办?
  • 我在回答中提出的建议与操作系统无关,因此假设您提供正确的类路径和有效的输入和输出路径,它也可以在 Windows 操作系统上运行
  • 在这种情况下输入和输出路径会指向文本文档吗?
  • 输入路径是的,它必须是文本文档的路径,输出路径应该是包含 map/reduce 结果的文件的路径
  • my-classpath-here 需要是jar文件吗?
猜你喜欢
  • 1970-01-01
  • 2014-01-30
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
相关资源
最近更新 更多