Created
May 22, 2019 07:37
-
-
Save appcoreopc/ca69d79527cb955fc54ac32d277a1821 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 __future__ import print_function | |
import sys | |
from pyspark.sql import SparkSession | |
from pyspark.sql.functions import explode | |
from pyspark.sql.functions import split | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print(""" | |
Usage: structured_kafka_wordcount.py <bootstrap-servers> <subscribe-type> <topics> | |
""", file=sys.stderr) | |
sys.exit(-1) | |
bootstrapServers = sys.argv[1] | |
subscribeType = sys.argv[2] | |
topics = sys.argv[3] | |
spark = SparkSession\ | |
.builder\ | |
.appName("StructuredKafkaWordCount")\ | |
.getOrCreate() | |
# Create DataSet representing the stream of input lines from kafka | |
lines = spark\ | |
.readStream\ | |
.format("kafka")\ | |
.option("kafka.bootstrap.servers", bootstrapServers)\ | |
.option(subscribeType, topics)\ | |
.load()\ | |
.selectExpr("CAST(value AS STRING)") | |
# Split the lines into words | |
words = lines.select( | |
# explode turns each item in an array into a separate row | |
explode( | |
split(lines.value, ' ') | |
).alias('word') | |
) | |
print("Outputing what we get from our kafka queue") | |
# Generate running word count | |
wordCounts = words.groupBy('word').count() | |
# Start running the query that prints the running counts to the console | |
query = wordCounts\ | |
.writeStream\ | |
.outputMode('complete')\ | |
.format('console')\ | |
.start() | |
#query.awaitTermination() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment