如何为可写的文本版本编写compareTo()方法



我正在编写一个mapreduce代码,我的中间映射器输出键是ArrayWritable的Text版本,我已经创建了一个类TextArrayWrutable和扩展的ArrayWretable。下面是我的代码

公共类TextArrayWritable扩展ArrayWretable{

//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)

如果我正确阅读了ArrayWritable文档,要创建TextArrayWritable,您只需要以下内容:

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

相关内容

  • 没有找到相关文章

最新更新