我正在尝试实现自定义WritableComparable,如In this link
所示当我尝试在mapper方法中初始化自定义可写的可比较类时,我收到错误。我在代码中显示了错误。 textpair类应该在sperate文件中吗?
public class Myprog {
public static class MyMap extends Mapper<Object, Text, TextPair, IntWritable> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
TextPair writable = new TextPair();
//ERROR in this line
//No enclosing instance of type Myprog is accessible. Must qualify
//the allocation with an enclosing instance of type Myprog
//(e.g. x.new A() where x is an instance of Myprog).
....
}
}
public static class MyReduce extends Reducer<TextPair, IntWritable, Text, Text> {
public void reduce(TextPair key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
}
}
public class TextPair implements WritableComparable<TextPair> {
....
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs =
new GenericOptionsParser(conf, args).getRemainingArgs();
Collections.addAll(headerList, header.split(","));
Job job = new Job(conf, "Myprog");
job.setJarByClass(Myprog.class);
job.setMapperClass(MyMap.class);
job.setReducerClass(MyReduce.class);
job.setMapOutputKeyClass(TextPair.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
答案 0 :(得分:1)
最好的方法是将TextPair放在一个单独的.java文件中。随着mapper和reducer的增长,将它们放在单独的文件中也会更好 话虽这么说,你也可以将你的TextPair类定义为静态,就像你的Mapper和Reducer一样。
答案 1 :(得分:1)
您必须将TextPair
定义为static
。如果没有Myprog
的外部实例,则无法实例化它。由于您的Mapper
是静态的,因此没有Myprog
的实例可供参考。
使用
public static class TextPair
将解决您的问题。