Building Data Streaming Applications with Apache Kafka by Manish Kumar & Chanchal Singh

Building Data Streaming Applications with Apache Kafka by Manish Kumar & Chanchal Singh

Author:Manish Kumar & Chanchal Singh [Kumar, Manish]
Language: eng
Format: azw3
Tags: COM062000 - COMPUTERS / Data Modeling and Design, COM018000 - COMPUTERS / Data Processing, COM051280 - COMPUTERS / Programming Languages / Java
Publisher: Packt Publishing
Published: 2017-08-18T04:00:00+00:00


Wordcount Bolt: Example of Wordcount Bolt is given next:

import org.apache.Storm.task.OutputCollector import org.apache.Storm.task.TopologyContext import org.apache.Storm.topology.IRichBolt import org.apache.Storm.topology.OutputFieldsDeclarer import org.apache.Storm.tuple.Tuple import java.util.HashMap import java.util.Map class WordCountCalculatorBolt extends IRichBolt { var wordCountMap: Map[String, Integer] = _ private var collector: OutputCollector = _ def prepare(StormConf: Map[_, _], context: TopologyContext, collector: OutputCollector): Unit = { this.wordCountMap = new HashMap[String, Integer]() this.collector = collector } def execute(input: Tuple): Unit = { var str: String = input.getString(0) str = str.toLowerCase().trim() if (!wordCountMap.containsKey(str)) { wordCountMap.put(str, 1) } else { val c: java.lang.Integer = wordCountMap.get(str) + 1 wordCountMap.put(str, c) } collector.ack(input) } def cleanup(): Unit = { for ((key, value) <- wordCountMap) { println(key + " : " + value) } } override def declareOutputFields(declarer: OutputFieldsDeclarer): Unit = {} override def getComponentConfiguration(): Map[String, Any] = null }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.