Class convertedKeyClass, Class rawKeyClass, Class rawValueClass)
throws IOException {
SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf,
destinationFilePath, rawKeyClass, rawValueClass, CompressionType.NONE);
WritableComparable convertedKey;
MapWritable value;
Map<Integer, SequenceFile.Reader> readers = new HashMap<Integer, SequenceFile.Reader>();
for (int i = 0; i < status.length; i++) {
SequenceFile.Sorter sorter = new SequenceFile.Sorter(fs,
convertedKeyClass, MapWritable.class, conf);
sorter.setMemory(conf
.getInt("bsp.input.runtime.partitioning.sort.mb", 50) * 1024 * 1024);
sorter.setFactor(conf.getInt(
"bsp.input.runtime.partitioning.sort.factor", 10));
sorter.sort(status[i].getPath(), status[i].getPath().suffix(".sorted"));
readers.put(i,
new SequenceFile.Reader(fs, status[i].getPath().suffix(".sorted"),
conf));
}
for (int i = 0; i < readers.size(); i++) {
convertedKey = (WritableComparable) ReflectionUtils.newInstance(
convertedKeyClass, conf);
value = new MapWritable();
readers.get(i).next(convertedKey, value);
candidates.put(i, new KeyValuePair(convertedKey, value));
}
while (readers.size() > 0) {
convertedKey = (WritableComparable) ReflectionUtils.newInstance(
convertedKeyClass, conf);
value = new MapWritable();
int readerIndex = 0;
WritableComparable firstKey = null;
MapWritable rawRecord = null;
for (Map.Entry<Integer, KeyValuePair<WritableComparable, MapWritable>> keys : candidates
.entrySet()) {
if (firstKey == null) {
readerIndex = keys.getKey();
firstKey = keys.getValue().getKey();
rawRecord = (MapWritable) keys.getValue().getValue();
} else {
WritableComparable currentKey = keys.getValue().getKey();
if (firstKey.compareTo(currentKey) > 0) {
readerIndex = keys.getKey();
firstKey = currentKey;
rawRecord = (MapWritable) keys.getValue().getValue();
}
}
}
for (Map.Entry<Writable, Writable> e : rawRecord.entrySet()) {
writer.append(e.getKey(), e.getValue());
}
candidates.remove(readerIndex);