MapReduce中ArrayWritable 使用指南
在编写MapReduce程序时,Map和Reduce之间传递的数据需要是ArrayList类型的,在调试运行时遇到了这样的一个错误:
java.lang.RuntimeException:java.lang.NoSuchMethodException:org.apache.hadoop.io.ArrayWritable.<init>()
经查询官网API文档后发现这样的一段话:
AWritableforarrayscontaininginstancesofaclass.Theelementsofthiswritablemustallbeinstancesofthesameclass.IfthiswritablewillbetheinputforaReducer,youwillneedtocreateasubclassthatsetsthevaluetobeofthepropertype.Forexample:publicclassIntArrayWritableextendsArrayWritable{publicIntArrayWritable(){super(IntWritable.class);}}
原来是要自己实现一个ArrayWritable类的派生类,使用时只要实现两个构造函数即可
publicstaticclassTextArrayWritableextendsArrayWritable{ publicTextArrayWritable(){ super(Text.class); } publicTextArrayWritable(String[]strings){ super(Text.class); Text[]texts=newText[strings.length]; for(inti=0;i<strings.length;i++){ texts[i]=newText(strings[i]); } set(texts); } }