zoukankan      html  css  js  c++  java
  • 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());
          }
        }
      }

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

  • 相关阅读:
    poj 1149 最大流
    poj 3281 最大流建图
    lightoj 1300 边双联通分量+交叉染色求奇圈
    lightoj 1291 无向图边双联通+缩点统计叶节点
    lightoj 1063 求割点
    lightoj 1026 无向图 求桥
    lightoj 1407 2-sat
    lightoj 1251 (Two_Sat)
    hdu 4681 最长公共子序列+枚举
    OD汇编需要标签
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/6910797.html
Copyright © 2011-2022 走看看