Iterate twice on values (MapReduce)

前端 未结 11 1070
轮回少年
轮回少年 2020-11-29 07:22

I receive an iterator as argument and I would like to iterate on values twice.

public void reduce(Pair key, Iterator          


        
11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 08:17

    After searching and doing so many tries and errors, I found a solution.

    1. Declare a new collection (say cache) (linked list or Arraylist or any else)

    2. Inside first iteration, assign the current iterator like below example:

      cache.add(new Text(current.get()))  
      
    3. Iterate through cache:

      for (Text count : counts) {
          //counts is iterable object of Type Text
          cache.add(new Text(count.getBytes()));
      }
      for(Text value:cache) {
          // your logic..
      }
      

提交回复
热议问题