2013-01-07 23 views
1

我寫了一個自定義分區程序,但無法將其設置爲主類中的JobConf對象。無法將作業設置爲JobConf對象

import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapreduce.Partitioner; 

public class FirstCharTextPartitioner extends Partitioner<Text, Text> { 

    @Override 
    public int getPartition(Text key, Text value, int numReduceTasks) { 
     return (key.toString().charAt(0)) % numReduceTasks; 
    }  
} 

但是當我嘗試將它設置爲JobConf對象,我得到以下錯誤。
在類型JobConf方法setPartitionerClass(類)不適用的參數(類)

public class WordCount { 

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

     public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 
      String line = value.toString(); 
      String[] tokens = line.split("\\s"); 
      for (String token : tokens) { 
       word.set(token); 
       output.collect(word, one); 
      } 
     } 
    } 

    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { 
     public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 
      int sum = 0; 
      while (values.hasNext()) { 
       sum += values.next().get(); 
      } 
      output.collect(key, new IntWritable(sum)); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     JobConf conf = new JobConf(WordCount.class); 
     conf.setJobName("wordcount"); 

     conf.setOutputKeyClass(Text.class); 
     conf.setOutputValueClass(IntWritable.class); 

     conf.setMapperClass(Map.class); 
     conf.setCombinerClass(Reduce.class); 
     conf.setReducerClass(Reduce.class); 
     conf.setPartitionerClass(FirstCharTextPartitioner.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); 
    } 
} 

有人能告訴我什麼,我做錯了什麼?

回答

2

您正在導入org.apache.hadoop.mapreduce.Partitioner

您需要實現舊接口org.apache.hadoop.mapred.Partitioner,像這樣:

import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapred.Partitioner; 

public class FirstCharTextPartitioner implements Partitioner<Text, Text> { 

    @Override 
    public int getPartition(Text key, Text value, int numReduceTasks) { 
     return (key.toString().charAt(0)) % numReduceTasks; 
    }  
} 
+0

謝謝,這確實起作用。但是,請你解釋一下新的分區器用於什麼? – aa8y

+0

@Expressions_Galore它是一樣的東西,但抽象類基於適合「新」mapreduce API。舊的基於接口,而新的基於抽象類更容易擴展以供將來使用。 –

+0

因此,如果我想使用新的分區程序,我該如何修改代碼? – aa8y

相關問題