当前位置: 移动技术网 > IT编程>开发语言>Java > MapReduce中ArrayWritable 使用指南

MapReduce中ArrayWritable 使用指南

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网