@Override public final void reduce(DatumWrapper<ITuple> key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException { try { Iterator<NullWritable> iterator = values.iterator(); tupleIterator.setIterator(iterator); ITuple currentTuple = key.datum(); ITuple previousKey = key.previousDatum(); int indexMismatch; if (firstRun) { indexMismatch = minDepth; firstRun = false; } else { indexMismatch = indexMismatch(previousKey, currentTuple, 0, maxDepth); if (indexMismatch < minDepth) { indexMismatch = minDepth; } for (int i = maxDepth; i >= indexMismatch; i--) { handler.onCloseGroup( i, groupSchema.getField(i).getName(), previousKey, this.context, collector); } } for (int i = indexMismatch; i <= maxDepth; i++) { handler.onOpenGroup( i, groupSchema.getField(i).getName(), currentTuple, this.context, collector); } // We set a view over the group fields to the method. if (isMultipleSources) { int schemaId = tupleMRConfig.getSchemaIdByName(currentTuple.getSchema().getName()); int[] indexTranslation = serInfo.getGroupSchemaIndexTranslation(schemaId); groupTuple.setContained(currentTuple, indexTranslation); } else { groupTuple.setContained(currentTuple); } handler.reduce(groupTuple, tupleIterator, this.context, collector); // This loop consumes the remaining elements that reduce didn't consume // The goal of this is to correctly set the last element in the next // onCloseGroup() call while (iterator.hasNext()) { iterator.next(); } } catch (TupleMRException e) { throw new RuntimeException(e); } }