问题
I am writing a MR job where in the reducer side, I need to check the size of Iterable before doing anything. Someone did asked the same question long back(How to find the size of an Iterable Object?) but the solutions given are not working.Since the Iterable doesnt have a size() method, please suggesst me how to do it. I tried the following options.
Tried to get a iterator object from iterable, but got following typecast error. Same error with ResettableIterator.
java.lang.ClassCastException: org.apache.hadoop.mapreduce.ReduceContext$ValueIterator cannot be cast to java.util.ListIterator
If i iterate to the end of the Iterable object to get size, then How can I go back to the start of the index.
if(values instanceof Collection){ return ((Collection)values).size(); }
this doesnt get hit as the values is of different type.
Please help me with some sample code. This issue has really taken a lot of time. Thanks a lot.!!
回答1:
Iterator iterator = Use values.iterator();
to get iterator.- Store values in
List
or some other data structure and then iterate over it.
来源:https://stackoverflow.com/questions/17650119/resetting-iterable-object