程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> MapReduce中ArrayWritable 使用指南

MapReduce中ArrayWritable 使用指南

編輯:JAVA編程入門知識

在編寫MapReduce程序時,Map和Reduce之間傳遞的數據需要是ArrayList類型的,在調試運行時遇到了這樣的一個錯誤:

java.lang.RuntimeException: java.lang.NoSuchMethodException: org.apache.hadoop.io.ArrayWritable.<init>()

經查詢官網API文檔後發現這樣的一段話:

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

原來是要自己實現一個ArrayWritable類的派生類,使用時只要實現兩個構造函數即可

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);
 }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved