java数组reduce,MapReduce中ArrayWritable 使用指南

在编写MapReduce程序时,Map和Reduce之间传递的数据需要是ArrayList类型的,在调试运行时遇到了这样的一个错误:

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

经查询官网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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值