Java 执行mapreduce程序时出错
我不熟悉java和mapreduce。我已经编写了mapreduce程序来执行字数统计。我面临以下错误Java 执行mapreduce程序时出错,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,我不熟悉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])); 下面是我
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);
}
}
包mapreduce.mrunit;
导入java.util.StringTokenizer;
导入java.io.IOException;
导入org.apache.hadoop.conf.Configuration;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.io.IntWritable;
导入org.apache.hadoop.io.LongWritable;
导入org.apache.hadoop.io.Text;
导入org.apache.hadoop.mapreduce.Job;
导入org.apache.hadoop.mapreduce.Mapper;
导入org.apache.hadoop.mapreduce.Reducer;
导入org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
导入org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
公共类字数{
公共静态类映射扩展
制图员{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共void映射(可长写键、文本值、上下文)
抛出IOException、InterruptedException{
字符串行=value.toString();
StringTokenizer标记器=新的StringTokenizer(行);
while(tokenizer.hasMoreTokens()){
set(tokenizer.nextToken());
上下文。写(单词,一);
}
}
}
公共静态类Reduce扩展
减速器{
public void reduce(文本键、Iterable值、,
上下文)抛出IOException、InterruptedException{
整数和=0;
for(可写入值:值){
sum+=val.get();
}
write(key,newintwriteable(sum));
}
}
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
@抑制警告(“弃用”)
Job Job=新作业(conf,“wordcount”);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
//setInputFormatClass(TextInputFormat.class);
//setOutputFormatClass(TextOutputFormat.class);
setInputPaths(作业,新路径(args[0]);
setOutputPath(作业,新路径(args[1]);
job.waitForCompletion(true);
}
}
我无法纠正这个错误。请帮助我修复此错误。您是如何运行它的?该错误表明在运行作业时没有输入参数。必须在参数中插入输入和输出路径,如下所示:
hadoop jar MyProgram.jar /path/to/input /path/to/output
你是怎么运作的?该错误表明在运行作业时没有输入参数。必须在参数中插入输入和输出路径,如下所示:
hadoop jar MyProgram.jar /path/to/input /path/to/output
错误位于
main()
方法的下一行:
FileInputFormat.setInputPaths(job, new Path(args[0]));
从中,当
抛出以指示已使用非法
指数索引为负数或大于或等于
数组的大小
这意味着,数组args
参数到main()
方法的长度缺少元素
根据您的程序,您希望它包含2个元素,其中
第一个元素args[0]
是输入路径
第二个元素是输出路径
请创建一个输入目录,并放置一个包含一些行的文本文件。
请注意,不应创建输出目录(可以创建到父目录)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
程序的分步指令。错误位于main()
方法的下面一行:
FileInputFormat.setInputPaths(job, new Path(args[0]));
从中,当
抛出以指示已使用非法
指数索引为负数或大于或等于
数组的大小
这意味着,数组args
参数到main()
方法的长度缺少元素
根据您的程序,您希望它包含2个元素,其中
第一个元素args[0]
是输入路径
第二个元素是输出路径
请创建一个输入目录,并放置一个包含一些行的文本文件。
请注意,不应创建输出目录(可以创建到父目录)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
程序的分步指令。什么是args[0]
?我添加了关于为什么会出现此错误以及应该如何处理的详细信息。什么是args[0]
?我添加了关于为什么会出现此错误以及应该如何处理的详细信息。