Quantcast
Channel: Hortonworks » All Replies
Viewing all articles
Browse latest Browse all 3435

Running Wordcount on Hadoop throw an Exception

$
0
0

Hi All,
Running Wordcount on Hadoop its show this Exception

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at WordCount.Word_Runner.main(Word_Runner.java:35)

package WordCount;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

public class Word_Mapper extends MapReduceBase implements Mapper<LongWritable , Text, Text, IntWritable >{

private final IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(LongWritable Key, Text Value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String line = Value.toString();
StringTokenizer t = new StringTokenizer(line);
while (t.hasMoreTokens())
{
word.set(t.nextToken());
output.collect(word, one);

}

}

}

package WordCount;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public class Word_Reducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable>{

@Override
public void reduce(Text keys, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
// TODO Auto-generated method stub
int sum = 0;
while (values.hasNext())
{
sum += values.next().get();
output.collect(keys,new IntWritable(sum));
}

}

}

package WordCount;

import java.io.IOException;

import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;

import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

public class Word_Runner
{
public static void main(String[] args)throws IOException
{
JobConf conf = new JobConf(Word_Runner.class);

conf.setJobName(“WordCount”);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Word_Mapper.class);
conf.setCombinerClass(Word_Reducer.class);
conf.setReducerClass(Word_Reducer.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf,new Path(args[0]));
FileOutputFormat.setOutputPath(conf,new Path(args[1]));

JobClient.runJob(conf);

}

}


Viewing all articles
Browse latest Browse all 3435

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>