亲测,只能调用一次,如果想想在一次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()); } } }
这样是取不出来数据的!!