Spark Streaming Example :
- Start spark-shell
- Import required libraries :
- import org.apache.spark.SparkConf
- import org.apache.spark.streaming.{Seconds, StreamingContext}
- import org.apache.spark.storage.StorageLevel
- import StorageLevel._
- import org.apache.spark.streaming.StreamingContext.
- val ssc = new StreamingContext(sc, Seconds(2))
- val lines = ssc.socketTextStream("192.168.2.156",8585,MEMORY_ONLY)
4. Split and count number of words
- val wordsFlatMap = lines.flatMap(_.split(" "))
- val wordsMap =wordsFlatMap.map(w => (w,1))
- val wordCount = wordsMap.reduceByKey( (a,b) => (a+b))
- wordCount.print
5. ssc.start
No comments:
Post a Comment