Database Reference
In-Depth Information
Tweaking the WordCount topology to customize it
Now that we have deployed the WordCount topology in distributed mode, let's tweak the
code in the bolts a bit to write WordCount onto a file. To achieve this, we will proceed
with the following steps:
1. We intend to create a new bolt, FileWriterBolt , to achieve this. Open
WordCountTopology.java and add the following snippet to WordCoun-
tTopology.java :
public static class FileWriterBolt extends
BaseBasicBolt {
Map<String, Integer> counts = new HashMap<String,
Integer>();
@Override
public void execute(Tuple tuple,
BasicOutputCollector collector) {
String word = tuple.getString(0);
Integer count = counts.get(word);
if(count==null) {count = 0;
count = 0;
}
count++;
counts.put(word, count);
OutputStream ostream;
try {
ostream = new FileOutputStream("~/
wordCount.txt", true);
ostream.write(word.getBytes());
ostream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
collector.emit(new Values(word, count));
}
@Override
Search WWH ::




Custom Search