MapReduce - 迭代 Iterable<Text> 的偶数值

MapReduce - Iterate on even values of an Iterable<Text>

我正在尝试解决 mapreduce 问题。目前我得到了一个以这种方式组织我的 类 的方法映射:

    MapResult:    
    <key , "YES">
    <key , "NO">
    <key , "NO">
    <key , "YES">

我要做的是验证在这个集合的偶数条目中是否至少有一个 "YES" 值,并且在这个集合的奇数条目中至少有一个 "YES" 值.简而言之,如果 MapResult[0] == "YES" && MapResult[3] == YES.

我的条件是真的

这是我需要为 reduce 任务完成的代码:

public static class IntSumReducer
       extends Reducer<Text,Text,Text,Text> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<Text> values,
                       Context context
                       ) throws IOException, InterruptedException {

      for (Text value : values) {
        // Iteration on this values
      }

      //context.write();
    }
  }

您可以迭代可迭代对象并"flip"检查偶数和奇数元素:

布尔结果=假; 布尔 evenFound = false; 布尔 oddFound = false; boolean checkEven = true;

for (Text value : values) {
   if (checkEven) {
       evenFound |= "YES".equals(value);
   } else {
       oddFound |= "YES".equals(value);
   }

   if (evenFound && oddFound) {
       result = true;
       break;
   }

   checkEven = !checkEven;
}