BSPPeer<VectorWritable, NullWritable, IntWritable, VectorWritable, CenterMessage> peer)
throws IOException {
final NullWritable value = NullWritable.get();
// also use our cache to speed up the final writes if exists
if (cache == null) {
final VectorWritable key = new VectorWritable();
IntWritable keyWrite = new IntWritable();
while (peer.readNext(key, value)) {
final int lowestDistantCenter = getNearestCenter(key.getVector());
keyWrite.set(lowestDistantCenter);
peer.write(keyWrite, key);
}
} else {
IntWritable keyWrite = new IntWritable();
for (DoubleVector v : cache) {
final int lowestDistantCenter = getNearestCenter(v);
keyWrite.set(lowestDistantCenter);
peer.write(keyWrite, new VectorWritable(v));
}
}
// just on the first task write the centers to filesystem to prevent
// collisions
if (peer.getPeerName().equals(peer.getPeerName(0))) {
String pathString = conf.get(CENTER_OUT_PATH);
if (pathString != null) {
final SequenceFile.Writer dataWriter = SequenceFile.createWriter(
FileSystem.get(conf), conf, new Path(pathString),
VectorWritable.class, NullWritable.class, CompressionType.NONE);
for (DoubleVector center : centers) {
dataWriter.append(new VectorWritable(center), value);
}
dataWriter.close();
}
}
}