2016-10-11 5 views
1

Ich versuche, eine mapreduce in Java auszuführen, aber diesen Fehler zu bekommen.MapReduce java.lang.ArrayIndexOutOfBoundsException: 0

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)); 
     } 
    } 

} 

ich undarstand, dass es bedeutet, dass keine Elemente in der Anordnung gibt es, aber was brauche ich es zu tun laufen? Ich benutze Maven, um alle notwendigen Bibliotheken zu importieren. Ich benutze es unter Windows. Der Code ist ein Beispiel von http://www.macalester.edu/~shoop/sc13/hadoop/html/hadoop/wc-detail.html

+0

Die ArrayIndexOutOfBoundsException schlage vor, dass args [] leer ist. – Orejano

+0

ArrayIndexOutOfBoundsException, ja es ist ziemlich selbsterklärend. Sie erhalten nicht die erwarteten Argumente. – eldo

+0

Ich würde Ihnen empfehlen, zuerst mit einem Java-Buch zu beginnen und dann zu hadoop zu gehen. Es wird einfacher, als Java über Hadoop zu lernen. – vefthym

Antwort

2

Sie müssen Ihr Programm mit Argumenten starten entsprechend dem Eingangspfad und dem Ausgangspfad, so dass Ihr Startbefehl des nächsten Format sein sollte:

java -cp <my-classpath-here> com.mapreduce.WordCount <input-path> <output-path> 
+0

Was mache ich, wenn ich Windows verwende? –

+0

was ich vorschlagen in meiner Antwort ist nicht Betriebssystem abhängig, so wird es auf Windows OS auch unter der Annahme, dass Sie ordnungsgemäß den Klassenpfad und einen gültigen Eingabe-und Ausgabepfad geben –

+0

Würde Eingabe und Ausgabe Pfad in diesem Fall auf ein Textdokument zeigen? –

Verwandte Themen