在編寫MapReduce程序時(shí),Map和Reduce之間傳遞的數(shù)據(jù)需要是ArrayList類型的,在調(diào)試運(yùn)行時(shí)遇到了這樣的一個(gè)錯(cuò)誤:
java.lang.RuntimeException: java.lang.NoSuchMethodException: org.apache.hadoop.io.ArrayWritable.<init>()
經(jīng)查詢官網(wǎng)API文檔后發(fā)現(xiàn)這樣的一段話:
A Writable for arrays containing instances of a class. The elements of this writable must all be instances of the same class. If this writable will be the input for a Reducer, you will need to create a subclass that sets the value to be of the proper type. For example: public class IntArrayWritable extends ArrayWritable { public IntArrayWritable() { super(IntWritable.class); } }
原來是要自己實(shí)現(xiàn)一個(gè)ArrayWritable類的派生類,使用時(shí)只要實(shí)現(xiàn)兩個(gè)構(gòu)造函數(shù)即可
public static class TextArrayWritable extends ArrayWritable { 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); }}
新聞熱點(diǎn)
疑難解答
圖片精選