2016-04-26 24 views
0

我最近安裝了一個節點hadoop集羣,併成功地從輸入文件運行wordcount作業。現在我試圖運行與Maven相同的罐子,但我通過這個錯誤傳來:hadoop maven Java類null:InvocationTargetException

[WARNING] 
java.lang.reflect.InvocationTargetException 
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.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 
at WordCount.main(WordCount.java:54) 
... 6 more 
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven 
plugin:1.4.0:java (default-cli) on project avddb-wordcount: An exception 
occured while executing the Java class. null: InvocationTargetException 
-> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with 
the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, 
please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN 
/MojoExecutionException 

我通過終端與CMDS運行:

mvn compile 
mvn package 
mvn exec:java -Dexec.mainClass="WordCount" -Dexec.args="src/main/resources/input1.txt" 

我的POM文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.ece.lab</groupId> 
    <artifactId>avddb-wordcount</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-common</artifactId> 
      <version>2.6.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-core</artifactId> 
      <version>1.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-hdfs</artifactId> 
      <version>2.6.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-client</artifactId> 
      <version>2.6.0</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

的源代碼是這樣的:

import java.io.IOException; 
import java.util.StringTokenizer; 
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapreduce.Job; 
import org.apache.hadoop.mapreduce.Mapper; 
import org.apache.hadoop.mapreduce.Reducer; 
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 

public class WordCount { 

public static class TokenizerMapper extends Mapper<Object, Text, Text, 
IntWritable>{ 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    public void map(Object key, Text value, Context context) throws 
    IOException, InterruptedException { 
    StringTokenizer itr = new StringTokenizer(value.toString()); 
    while (itr.hasMoreTokens()) { 
    word.set(itr.nextToken()); 
    context.write(word, one); 
    } 
    } 
} 

public static class IntSumReducer 
    extends Reducer<Text,IntWritable,Text,IntWritable> { 
private IntWritable result = new IntWritable(); 

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

public static void main(String[] args) throws Exception { 
Configuration conf = new Configuration(); 
Job job = Job.getInstance(conf, "word count"); 
job.setJarByClass(WordCount.class); 
job.setMapperClass(TokenizerMapper.class); 
job.setCombinerClass(IntSumReducer.class); 
job.setReducerClass(IntSumReducer.class); 
job.setOutputKeyClass(Text.class); 
job.setOutputValueClass(IntWritable.class); 
FileInputFormat.addInputPath(job, new Path(args[0])); 
FileOutputFormat.setOutputPath(job, new Path(args[1])); 
System.exit(job.waitForCompletion(true) ? 0 : 1); 
} 
} 

有什麼想法?

+0

您可以將您的源代碼之前,請檢查您的數組的大小? – FearlessHyena

+0

我發佈了源代碼。 –

回答

1

你的數組大小大概是1,因此你路過的最後一個索引

既然你喜歡這個

運行它
mvn exec:java -Dexec.mainClass="WordCount" -Dexec.args="src/main/resources/input1.txt" 

您的參數只有1個元素(輸入路徑),它位於索引[0]處。您需要添加一個提供輸出路徑的額外參數(或者如果沒有指定任何參數,請選擇默認路徑)。此外,如果您的參數表是

if(args.length > 0) { FileInputFormat.addInputPath(job, new Path(args[0])); } else { //throw an exception } if(args.length > 1) { FileOutputFormat.setOutputPath(job, new Path(args[1])); } else { FileOutputFormat.setOutputPath(job, new Path("<Default output path here>"); }

然後像這樣如運行

mvn exec:java -Dexec.mainClass="WordCount" -Dexec.args="'src/main/resources/input1.txt' 'src/main/output/output.txt'" 

它總是好的做法,以訪問其內容,以避免一個ArrayIndexOutOfBoundsException

+0

args [0]或args [1]在輸出路徑中?如果輸入是args [0]? –

+0

我將其改爲參數[1]並給出了參數的輸出路徑,謝謝你的幫助 –

+0

沒問題。在查看您的來源後編輯我的答案。如果您未指定默認輸出路徑,則可以在將來獲取默認輸出路徑 – FearlessHyena

1

有一個在您wordcount應用程序的錯誤:

Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at WordCount.main(WordCount.java:54)

+0

第54行:FileOutputFormat.setOutputPath(job,new Path(args [1]));這是主要的課程。我怎樣才能解決這個問題? –

+0

如果它在主類中,那麼我猜測'args []'是由調用者提供的。如果是這種情況,那麼你需要用它需要的參數來調用你的main方法。 –