欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MapReduce中ArrayWritable 使用指南

程序员文章站 2024-02-28 12:45:52
在编写mapreduce程序时,map和reduce之间传递的数据需要是arraylist类型的,在调试运行时遇到了这样的一个错误: java.lang.runt...

在编写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);
 }
}