2016-08-09 52 views
1

Ich bin ein Anfänger versucht, map-reduce mit Java zu lernen. Ich versuche, das Wortzählungsbeispiel mit dem Oozie-Koordinator auszuführen. Ich erhalte einen Casting-Fehler. Es wäre toll, wenn mir jemand mit diesem Fehler helfen würde.Fehler: java.lang.ClassCastException: WordCountTest.WordCountTest kann nicht in org.apache.hadoop.mapreduce.Mapper

"Error: java.lang.ClassCastException: wordCountTest.WordCountTest cannot be cast to org.apache.hadoop.mapreduce.Mapper"

Hier ist mein WordCountTest.java Snippet:

package wordCountTest; 

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 WordCountTest { 
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 = new Job(conf,"WordCount"); 
    job.setJarByClass(WordCountTest.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); 
} 
} 

Auch meine workflow.xml Datei:

<?xml version="1.0" encoding="UTF-8"?> 
<workflow-app xmlns="uri:oozie:workflow:0.1" name="map-reduce-wf"> 
<start to="mr-node" /> 
<action name="mr-node"> 
    <map-reduce> 
    <job-tracker>${jobTracker}</job-tracker> 
    <name-node>${nameNode}</name-node> 
    <configuration> 
    <property> 
    <name>mapred.mapper.new-api</name> 
    <value>true</value> 
    </property> 
    <property> 
    <name>mapred.reducer.new-api</name> 
    <value>true</value> 
    </property> 
    <property> 
    <name>mapred.job.queue.name</name> 
    <value>${queueName}</value> 
    </property> 
    <property> 
    <name>mapreduce.map.class</name> 
    <value>wordCountTest.WordCountTest.TokenizerMapper</value> 
    </property> 
    <property> 
    <name>mapreduce.reduce.class</name> 
    <value>wordCountTest.WordCountTest.IntSumReducer</value> 
    </property> 
    <property> 
    <name>mapreduce.combine.class</name> 
    <value>wordCountTestEmr.WordCountTest.IntSumReducer</value> 
    </property> 
    <property> 
    <name>mapred.output.key.class</name> 
    <value>org.apache.hadoop.io.Text</value> 
    </property> 
    <property> 
    <name>mapred.output.value.class</name> 
    <value>org.apache.hadoop.io.IntWritable</value> 
    </property> 
    <property> 
    <name>mapred.input.dir</name> 
    <value>/user/ahegde/testemr/InputData/</value> 
    </property> 
    <property> 
    <name>mapred.output.dir</name> 
    <value>/user/ahegde/testemr/OutputData</value> 
    </property> 
    </configuration> 
    </map-reduce> 
<ok to="end" /> 
<error to="fail" /> 
</action> 
    <kill name="fail"> 
    <message>Map/Reduce failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message> 
    </kill> 
    <end name="end" /> 
</workflow-app> 
+0

überprüfen Sie diese [link] (http://stackoverflow.com/questions/21722173/getting-javal-lang-classcastexception-class-java-lang-string-in-running-a-einfach) –

+0

Warum Sie wollen Mapper- und Reducer-Klassen erstellen Statisch? Könnten Sie bitte statische Kartenklassen entfernen und Klassen reduzieren und wenn möglich auch herausnehmen und als separate Klassen im selben Paket erstellen und ausprobieren. – Aditya

Antwort

0

Sie müssen wahrscheinlich Ihre Oozie Config ändern, da diese statische innere sind Klassen zu:

<property> 
    <name>mapreduce.map.class</name> 
    <value>wordCountTest.WordCountTest$TokenizerMapper</value> 
</property> 

Die Änderung ist die . zu einer $.