简体   繁体   English

Apache Giraph / Hadoop:通过自定义ArrayWritable进行迭代

[英]Apache Giraph/Hadoop: Iterating through custom ArrayWritable

I thought this would be simple to implement, but it's starting to be a pain. 我认为这很容易实现,但是开始很痛苦。

I've got a ArrayWritable subclass like so: 我有一个像这样的ArrayWritable子类:

public class VertexDistanceArrayWritable extends ArrayWritable {
    public VertexDistanceArrayWritable() {
        super(VertexDistanceWritable.class);
    }
    public VertexDistanceArrayWritable(VertexDistanceWritable[] v) {
        super(VertexDistanceWritable.class, v);
    }
}

And a Writable subclass like so: 像这样的Writable子类:

public class VertexDistanceWritable implements Writable {

    //Implements write, readFields, and some custom functions that aren't used yet

}

In my Giraph compute function, Messages are VertexDistanceArrayWritable's. 在我的Giraph计算函数中,消息是VertexDistanceArrayWritable的。 I want to iterate through every VertexDistanceWritable every message (VertexDistanceArrayWritable). 我想遍历每个消息(VertexDistanceArrayWritable)的每个VertexDistanceWritable。 Here is my compute function: 这是我的计算功能:

@Override
public void compute(Vertex<Text, MapWritable, FloatWritable> vertex,
    Iterable<VertexDistanceArrayWritable> messages) throws IOException {

    for(VertexDistanceArrayWritable message : messages) {
        for(VertexDistanceWritable distEntry : message) {
            //Do stuff with distEntry
        }
    }

    //do other stuff

    vertex.voteToHalt();
}

When I compile the code, I get this error: 编译代码时,出现以下错误:

for-each not applicable to expression type
    for(VertexDistanceWritable distEntry : message) {

  required: array or java.lang.Iterable
  found:    VertexDistanceArrayWritable

So now I have a problem. 所以现在我有一个问题。 I want to iterate over the arrayWritable sub-class. 我想遍历arrayWritable子类。

I've tried the following: 我尝试了以下方法:

  • Change that line to for(VertexDistanceWritable distEntry : message.toArray()) which tells me that for-each not applicaable to type Object (required: array or java.lang.Iterable, found: Object). 将该行更改为for(VertexDistanceWritable distEntry : message.toArray()) ,这告诉我for-each均不适用于键入Object(必需:array或java.lang.Iterable,找到:Object)。

  • Change that line to for(VertexDistanceWritable distEntry : message.get() ) , which gives me error: incompatible types -- required: VertexDistanceWritable, found: Writable . 将该行更改为for(VertexDistanceWritable distEntry : message.get() ) ,这会给我error: incompatible types - required: VertexDistanceWritable, found: Writable This is the strangest problem -- VertexDistanceWritable extends Writable, shouldn't this work fine? 这是最奇怪的问题-VertexDistanceWritable扩展了Writable,这不行吗?

  • Writing my own custom "get_foo()" function for VertexDistanceWritable, which returns values as a VertexDistanceWritable[]. 为VertexDistanceWritable编写我自己的自定义“ get_foo()”函数,该函数以VertexDistanceWritable []的形式返回值。 Of course, values is private, and has no getter function according to the documentation other than get() which I'm already having problems with 当然,值是私有的,根据文档 (除了get()之外)没有getter函数,我已经遇到了问题

I just want a way to iterate over my VertexDistanceArrayWritable class. 我只想要一种遍历我的VertexDistanceArrayWritable类的方法。 Is this even possible in Hadoop? 在Hadoop中甚至可能吗? It has to be, right? 一定是吧? I should be able to iterate over a bunch of elements I made in an array, no? 我应该能够遍历在数组中创建的一堆元素,不是吗? It seems like pretty darn basic stuff. 看起来很基本的东西。

After about 30 minutes of experimenting and googling, I found out a clue here . 经过大约30分钟的实验和谷歌搜索,我在这里找到了一个线索。 Sort of cheesy, but it seems to compile correctly. 有点俗气,但似乎可以正确编译。 Basically just use a Writable then cast it to my custom writable. 基本上只使用可写对象,然后将其转换为我的自定义可写对象。

for(VertexDistanceArrayWritable message : messages) {
    for(Writable distWritable : message.get()) {
        vertexDistanceWritable distEntry = (VertexDistanceWritable) distWritable;
        //do other stuff
    }
}

I haven't yet confirmed if it works correctly, I will update and confirm my answer when I can make sure it works. 我尚未确认它是否可以正常工作,我将在确定可以正常工作时进行更新并确认我的答案。

edit: it works. 编辑:它有效。 Might require a copy constructor since I had one for VertexDistanceWritable, but never checked that out. 可能需要一个拷贝构造函数,因为我有一个VertexDistanceWritable构造函数,但从未检查过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM