码迷,mamicode.com
首页 > 其他好文 > 详细

MapReduce中ArrayWritable 使用方法

时间:2014-08-30 16:25:09      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   使用   io   java   ar   for   数据   

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

我的个人博客:http://www.yancey.info/?p=188

  

MapReduce中ArrayWritable 使用方法

标签:blog   http   os   使用   io   java   ar   for   数据   

原文地址:http://www.cnblogs.com/yancey/p/3946513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!