问题描述
我尝试在Java中运行mapreduce,但出现此错误.
I try to run a mapreduce in java, but get this error.
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
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
I undarstand that it means there are no elements in the array, but what do I need to do run it? I'm using Maven to import all the necessary libraries. I'm running it on Windows. The code is an example from http://www.macalester.edu/~shoop/sc13/hadoop/html/hadoop/wc-detail.html
推荐答案
您需要使用相应于输入路径和输出路径的自变量启动程序,因此您的启动命令应该是下一个格式:
You need to launch your program with arguments corresponding to the input path and the output path, so your launch command should be of the next format:
java -cp <my-classpath-here> com.mapreduce.WordCount <input-path> <output-path>
这篇关于MapReduce java.lang.ArrayIndexOutOfBoundsException:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!