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);
}
}
有人能告訴我什麼,我做錯了什麼?
謝謝,這確實起作用。但是,請你解釋一下新的分區器用於什麼? – aa8y
@Expressions_Galore它是一樣的東西,但抽象類基於適合「新」mapreduce API。舊的基於接口,而新的基於抽象類更容易擴展以供將來使用。 –
因此,如果我想使用新的分區程序,我該如何修改代碼? – aa8y