Suzy Tros Suzy Tros - 1 month ago 18
Java Question

how to emit and collect an arraylist in storm?

I have an arrayList that I want to emit and receive it in another bolt.
So as suggested on another post here I used:
the first bolt:

collector.emit(new Values(listI));


the next bolt:

public void execute(Tuple tuple) {
ArrayList<Integer> i = (ArrayList<Integer>)tuple.getValue(0);
....
}


but instead of a size 4 list, I am getting a list with size 0.
Any thoughts?

Answer

Not sure what type listI is (I'm assuming an ArrayList). If so, your first bolt might have this:

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    List<Integer> listI = getSomeListOfIntegers();
    collector.emit(new Values(listI));
}

Make sure you also declare it in bolt 1:

@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
    declarer.declare(new Fields("listI"));
}

Then in your second bolt:

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    List<Integer> listOfInts = (List<Integer>)tuple.getValue(0);
}