Sahil
Sahil

Reputation: 9478

How is this word count bolt thread safe?

I am new with storm, I was going through the word count example of storm. Here is the bolt which keeps track of the counts

public static class WordCount 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++;
      counts.put(word, count);
      collector.emit(new Values(word, count));
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
      declarer.declare(new Fields("word", "count"));
    }
  }

From my understand, storm launches multiple threads for bolt which run in parallel, so how is using HashMap here thread-safe?

Upvotes: 1

Views: 473

Answers (1)

Related Questions