【问题标题】:Implementation of an ArrayWritable for a custom Hadoop type为自定义 Hadoop 类型实现 ArrayWritable
【发布时间】:2010-12-08 11:10:26
【问题描述】:

如何为自定义 Hadoop 类型定义 ArrayWritable?我正在尝试在 Hadoop 中实现倒排索引,并使用自定义 Hadoop 类型来存储数据

我有一个 Individual Posting 类,它存储术语频率、文档 ID 和文档中术语的字节偏移列表。

我有一个 Posting 类,它有一个文档频率(该术语出现的文档数量)和个人帖子列表

我已经为 IndividualPostings

中的字节偏移列表定义了一个 LongArrayWritable 扩展 ArrayWritable 类

当我为 IndividualPosting 定义自定义 ArrayWritable 时,我在本地部署后遇到了一些问题(使用 Karmasphere、Eclipse)。

Posting 类列表中的所有 IndividualPosting 实例都是相同的,即使我在 Reduce 方法中得到不同的值

【问题讨论】:

  • 您能具体解释一下问题所在吗?也许为您的自定义 ArrayWritable 发布一些代码?

标签: hadoop mapreduce


【解决方案1】:

来自ArrayWritable的文档:

包含类实例的数组的可写对象。这个可写的元素必须都是同一个类的实例。如果这个 writable 将成为 Reducer 的输入,您将需要创建一个子类,将值设置为正确的类型。例如:public class IntArrayWritable extends ArrayWritable { public IntArrayWritable() { super(IntWritable.class); } }

您已经引用了使用 Hadoop 定义的 WritableComparable 类型执行此操作。这是我假设你的实现对于LongWritable 的样子:

public static class LongArrayWritable extends ArrayWritable
{
    public LongArrayWritable() {
        super(LongWritable.class);
    }
    public LongArrayWritable(LongWritable[] values) {
        super(LongWritable.class, values);
    }
}

您应该能够使用任何实现WritableComparable 的类型来执行此操作,如the documentation 所给出的。使用他们的例子:

public class MyWritableComparable implements
        WritableComparable<MyWritableComparable> {

    // Some data
    private int counter;
    private long timestamp;

    public void write(DataOutput out) throws IOException {
        out.writeInt(counter);
        out.writeLong(timestamp);
    }

    public void readFields(DataInput in) throws IOException {
        counter = in.readInt();
        timestamp = in.readLong();
    }

    public int compareTo(MyWritableComparable other) {
        int thisValue = this.counter;
        int thatValue = other.counter;
        return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
    }
}

应该就是这样。这假设您使用的是 Hadoop API 的修订版 0.20.20.21.0

【讨论】:

    猜你喜欢
    • 2016-09-25
    • 2013-11-11
    • 2015-02-27
    • 2023-03-29
    • 2017-01-07
    • 2020-03-13
    • 1970-01-01
    • 2018-10-05
    • 2016-04-13
    相关资源
    最近更新 更多