线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2

时间:2013-11-30 17:59:03

标签: java eclipse hadoop

该程序的目的是根据字符串从一个文件到另一个文件字符串查找值。

当我去编译程序时,它工作正常。当我运行程序时,我收到错误,说

ERROR:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
        at StockAnalyzer.run(StockAnalyzer.java:102)
        at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65)
        at StockAnalyzer.main(StockAnalyzer.java:119)
        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:160)

我的代码:

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.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.InputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.lib.MultipleInputs;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class StockAnalyzer extends Configured implements Tool 

{

    public class StockAnalysisMapper1 extends MapReduceBase implements Mapper<Text, Text, Text, Text>
    {
    private String Commonkey, Stockadj, FileTag = "f1~";

    @Override
    public void map(Text key, Text value,OutputCollector<Text, Text> output, Reporter reporter)
        throws IOException 
        {

        String values[] = value.toString().split(",");

        Commonkey = values[1].trim()+values[2].trim();
        Stockadj = values[8].trim();

        output.collect(new Text(Commonkey), new Text(FileTag + Stockadj));
      }
    }

    public class StockAnalysisMapper2 extends MapReduceBase implements Mapper <Text, Text, Text, Text> {

        private String Commonkey, Dividend, FileTag = "f2~";

        @Override
        public void map(Text key, Text value,OutputCollector<Text, Text> output, Reporter reporter)
            throws IOException {

            String values[] = value.toString().split(",");

            Commonkey = values[1].trim()+values[2].trim();
            Dividend = values[3].trim();

            output.collect(new Text(Commonkey), new Text(FileTag + Dividend));
          }
        }

    public class StockAnalysisReducer extends MapReduceBase  implements Reducer<Text, Text, Text, Text> 

    {

    private String Stockadj, Dividend;

    @Override
    public void reduce(Text key, Iterator<Text> values,OutputCollector<Text, Text> output, Reporter reporter)
                      throws IOException 
       {
        while (values.hasNext()) 
        {
        String currValue = values.next().toString();
        String splitVals[] = currValue.split("~");

      if (splitVals[0].equals("F1")) 
      {
         Stockadj = splitVals[1] != null ? splitVals[1].trim(): "Stockadj";
      } 
      else if (splitVals[0].equals("F2"))
      {
          Dividend = splitVals[2] != null ? splitVals[2].trim(): "Dividend";
      }
     output.collect(new Text(Stockadj), new Text(Dividend));
     }
    }
    }

    @Override

    public int run(String [] arguments) throws Exception
    {
        JobConf conf = new JobConf(getConf(),StockAnalyzer.class);
        conf.setJobName("Stock Analysis"); 

        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(Text.class);

        conf.setReducerClass(StockAnalysisReducer.class);

        Path Mapper1InputPath = new Path(arguments[0]);
        Path Mapper2InputPath = new Path(arguments[1]);
        Path OutputPath = new Path(arguments[2]);

        MultipleInputs.addInputPath(conf,Mapper1InputPath,
                    (Class<? extends InputFormat>) TextInputFormat.class,StockAnalysisMapper1.class);

        MultipleInputs.addInputPath(conf, Mapper2InputPath,
                    (Class<? extends InputFormat>) TextInputFormat.class,StockAnalysisMapper2.class);

        FileOutputFormat.setOutputPath(conf, OutputPath);

        JobClient.runJob(conf);

        return 0;

    }
    public static void main(String [] args) throws Exception
    {
         int res = ToolRunner.run(new Configuration(),new StockAnalyzer(), args);
         System.exit(res);
    }
}

1 个答案:

答案 0 :(得分:1)

您的arguments数组似乎只有两个值,但您试图在以下语句中获取第三个索引值,导致ArrayIndexOutOfBoundException

    Path OutputPath = new Path(arguments[2]);

尝试使用调试器检查数组的长度,或尝试使用arguments.length

进行打印