yesh
yesh

Reputation: 2070

Error in using Custom WritableComparable

I am trying to implement a Custom WritableComparable as shown In this link

I am getting an error when I am trying to initialize the custom writable comparable class inside my mapper method. I have shown my error in side my code. Should textpair class be in a sperate file ?

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

Upvotes: 1

Views: 655

Answers (2)

Thomas Jungblut
Thomas Jungblut

Reputation: 20969

You have to define your TextPair as static. It can not be instantiated without the outer instance of Myprog. Since your Mapper is static, it has no instance of Myprog to refer to.

Using

public static class TextPair 

will resolve your problem.

Upvotes: 1

Frederic
Frederic

Reputation: 3284

The best way to go is to put your TextPair in a separate .java file. As your mapper and reducer grow, it's also better to put them in separate files, too.
That being said, you could also define your TextPair class as static as your Mapper and Reducer are.

Upvotes: 1

Related Questions