mapreduce中reduce中的迭代器只能调用一次。其实迭代器就只能调用一次

亲测,只能调用一次,如果想想在一次reduce重复使用迭代器中的数据,得先取出来放在list中然后在从list中取出来!!多次读取reduce函数中迭代器的数据

 

public static void main(String[] args) { 
     List<String> l = new ArrayList<String>();
     l.add("aa");
     l.add("bb");
     l.add("cc");
     Iterator<String> iter = l.iterator();
     while (iter.hasNext()) {
      String str = (String)iter.next();
      System.out.println(str);
     }
     
     while (iter.hasNext()) {
          String str = (String)iter.next();
          System.out.println(str);
         }
}
    

根本原因是迭代器本来就只能调用一次,因为便利完之后指针就移动到最后了,再使用iter.hasNext()肯定烦返回的是false啊。由于reduce中的迭代器是已经提供好的,所以我们能做的只有调用,如果想再次遍历,只能在第一次便利时加入list中

http://www.cnblogs.com/amboyna/archive/2007/09/25/904804.html

  public static class FindFriendReducer extends Reducer<Text, AllInfoBean, AllInfoBean, NullWritable>
  {
    protected void reduce(Text Keyin, Iterable<AllInfoBean> values, Reducer<Text, AllInfoBean, AllInfoBean, NullWritable>.Context context)
      throws IOException, InterruptedException
    {
      AllInfoBean allInfoBean = new AllInfoBean();
      allInfoBean.setPid(Keyin.toString());
      for (AllInfoBean bean : values) {
        if (bean.getOderid() != 0)
          continue;
        allInfoBean.setPname(bean.getPname());
        allInfoBean.setCategory_id(bean.getCategory_id());
        allInfoBean.setPrice(bean.getPrice());
      }

      for (AllInfoBean bean : values)
      {
        context.write(bean, NullWritable.get());
      }
    }
  }

这样是取不出来数据的!!

 

转载于:https://www.cnblogs.com/rocky-AGE-24/p/6910797.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值