File tree Expand file tree Collapse file tree 2 files changed +6
-12
lines changed
examples/src/main/python/streaming Expand file tree Collapse file tree 2 files changed +6
-12
lines changed Original file line number Diff line number Diff line change 1
1
import sys
2
2
from operator import add
3
3
4
- from pyspark .conf import SparkConf
5
4
from pyspark .streaming .context import StreamingContext
6
5
from pyspark .streaming .duration import *
7
6
8
7
if __name__ == "__main__" :
9
8
if len (sys .argv ) != 3 :
10
9
print >> sys .stderr , "Usage: wordcount <hostname> <port>"
11
10
exit (- 1 )
12
- conf = SparkConf ()
13
- conf .setAppName ("PythonStreamingNetworkWordCount" )
14
- ssc = StreamingContext (conf = conf , duration = Seconds (1 ))
11
+ ssc = StreamingContext (appName = "PythonStreamingNetworkWordCount" ,
12
+ duration = Seconds (1 ))
15
13
16
14
lines = ssc .socketTextStream (sys .argv [1 ], int (sys .argv [2 ]))
17
15
words = lines .flatMap (lambda line : line .split (" " ))
18
16
mapped_words = words .map (lambda word : (word , 1 ))
19
17
count = mapped_words .reduceByKey (add )
20
-
21
18
count .pyprint ()
19
+
22
20
ssc .start ()
23
21
ssc .awaitTermination ()
Original file line number Diff line number Diff line change 1
1
import sys
2
- from operator import add
3
2
4
- from pyspark .conf import SparkConf
5
3
from pyspark .streaming .context import StreamingContext
6
4
from pyspark .streaming .duration import *
7
5
8
6
if __name__ == "__main__" :
9
7
if len (sys .argv ) != 2 :
10
8
print >> sys .stderr , "Usage: wordcount <directory>"
11
9
exit (- 1 )
12
- conf = SparkConf ()
13
- conf .setAppName ("PythonStreamingWordCount" )
14
10
15
- ssc = StreamingContext (conf = conf , duration = Seconds (1 ))
11
+ ssc = StreamingContext (appName = "PythonStreamingWordCount" , duration = Seconds (1 ))
16
12
17
13
lines = ssc .textFileStream (sys .argv [1 ])
18
14
words = lines .flatMap (lambda line : line .split (" " ))
19
15
mapped_words = words .map (lambda x : (x , 1 ))
20
- count = mapped_words .reduceByKey (add )
21
-
16
+ count = mapped_words .reduceByKey (lambda a , b : a + b )
22
17
count .pyprint ()
18
+
23
19
ssc .start ()
24
20
ssc .awaitTermination ()
You can’t perform that action at this time.
0 commit comments