如何为arrywritable的Text版本编写compareTo()方法

时间:2015-03-28 07:04:41

标签: hadoop mapreduce

我正在编写mapreduce代码,我的中间映射器输出键是ArrayWritable的Text版本,我创建了一个类TextArrayWritable和扩展的ArrayWritable.Below是我的代码

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

}

如何为上面的类编写compareTo()方法我明白它需要通过实现WritableComparable接口来实现,但不知道如何编写方法。

编辑: 它失败并出现以下错误:

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 个答案:

答案 0 :(得分:0)

如果我正确地阅读了ArrayWritable documentation,那么创建一个TextArrayWritable只需要:

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