MapReduce java.lang.ArrayIndexOutOfBoundsException:0
我试图在java中运行mapreduce,但出现了这个错误MapReduce java.lang.ArrayIndexOutOfBoundsException:0,java,mapreduce,Java,Mapreduce,我试图在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.
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));
}
}
}
package com.mapreduce;
导入java.io.IOException;
导入java.util.*;
导入org.apache.hadoop.conf.*;
导入org.apache.hadoop.fs.*;
导入org.apache.hadoop.conf.*;
导入org.apache.hadoop.io.*;
导入org.apache.hadoop.mapreduce.*;
导入org.apache.hadoop.mapreduce.lib.input.*;
导入org.apache.hadoop.mapreduce.lib.output.*;
导入org.apache.hadoop.util.*;
公共类WordCount扩展配置的实现工具{
公共静态void main(字符串args[])引发异常{
int res=ToolRunner.run(new WordCount(),args);
系统退出(res);
}
公共int运行(字符串[]args)引发异常{
路径输入路径=新路径(args[0]);
路径outputPath=新路径(args[1]);
配置conf=getConf();
Job Job=新作业(conf,this.getClass().toString());
setInputPath(作业,inputPath);
setOutputPath(作业,outputPath);
job.setJobName(“字数”);
job.setJarByClass(WordCount.class);
setInputFormatClass(TextInputFormat.class);
setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
返回作业。waitForCompletion(true)?0:1;
}
公共静态类映射扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
@凌驾
公共无效映射(可长写键、文本值、,
上下文)抛出IOException、InterruptedException{
字符串行=value.toString();
StringTokenizer标记器=新的StringTokenizer(行);
while(tokenizer.hasMoreTokens()){
set(tokenizer.nextToken());
上下文。写(单词,一);
}
}
}
公共静态类Reduce扩展Reducer{
@凌驾
公共void reduce(文本键、Iterable值、上下文上下文)引发IOException、InterruptedException{
整数和=0;
for(可写入值:值){
sum+=value.get();
}
write(key,newintwriteable(sum));
}
}
}
我明白这意味着数组中没有元素,但是我需要做什么来运行它呢?我正在使用Maven导入所有必要的库。我正在Windows上运行它。代码是中的一个示例,您需要使用与输入路径和输出路径相对应的参数启动程序,因此启动命令应采用下一种格式:
java -cp <my-classpath-here> com.mapreduce.WordCount <input-path> <output-path>
java-cp com.mapreduce.WordCount
ArrayIndexOutOfBoundsException表示args[]为空。ArrayIndexOutOfBoundsException,是的,它可以自我解释。您没有得到预期的参数。我建议您先从Java书籍开始,然后转到hadoop。这样比较容易,,而不是通过Hadoop学习Java。如果我使用windows,我该怎么办?我在回答中提出的不依赖于操作系统,因此它也可以在windows操作系统上工作,假设您提供了正确的类路径和有效的输入和输出路径。在这种情况下,输入和输出路径将指向文本文档?输入路径是,它必须是指向文本的路径文档,输出路径应该是包含映射/还原结果的文件的路径。我的类路径需要是jar文件吗?