【发布时间】:2019-08-13 17:56:34
【问题描述】:
我尝试从 .csv 文件中获取最小值和最大值,但我的代码抛出了这个错误:
线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 1 超出长度 0 的范围 在 PSAnalyserGui.Test.main(Test.java:53)
代码:
public class Test {
public static class ColMapper extends
Mapper<Object, Text, Text, DoubleWritable> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String[] cols = value.toString().split(",");
for (int i = 0; i < cols.length; i++) {
context.write(new Text(String.valueOf(i + 1)),new
DoubleWritable(Double.parseDouble(cols[i])));
}
}
}
public static class ColReducer extends
Reducer<Text, DoubleWritable, Text, DoubleWritable> {
public void reduce(Text key, Iterable<DoubleWritable> values,
Context context) throws IOException, InterruptedException {
double min = Integer.MAX_VALUE, max = 0;
Iterator<DoubleWritable> iterator = values.iterator(); //Iterating
while (iterator.hasNext()) {
double value = iterator.next().get();
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
}
context.write(new Text(key), new DoubleWritable(min));
context.write(new Text(key), new DoubleWritable(max));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "\\ok.csv");
job.setJarByClass(Test.class);
FileSystem fs = FileSystem.get(conf);
if (fs.exists(new Path(args[1]))) {
fs.delete(new Path(args[1]), true);
}
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
job.setMapperClass(ColMapper.class);
job.setReducerClass(ColReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
【问题讨论】:
-
你没有传递任何命令行参数,是吗?您的代码需要两个,但在尝试使用它们之前没有查看是否有两个。
标签: java