我正在编写一个mapreduce代码,我的中间Map器输出键是arraywritable的文本版本,我创建了一个类textarraywritable和扩展arraywritable。下面是我的代码
公共类textarraywritable扩展了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()方法我理解它需要通过实现writeablecomparable接口来实现,但不确定如何编写方法。
编辑:失败,错误如下:
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条答案
按热度按时间lyr7nygr1#
如果我在读
ArrayWritable
正确的文档,创建TextArrayWritable
您所需要的就是: