1

I try to run a mapreduce in java, but get this error.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at com.mapreduce.WordCount.run(WordCount.java:23)
        at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
        at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:84)
        at com.mapreduce.WordCount.main(WordCount.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

WordCount.java

package com.mapreduce;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;

public class WordCount extends Configured implements Tool {

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

    public int run(String[] args) throws Exception {
        Path inputPath = new Path(args[0]);
        Path outputPath = new Path(args[1]);

        Configuration conf = getConf();
        Job job = new Job(conf, this.getClass().toString());

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);

        job.setJobName("WordCount");
        job.setJarByClass(WordCount.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);

        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        @Override
        public void map(LongWritable key, Text value,
                        Mapper.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> {

        @Override
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }

            context.write(key, new IntWritable(sum));
        }
    }

}

I undarstand that it means there are no elements in the array, but what do I need to do run it? I'm using Maven to import all the necessary libraries. I'm running it on Windows. The code is an example from http://www.macalester.edu/~shoop/sc13/hadoop/html/hadoop/wc-detail.html

3
  • The ArrayIndexOutOfBoundsException suggest that args[] is empty. Commented Oct 11, 2016 at 11:46
  • ArrayIndexOutOfBoundsException, yes its pretty self explaining. You are not getting the expected arguments. Commented Oct 11, 2016 at 11:49
  • I would advise you to start with a Java book first and then move to hadoop. It will be easier this way, than learning Java through Hadoop. Commented Oct 11, 2016 at 14:17

1 Answer 1

2

You need to launch your program with arguments corresponding to the input path and the output path, so your launch command should be of the next format:

java -cp <my-classpath-here> com.mapreduce.WordCount <input-path> <output-path>
Sign up to request clarification or add additional context in comments.

6 Comments

What do I do if I'm using windows?
what I propose in my answer is not OS dependent so it will work on windows OS too assuming that you provide properly the classpath and a valid input and output path
Would input and output path in this case point to a text document?
input path yes it must be a path to a text document, output path should be a path to the file that will contain the result of your map/reduce
Does my-classpath-here need to be a jar file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.