Last active
January 24, 2019 16:28
-
-
Save rssanders3/66cef52b59e26ef423425034416dbd7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val sparkConf = new SparkConf().setAppName("DirectKafkaWordCount") | |
| val ssc = new StreamingContext(sparkConf, Seconds(2)) | |
| // Create direct kafka stream with brokers and topics | |
| val topicsSet = topics.split(",").toSet | |
| val kafkaParams = Map[String, Object]( | |
| ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG -> brokers, | |
| ConsumerConfig.GROUP_ID_CONFIG -> groupId, | |
| ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG -> classOf[StringDeserializer], | |
| ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG -> classOf[StringDeserializer]) | |
| val messages = KafkaUtils.createDirectStream[String, String]( | |
| ssc, | |
| LocationStrategies.PreferConsistent, | |
| ConsumerStrategies.Subscribe[String, String] | |
| (topicsSet, kafkaParams)) | |
| // Get the lines, split them into words, count the words and print | |
| val lines = messages.map(_.value) | |
| val words = lines.flatMap(_.split(" ")) | |
| val wordCounts = words.map(x => (x, 1L)).reduceByKey(_ + _) | |
| wordCounts.print() | |
| // Start the computation | |
| ssc.start() | |
| ssc.awaitTermination() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment