gpt4 book ai didi

java - 使用自定义 WritableComparable 时出错

转载 作者:可可西里 更新时间:2023-11-01 15:18:19 27 4
gpt4 key购买 nike

我正在尝试实现自定义 WritableComparable,如图所示 In this link

当我尝试在我的映射器方法中初始化自定义可写比较类时出现错误。我已经在我的代码中显示了我的错误。textpair 类应该放在单独的文件中吗?

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);
}

最佳答案

您必须将TextPair 定义为static。没有 Myprog 的外部实例就无法实例化。由于您的 Mapper 是静态的,因此它没有要引用的 Myprog 实例。

使用

public static class TextPair 

将解决您的问题。

关于java - 使用自定义 WritableComparable 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com