-2

The program's purpose is to look up the value on the basis of string from one file to another file string.

When I go to compile the program, it works fine. When I run the program,I get an error, saying

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)

My Code:

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);
    }
}
Sudhanshu
  • 15
  • 4
  • possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Raedwald Mar 29 '14 at 12:15

1 Answers1

1

Your arguments array seems to be having only two value but you are trying to get the third index value in the following statement leading to ArrayIndexOutOfBoundException:

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

Try to use debugger for checking the length of your array or try printing it using arguments.length

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136