Skip to main content

Spark Streaming multithread in IPython Notebooks

Project description

Spark Streaming multithread in IPython Notebooks.

It’s now simple to execute Spark Structured Streaming in Jupyter Notebooks

Install

pip install nbthread_spark --process-dependency-links

Usage

Given a Socket Stream:

TCP_IP = "localhost"
TCP_PORT = 9005

from pyspark.sql.functions import from_json
from pyspark.sql import SparkSession
from pyspark.sql.types import StructField, StructType, IntegerType

schema = StructType([
    StructField("bip", IntegerType(), True),
    StructField("is_on", IntegerType(), True)
])

spark = SparkSession \
    .builder \
    .appName("IOTStreamApp") \
    .getOrCreate()

iot_stream = spark \
    .readStream \
    .format("socket") \
    .option("host", TCP_IP) \
    .option("port", TCP_PORT) \
    .load()

iot_expanded = iot_stream.withColumn('value_json',
                                    from_json('value', schema)
                                    ).drop('value').select('value_json.*')

query = iot_expanded \
    .writeStream \
    .outputMode("update") \
    .format("memory") \
    .queryName("iot_table") \
    .start()

You can run queries using this:

from nbthread_spark.stream import StreamRunner

runner = StreamRunner(query)

runner.controls()
## you will see buttons ;)

runner.start() # start without controls

runner.status() # show stream status

runner.stop() # stop streaming and thread

Special Thanks

Here the list of students that contribute with this module.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbthread_spark-0.0.4.tar.gz (2.4 kB view details)

Uploaded Source

File details

Details for the file nbthread_spark-0.0.4.tar.gz.

File metadata

File hashes

Hashes for nbthread_spark-0.0.4.tar.gz
Algorithm Hash digest
SHA256 6f3f44a5fcf265289f1ad397e6bea77b9f4afb5c96e544d31fe3282bf2e073f9
MD5 e79327b2efb72ea8d9ba422a72bf8928
BLAKE2b-256 a9c3161ac18a2de14e53d5daa4c4284c9d725f97c966bc43239a06d28f698882

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page