MapReduce中ArrayWritable MapReduce中ArrayWritable 使用指南

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

MapReduce中ArrayWritable MapReduce中ArrayWritable 使用指南

  2021-03-19 我要评论
想了解MapReduce中ArrayWritable 使用指南的相关内容吗,在本文为您仔细讲解MapReduce中ArrayWritable的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:MapReduce,ArrayWritable,下面大家一起来学习吧。

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们