这个问题在这里已经有了答案:
mapreduce java.lang.arrayindexoutofboundsexception:0(1个答案)
上个月关门了。
我正在尝试使用hadoop计算文本文件中每个单词的频率。我已附上密码。
package sub4;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
public class count {
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{
//private final static IntWritable one = new IntWritable(1);
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().split(",");
for(String lines:line) {
context.write(new Text(lines),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();
Job job = new Job(conf, "count");
job.setJarByClass(count.class);
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.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
} ``
}
我保存了这段代码,并试图通过右键单击它并按export使它成为eclipse中的jar文件。之后,我转到终端并键入hadoopjar/home/user/wcount.jar sub4.count//wc/output。
这个错误来了
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at sub4.count.main(count.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
1条答案
按热度按时间l0oc07j21#
代码需要在命令行上输入文件名和输出文件名。
你没有提供。