Created
September 3, 2017 08:10
-
-
Save mannharleen/3f77b221ef70dce939759a1991c69359 to your computer and use it in GitHub Desktop.
Wordcount from an input stream within 1) a batch 2) a window
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
| import org.apache.spark.streaming.StreamingContext | |
| import org.apache.spark.streaming.Seconds | |
| import org.apache.spark.{SparkConf, SparkContext} | |
| /* | |
| Streaming batch size = 10 seconds | |
| Window size = 20 secs | |
| Window slide interval = 10 secs | |
| */ | |
| val conf = new SparkConf().setAppName("sbtapp").setMaster("local[3]") | |
| val ssc = new StreamingContext(conf,Seconds(10)) | |
| ssc.sparkContext.setLogLevel("ERROR") | |
| val dStream = ssc.socketTextStream("localhost",9999) | |
| val pairs = dStream.map(x=> (x,1)) //create key value | |
| // word count in the batch | |
| val wcount = pairs.reduceByKey((a,n)=> a+n) | |
| wcount.print() | |
| / word count in the window | |
| val wcount_window = pairs.reduceByKeyAndWindow((a:Int,n:Int) => (a+n), Seconds(20), Seconds(10)) | |
| wcount_window.print() | |
| ssc.start | |
| ssc.stop(true) | |
| /* Output: | |
| ------------------------------------------- | |
| Time: 1504424600000 ms | |
| ------------------------------------------- | |
| (w,2) | |
| (q,2) | |
| ------------------------------------------- | |
| Time: 1504424600000 ms | |
| ------------------------------------------- | |
| (w,2) | |
| (q,2) | |
| ------------------------------------------- | |
| Time: 1504424610000 ms | |
| ------------------------------------------- | |
| (w,2) | |
| (q,2) | |
| ------------------------------------------- | |
| Time: 1504424610000 ms | |
| ------------------------------------------- | |
| (w,4) | |
| (q,4) | |
| */ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to do: modify to reflect actul wordcount using flatMap