0
votes

I am writing a mapreduce code and my intermediate mapper output key is a Text version of ArrayWritable and i have created a class TextArrayWritable and extended ArrayWritable.Below is my code

public class TextArrayWritable extends ArrayWritable {

//private Writable[] values;
public TextArrayWritable()
{
    super(Text.class);

}
public TextArrayWritable(String[] strings)
{
    super(Text.class);
    Text[] texts=new Text[strings.length];
    for(int i=0;i<strings.length;i++)
    {
        texts[i]=new Text(strings[i]);
    }
    set(texts);
}
public String[] returnarray(Text[] mytext)
{
    //super(Text.class);
    String[] mystr=new String[mytext.length];
    //ArrayList<String> mylist=new ArrayList<String>();

    for(int i=0;i<mystr.length;i++)
    {
        mystr[i]=mystr[i].toString();
    }
    return mystr;
}
 public Text[] toArray() {
     return (Text[]) super.toArray();
     }  

}

How to write a compareTo() method for the above class i understand it needs to implemented by implementing WritableComparable interface but not sure how to write a method.

Edit: It fails with below error:

java.lang.Exception: java.lang.ClassCastException: class TextArrayWritable
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:354)
Caused by: java.lang.ClassCastException: class TextArrayWritable
    at java.lang.Class.asSubclass(Class.java:3208)
    at org.apache.hadoop.mapred.JobConf.getOutputKeyComparator(JobConf.java:795)
    at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.<init>(MapTask.java:964)
    at org.apache.hadoop.mapred.MapTask$NewOutputCollector.<init>(MapTask.java:673)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:756)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:364)
    at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:223)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
1

1 Answers

0
votes

If I'm reading the ArrayWritable documentation correctly, to create a TextArrayWritable all you need is this:

public class TextArrayWritable extends ArrayWritable {
    public TextArrayWritable() { super(Text.class); }
}