Last active
July 24, 2019 13:08
-
-
Save jrx/fb99c6c8d74b8e86c16e609803072726 to your computer and use it in GitHub Desktop.
This file contains 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
from pyspark import SparkContext | |
from pyspark.streaming import StreamingContext | |
from pyspark.streaming.kafka import KafkaUtils | |
sc = SparkContext(appName="StreamingExampleWithKafka") | |
ssc = StreamingContext(sc, 10) | |
opts = {"metadata.broker.list": "broker.kafka.l4lb.thisdcos.directory:9092"} | |
kvs = KafkaUtils.createDirectStream(ssc, ["mytopic"], opts) | |
lines = kvs.map(lambda x: x[1]) | |
counts = lines.flatMap(lambda line: line.split(" ")) \ | |
.map(lambda word: (word, 1)) \ | |
.reduceByKey(lambda a, b: a+b) | |
counts.pprint() | |
ssc.start() | |
ssc.awaitTermination() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment