038_StructuredStreamingProgGuide(Scala)

Overview

Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark SQL engine. You can express your streaming computation the same way you would express a batch computation on static data. The Spark SQL engine will take care of running it incrementally and continuously and updating the final result as streaming data continues to arrive. You can use the Dataset/DataFrame API in Scala, Java, Python or R to express streaming aggregations, event-time windows, stream-to-batch joins, etc. The computation is executed on the same optimized Spark SQL engine. Finally, the system ensures end-to-end exactly-once fault-tolerance guarantees through checkpointing and Write Ahead Logs. In short, Structured Streaming provides fast, scalable, fault-tolerant, end-to-end exactly-once stream processing without the user having to reason about streaming.

In this guide, we are going to walk you through the programming model and the APIs. First, let’s start with a simple example - a streaming word count.

Programming Model

The key idea in Structured Streaming is to treat a live data stream as a table that is being continuously appended. This leads to a new stream processing model that is very similar to a batch processing model. You will express your streaming computation as standard batch-like query as on a static table, and Spark runs it as an incremental query on the unbounded input table. Let’s understand this model in more detail.

Basic Concepts

Consider the input data stream as the “Input Table”. Every data item that is arriving on the stream is like a new row being appended to the Input Table.

Stream as a
Table

A query on the input will generate the “Result Table”. Every trigger interval (say, every 1 second), new rows get appended to the Input Table, which eventually updates the Result Table. Whenever the result table gets updated, we would want to write the changed result rows to an external sink.

Model

The “Output” is defined as what gets written out to the external storage. The output can be defined in a different mode:

  • Complete Mode - The entire updated Result Table will be written to the external storage. It is up to the storage connector to decide how to handle writing of the entire table.

  • Append Mode - Only the new rows appended in the Result Table since the last trigger will be written to the external storage. This is applicable only on the queries where existing rows in the Result Table are not expected to change.

  • Update Mode - Only the rows that were updated in the Result Table since the last trigger will be written to the external storage (available since Spark 2.1.1). Note that this is different from the Complete Mode in that this mode only outputs the rows that have changed since the last trigger. If the query doesn’t contain aggregations, it will be equivalent to Append mode.

Note that each mode is applicable on certain types of queries. This is discussed in detail later on output-modes. To illustrate the use of this model, let’s understand the model in context of the Quick Example above.

The first streamingLines DataFrame is the input table, and the final wordCounts DataFrame is the result table. Note that the query on streamingLines DataFrame to generate wordCounts is exactly the same as it would be a static DataFrame. However, when this query is started, Spark will continuously check for new data from the directory. If there is new data, Spark will run an “incremental” query that combines the previous running counts with the new data to compute updated counts, as shown below.

Model

This model is significantly different from many other stream processing engines. Many streaming systems require the user to maintain running aggregations themselves, thus having to reason about fault-tolerance, and data consistency (at-least-once, or at-most-once, or exactly-once). In this model, Spark is responsible for updating the Result Table when there is new data, thus relieving the users from reasoning about it. As an example, let’s see how this model handles event-time based processing and late arriving data.

Quick Example

Let’s say you want to maintain a running word count of text data received from a file writer that is writing files into a directory datasets/streamingFiles in the distributed file system. Let’s see how you can express this using Structured Streaming.

Let’s walk through the example step-by-step and understand how it works.

First we need to start a file writing job in the companion notebook 037a_AnimalNamesStructStreamingFiles and then return here.

display(dbutils.fs.ls("/datasets/streamingFiles"))
dbfs:/datasets/streamingFiles/41_44.log41_44.log35
dbfs:/datasets/streamingFiles/41_46.log41_46.log35
dbfs:/datasets/streamingFiles/41_48.log41_48.log35
dbfs:/datasets/streamingFiles/41_50.log41_50.log35
dbfs:/datasets/streamingFiles/41_52.log41_52.log35
dbfs:/datasets/streamingFiles/41_54.log41_54.log35
dbfs:/datasets/streamingFiles/41_56.log41_56.log35
dbfs:/datasets/streamingFiles/41_58.log41_58.log35
dbfs:/datasets/streamingFiles/42_00.log42_00.log35
dbfs:/datasets/streamingFiles/42_02.log42_02.log35
dbfs:/datasets/streamingFiles/42_04.log42_04.log35
dbfs:/datasets/streamingFiles/42_06.log42_06.log35
dbfs:/datasets/streamingFiles/42_08.log42_08.log35
dbfs:/datasets/streamingFiles/42_10.log42_10.log35
dbfs:/datasets/streamingFiles/42_12.log42_12.log35
dbfs:/datasets/streamingFiles/42_14.log42_14.log35
dbfs:/datasets/streamingFiles/42_16.log42_16.log35
dbfs:/datasets/streamingFiles/42_18.log42_18.log35
dbfs:/datasets/streamingFiles/42_20.log42_20.log35
dbfs:/datasets/streamingFiles/42_22.log42_22.log35
dbfs:/datasets/streamingFiles/42_24.log42_24.log35
dbfs:/datasets/streamingFiles/42_26.log42_26.log35
dbfs:/datasets/streamingFiles/42_28.log42_28.log35
dbfs:/datasets/streamingFiles/42_30.log42_30.log35
dbfs:/datasets/streamingFiles/42_32.log42_32.log35
dbfs:/datasets/streamingFiles/42_34.log42_34.log35
dbfs:/datasets/streamingFiles/42_36.log42_36.log35
dbfs:/datasets/streamingFiles/42_38.log42_38.log35
dbfs:/datasets/streamingFiles/42_40.log42_40.log35
dbfs:/datasets/streamingFiles/42_42.log42_42.log35
dbfs:/datasets/streamingFiles/42_44.log42_44.log35
dbfs:/datasets/streamingFiles/42_46.log42_46.log35
dbfs:/datasets/streamingFiles/42_48.log42_48.log35
dbfs:/datasets/streamingFiles/42_50.log42_50.log35
dbfs:/datasets/streamingFiles/42_52.log42_52.log35
dbfs:/datasets/streamingFiles/42_54.log42_54.log35
dbfs:/datasets/streamingFiles/42_56.log42_56.log35
dbfs:/datasets/streamingFiles/42_58.log42_58.log35
dbfs:/datasets/streamingFiles/43_00.log43_00.log35
dbfs:/datasets/streamingFiles/43_02.log43_02.log35
dbfs:/datasets/streamingFiles/43_04.log43_04.log35
dbfs:/datasets/streamingFiles/43_06.log43_06.log35
dbfs:/datasets/streamingFiles/43_08.log43_08.log35
dbfs:/datasets/streamingFiles/43_10.log43_10.log35
dbfs:/datasets/streamingFiles/43_12.log43_12.log35
dbfs:/datasets/streamingFiles/43_14.log43_14.log35
dbfs:/datasets/streamingFiles/43_16.log43_16.log35
dbfs:/datasets/streamingFiles/43_18.log43_18.log35
dbfs:/datasets/streamingFiles/43_20.log43_20.log35
dbfs:/datasets/streamingFiles/43_22.log43_22.log35
dbfs:/datasets/streamingFiles/43_24.log43_24.log35
dbfs:/datasets/streamingFiles/43_26.log43_26.log35
dbfs:/datasets/streamingFiles/43_28.log43_28.log35
dbfs:/datasets/streamingFiles/43_30.log43_30.log35
dbfs:/datasets/streamingFiles/43_32.log43_32.log35
dbfs:/datasets/streamingFiles/43_34.log43_34.log35
dbfs:/datasets/streamingFiles/43_36.log43_36.log35
dbfs:/datasets/streamingFiles/43_38.log43_38.log35
dbfs:/datasets/streamingFiles/43_40.log43_40.log35
dbfs:/datasets/streamingFiles/43_42.log43_42.log35
dbfs:/datasets/streamingFiles/43_44.log43_44.log35
dbfs:/datasets/streamingFiles/43_46.log43_46.log35
dbfs:/datasets/streamingFiles/43_48.log43_48.log35
dbfs:/datasets/streamingFiles/43_50.log43_50.log35
dbfs:/datasets/streamingFiles/43_52.log43_52.log35
dbfs:/datasets/streamingFiles/43_54.log43_54.log35
dbfs:/datasets/streamingFiles/43_56.log43_56.log35
dbfs:/datasets/streamingFiles/43_58.log43_58.log35
dbfs:/datasets/streamingFiles/44_00.log44_00.log35
dbfs:/datasets/streamingFiles/44_02.log44_02.log35
dbfs:/datasets/streamingFiles/44_04.log44_04.log35
dbfs:/datasets/streamingFiles/44_06.log44_06.log35
dbfs:/datasets/streamingFiles/44_08.log44_08.log35
dbfs:/datasets/streamingFiles/44_10.log44_10.log35
dbfs:/datasets/streamingFiles/44_12.log44_12.log35
dbfs:/datasets/streamingFiles/44_14.log44_14.log35
dbfs:/datasets/streamingFiles/44_16.log44_16.log35
dbfs:/datasets/streamingFiles/44_18.log44_18.log35
dbfs:/datasets/streamingFiles/44_20.log44_20.log35
dbfs:/datasets/streamingFiles/44_22.log44_22.log35
dbfs:/datasets/streamingFiles/44_24.log44_24.log35
dbfs:/datasets/streamingFiles/44_26.log44_26.log35
dbfs:/datasets/streamingFiles/44_28.log44_28.log35
dbfs:/datasets/streamingFiles/44_30.log44_30.log35
dbfs:/datasets/streamingFiles/44_32.log44_32.log35
dbfs:/datasets/streamingFiles/44_34.log44_34.log35
dbfs:/datasets/streamingFiles/44_36.log44_36.log35
dbfs:/datasets/streamingFiles/44_38.log44_38.log35
dbfs:/datasets/streamingFiles/44_40.log44_40.log35
dbfs:/datasets/streamingFiles/44_42.log44_42.log35
dbfs:/datasets/streamingFiles/44_44.log44_44.log35
dbfs:/datasets/streamingFiles/44_46.log44_46.log35
dbfs:/datasets/streamingFiles/44_48.log44_48.log35
dbfs:/datasets/streamingFiles/44_50.log44_50.log35
dbfs:/datasets/streamingFiles/44_52.log44_52.log35
dbfs:/datasets/streamingFiles/44_54.log44_54.log35
dbfs:/datasets/streamingFiles/44_56.log44_56.log35
dbfs:/datasets/streamingFiles/44_58.log44_58.log35
dbfs:/datasets/streamingFiles/45_00.log45_00.log35
dbfs:/datasets/streamingFiles/45_02.log45_02.log35
dbutils.fs.head("/datasets/streamingFiles/44_02.log")
res4: String = "2019-05-31 14:44:02+00:00; cat owl "

Next, let’s create a streaming DataFrame that represents text data received from the directory, and transform the DataFrame to calculate word counts.

import org.apache.spark.sql.types._

// Create DataFrame representing the stream of input lines from files in distributed file store
//val textFileSchema = new StructType().add("line", "string") // for a custom schema

val streamingLines = spark
  .readStream
  //.schema(textFileSchema) // using default -> makes a column of String named value
  .option("MaxFilesPerTrigger", 1) //  maximum number of new files to be considered in every trigger (default: no max) 
  .format("text")
  .load("/datasets/streamingFiles")
import org.apache.spark.sql.types._ streamingLines: org.apache.spark.sql.DataFrame = [value: string]

This streamingLines DataFrame represents an unbounded table containing the streaming text data. This table contains one column of strings named “value”, and each line in the streaming text data becomes a row in the table. Note, that this is not currently receiving any data as we are just setting up the transformation, and have not yet started it.

display(streamingLines)  // display will show you the contents of the DF
display_query_1(id: 87e4fd1f-54a5-48d7-b43b-26c9cb84b93c)
Last updated: 2132 days ago
2019-05-31 14:44:40+00:00; pig dog
2019-05-31 14:46:03+00:00; owl bat
2019-05-31 14:42:46+00:00; cat rat
2019-05-31 14:53:32+00:00; owl cat
2019-05-31 14:42:20+00:00; rat owl
2019-05-31 14:48:03+00:00; dog owl
2019-05-31 14:47:39+00:00; dog bat
2019-05-31 14:43:38+00:00; rat dog
2019-05-31 14:44:22+00:00; owl cat
2019-05-31 14:49:37+00:00; dog cat
2019-05-31 14:45:43+00:00; owl bat
2019-05-31 14:50:19+00:00; rat dog
2019-05-31 14:42:02+00:00; cat owl
2019-05-31 14:43:24+00:00; cat pig
2019-05-31 14:45:16+00:00; dog owl
2019-05-31 14:42:42+00:00; cat rat
2019-05-31 14:46:01+00:00; pig dog
2019-05-31 14:41:58+00:00; cat owl
2019-05-31 14:44:12+00:00; pig rat
2019-05-31 14:42:54+00:00; owl pig
2019-05-31 14:49:29+00:00; cat owl
2019-05-31 14:47:55+00:00; rat cat
2019-05-31 14:43:32+00:00; dog owl
2019-05-31 14:49:49+00:00; dog owl
2019-05-31 14:44:10+00:00; cat pig
2019-05-31 14:43:04+00:00; bat rat
2019-05-31 14:47:49+00:00; cat rat
2019-05-31 14:44:42+00:00; rat bat
2019-05-31 14:41:56+00:00; dog cat
2019-05-31 14:44:08+00:00; dog pig
2019-05-31 14:42:10+00:00; owl cat
2019-05-31 14:44:34+00:00; bat owl
2019-05-31 14:46:33+00:00; pig owl
2019-05-31 14:43:30+00:00; pig bat
2019-05-31 14:48:21+00:00; owl pig
2019-05-31 14:42:08+00:00; bat pig
2019-05-31 14:44:04+00:00; owl rat
2019-05-31 14:50:59+00:00; dog owl
2019-05-31 14:46:19+00:00; rat dog
2019-05-31 14:43:20+00:00; dog cat
2019-05-31 14:44:00+00:00; rat pig
2019-05-31 14:46:17+00:00; rat owl
2019-05-31 14:42:36+00:00; cat dog
2019-05-31 14:42:48+00:00; cat bat
2019-05-31 14:47:29+00:00; cat bat
2019-05-31 14:46:49+00:00; bat rat
2019-05-31 14:43:50+00:00; dog pig
2019-05-31 14:50:09+00:00; cat dog
2019-05-31 14:52:04+00:00; cat pig
2019-05-31 14:44:38+00:00; pig owl
2019-05-31 14:50:21+00:00; cat bat
2019-05-31 14:42:00+00:00; owl cat
2019-05-31 14:50:01+00:00; bat cat
2019-05-31 14:49:57+00:00; bat dog
2019-05-31 14:43:44+00:00; cat dog
2019-05-31 14:48:55+00:00; dog bat
2019-05-31 14:48:09+00:00; pig dog
2019-05-31 14:47:41+00:00; cat bat
2019-05-31 14:48:01+00:00; owl dog
2019-05-31 14:45:04+00:00; dog bat
2019-05-31 14:49:47+00:00; dog cat
2019-05-31 14:44:18+00:00; bat owl
2019-05-31 14:45:14+00:00; cat pig
2019-05-31 14:43:46+00:00; pig owl
2019-05-31 14:48:17+00:00; rat pig
2019-05-31 14:55:46+00:00; owl pig
2019-05-31 14:46:27+00:00; cat bat
2019-05-31 14:52:52+00:00; dog owl
2019-05-31 14:56:12+00:00; rat dog
2019-05-31 14:49:11+00:00; pig cat
2019-05-31 14:45:59+00:00; dog rat
2019-05-31 14:50:29+00:00; pig rat
2019-05-31 14:43:22+00:00; pig rat
2019-05-31 14:43:48+00:00; cat rat
2019-05-31 14:46:11+00:00; dog pig
2019-05-31 14:50:37+00:00; owl bat
2019-05-31 14:48:47+00:00; owl dog
2019-05-31 14:42:52+00:00; dog pig
2019-05-31 14:42:12+00:00; pig owl
2019-05-31 14:42:24+00:00; dog pig
2019-05-31 14:44:20+00:00; bat cat
2019-05-31 14:42:28+00:00; cat dog
2019-05-31 14:47:15+00:00; cat dog
2019-05-31 14:44:52+00:00; cat owl
2019-05-31 14:45:24+00:00; pig dog
2019-05-31 14:45:08+00:00; owl bat
2019-05-31 14:43:12+00:00; owl cat
2019-05-31 14:51:01+00:00; cat rat
2019-05-31 14:42:22+00:00; cat bat
2019-05-31 14:41:48+00:00; rat cat
2019-05-31 14:52:08+00:00; dog cat
2019-05-31 14:45:37+00:00; rat bat
2019-05-31 14:45:00+00:00; pig owl
2019-05-31 14:48:37+00:00; cat owl
2019-05-31 14:56:02+00:00; owl bat
2019-05-31 14:42:34+00:00; bat dog
2019-05-31 14:45:41+00:00; bat rat
2019-05-31 14:47:35+00:00; pig dog
2019-05-31 14:41:52+00:00; rat bat
2019-05-31 14:43:42+00:00; pig rat

Next, we will convert the DataFrame to a Dataset of String using .as[String], so that we can apply the flatMap operation to split each line into multiple words. The resultant words Dataset contains all the words.

val words = streamingLines.as[String]
                          .map(line => line.split(";").drop(1)(0)) // this is to simply cut out the timestamp from this stream
                          .flatMap(_.split(" ")) // flat map by splitting the animal words separated by whitespace
                          .filter( _ != "") // remove empty words that may be artifacts of opening whitespace
words: org.apache.spark.sql.Dataset[String] = [value: string]

Finally, we define the wordCounts DataFrame by grouping by the unique values in the Dataset and counting them. Note that this is a streaming DataFrame which represents the running word counts of the stream.

// Generate running word count
val wordCounts = words
                  .groupBy("value").count() // this does the word count
                  .orderBy($"count".desc) // we are simply sorting by the most frequent words
wordCounts: org.apache.spark.sql.Dataset[org.apache.spark.sql.Row] = [value: string, count: bigint]

We have now set up the query on the streaming data. All that is left is to actually start receiving data and computing the counts. To do this, we set it up to print the complete set of counts (specified by outputMode("complete")) to the console every time they are updated. And then start the streaming computation using start().

// Start running the query that prints the running counts to the console
val query = wordCounts.writeStream
      .outputMode("complete")
      .format("console")
      .start()

query.awaitTermination() // hit cancel to terminate - killall the bash script in 037a_AnimalNamesStructStreamingFiles
Stream initializing...
------------------------------------------- Batch: 0 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | owl| 1| | bat| 1| +-----+-----+ ------------------------------------------- Batch: 1 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 1| | pig| 1| | owl| 1| | bat| 1| +-----+-----+ ------------------------------------------- Batch: 2 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | bat| 2| | dog| 1| | cat| 1| | pig| 1| | owl| 1| +-----+-----+ ------------------------------------------- Batch: 3 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | bat| 2| | dog| 2| | owl| 2| | cat| 1| | pig| 1| +-----+-----+ ------------------------------------------- Batch: 4 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | dog| 3| | cat| 2| | owl| 2| | bat| 2| | pig| 1| +-----+-----+ ------------------------------------------- Batch: 5 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | dog| 3| | bat| 3| | cat| 2| | owl| 2| | pig| 1| | rat| 1| +-----+-----+ ------------------------------------------- Batch: 6 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | bat| 4| | dog| 3| | cat| 3| | owl| 2| | pig| 1| | rat| 1| +-----+-----+ ------------------------------------------- Batch: 7 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 4| | bat| 4| | dog| 3| | owl| 3| | pig| 1| | rat| 1| +-----+-----+ ------------------------------------------- Batch: 8 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 5| | owl| 4| | bat| 4| | dog| 3| | pig| 1| | rat| 1| +-----+-----+ ------------------------------------------- Batch: 9 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 6| | owl| 5| | bat| 4| | dog| 3| | rat| 1| | pig| 1| +-----+-----+ ------------------------------------------- Batch: 10 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 6| | owl| 5| | bat| 4| | dog| 3| | rat| 2| | pig| 2| +-----+-----+ ------------------------------------------- Batch: 11 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 7| | owl| 5| | bat| 4| | dog| 3| | pig| 3| | rat| 2| +-----+-----+ ------------------------------------------- Batch: 12 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 8| | owl| 5| | bat| 5| | dog| 3| | pig| 3| | rat| 2| +-----+-----+ ------------------------------------------- Batch: 13 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 8| | owl| 6| | bat| 5| | dog| 3| | rat| 3| | pig| 3| +-----+-----+ ------------------------------------------- Batch: 14 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 8| | bat| 6| | owl| 6| | pig| 4| | dog| 3| | rat| 3| +-----+-----+ ------------------------------------------- Batch: 15 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 8| | bat| 7| | owl| 6| | dog| 4| | pig| 4| | rat| 3| +-----+-----+ ------------------------------------------- Batch: 16 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 8| | bat| 7| | owl| 7| | pig| 5| | dog| 4| | rat| 3| +-----+-----+ ------------------------------------------- Batch: 17 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 9| | bat| 8| | owl| 7| | pig| 5| | dog| 4| | rat| 3| +-----+-----+ ------------------------------------------- Batch: 18 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 10| | bat| 9| | owl| 7| | pig| 5| | dog| 4| | rat| 3| +-----+-----+ ------------------------------------------- Batch: 19 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 11| | bat| 9| | owl| 7| | pig| 5| | dog| 4| | rat| 4| +-----+-----+ ------------------------------------------- Batch: 20 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 12| | bat| 9| | owl| 7| | dog| 5| | pig| 5| | rat| 4| +-----+-----+ ------------------------------------------- Batch: 21 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 12| | bat| 9| | owl| 7| | dog| 6| | rat| 5| | pig| 5| +-----+-----+ ------------------------------------------- Batch: 22 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 12| | bat| 9| | owl| 7| | rat| 6| | dog| 6| | pig| 6| +-----+-----+ ------------------------------------------- Batch: 23 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 12| | bat| 10| | owl| 7| | rat| 7| | dog| 6| | pig| 6| +-----+-----+ ------------------------------------------- Batch: 24 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 13| | bat| 10| | rat| 8| | owl| 7| | dog| 6| | pig| 6| +-----+-----+ ------------------------------------------- Batch: 25 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 14| | bat| 11| | rat| 8| | owl| 7| | dog| 6| | pig| 6| +-----+-----+ ------------------------------------------- Batch: 26 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 15| | bat| 11| | rat| 8| | pig| 7| | owl| 7| | dog| 6| +-----+-----+ ------------------------------------------- Batch: 27 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 15| | bat| 11| | rat| 9| | pig| 8| | owl| 7| | dog| 6| +-----+-----+ ------------------------------------------- Batch: 28 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 16| | bat| 11| | rat| 9| | pig| 9| | owl| 7| | dog| 6| +-----+-----+ ------------------------------------------- Batch: 29 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 16| | bat| 12| | pig| 9| | rat| 9| | owl| 8| | dog| 6| +-----+-----+ ------------------------------------------- Batch: 30 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 17| | bat| 12| | rat| 9| | pig| 9| | owl| 9| | dog| 6| +-----+-----+ ------------------------------------------- Batch: 31 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 18| | bat| 12| | pig| 9| | owl| 9| | rat| 9| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 32 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 19| | bat| 12| | rat| 10| | pig| 9| | owl| 9| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 33 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 20| | bat| 12| | rat| 11| | pig| 9| | owl| 9| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 34 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 20| | rat| 12| | bat| 12| | pig| 10| | owl| 9| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 35 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 20| | bat| 13| | rat| 12| | pig| 11| | owl| 9| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 36 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 20| | bat| 13| | pig| 12| | rat| 12| | owl| 10| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 37 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 20| | rat| 13| | bat| 13| | pig| 13| | owl| 10| | dog| 7| +-----+-----+ ------------------------------------------- Batch: 38 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 20| | pig| 14| | rat| 13| | bat| 13| | owl| 10| | dog| 8| +-----+-----+ ------------------------------------------- Batch: 39 ------------------------------------------- +-----+-----+ |value|count| +-----+-----+ | cat| 21| | pig| 14| | rat| 13| | bat| 13| | owl| 11| | dog| 8| +-----+-----+
Cancelled

After this code is executed, the streaming computation will have started in the background. The query object is a handle to that active streaming query, and we have decided to wait for the termination of the query using awaitTermination() to prevent the process from exiting while the query is active.

Handling Event-time and Late Data

Event-time is the time embedded in the data itself. For many applications, you may want to operate on this event-time. For example, if you want to get the number of events generated by IoT devices every minute, then you probably want to use the time when the data was generated (that is, event-time in the data), rather than the time Spark receives them. This event-time is very naturally expressed in this model – each event from the devices is a row in the table, and event-time is a column value in the row. This allows window-based aggregations (e.g. number of events every minute) to be just a special type of grouping and aggregation on the event-time column – each time window is a group and each row can belong to multiple windows/groups. Therefore, such event-time-window-based aggregation queries can be defined consistently on both a static dataset (e.g. from collected device events logs) as well as on a data stream, making the life of the user much easier.

Furthermore, this model naturally handles data that has arrived later than expected based on its event-time. Since Spark is updating the Result Table, it has full control over updating old aggregates when there is late data, as well as cleaning up old aggregates to limit the size of intermediate state data. Since Spark 2.1, we have support for watermarking which allows the user to specify the threshold of late data, and allows the engine to accordingly clean up old state. These are explained later in more detail in the Window Operations section below.

Fault Tolerance Semantics

Delivering end-to-end exactly-once semantics was one of key goals behind the design of Structured Streaming. To achieve that, we have designed the Structured Streaming sources, the sinks and the execution engine to reliably track the exact progress of the processing so that it can handle any kind of failure by restarting and/or reprocessing. Every streaming source is assumed to have offsets (similar to Kafka offsets, or Kinesis sequence numbers) to track the read position in the stream. The engine uses checkpointing and write ahead logs to record the offset range of the data being processed in each trigger. The streaming sinks are designed to be idempotent for handling reprocessing. Together, using replayable sources and idempotent sinks, Structured Streaming can ensure end-to-end exactly-once semantics under any failure.

API using Datasets and DataFrames

Since Spark 2.0, DataFrames and Datasets can represent static, bounded data, as well as streaming, unbounded data. Similar to static Datasets/DataFrames, you can use the common entry point SparkSession (Scala/Java/Python/R docs) to create streaming DataFrames/Datasets from streaming sources, and apply the same operations on them as static DataFrames/Datasets. If you are not familiar with Datasets/DataFrames, you are strongly advised to familiarize yourself with them using the DataFrame/Dataset Programming Guide.

Creating streaming DataFrames and streaming Datasets

Streaming DataFrames can be created through the DataStreamReader interface (Scala/Java/Python docs) returned by SparkSession.readStream(). In R, with the read.stream() method. Similar to the read interface for creating static DataFrame, you can specify the details of the source – data format, schema, options, etc.

Input Sources

In Spark 2.0, there are a few built-in sources.

  • File source - Reads files written in a directory as a stream of data. Supported file formats are text, csv, json, parquet. See the docs of the DataStreamReader interface for a more up-to-date list, and supported options for each file format. Note that the files must be atomically placed in the given directory, which in most file systems, can be achieved by file move operations.

  • Kafka source - Poll data from Kafka. It’s compatible with Kafka broker versions 0.10.0 or higher. See the Kafka Integration Guide for more details.

  • Socket source (for testing) - Reads UTF8 text data from a socket connection. The listening server socket is at the driver. Note that this should be used only for testing as this does not provide end-to-end fault-tolerance guarantees.

Some sources are not fault-tolerant because they do not guarantee that data can be replayed using checkpointed offsets after a failure. See the earlier section on fault-tolerance semantics. Here are the details of all the sources in Spark.

Source Options Fault-tolerant Notes
File source path: path to the input directory, and common to all file formats.
maxFilesPerTrigger: maximum number of new files to be considered in every trigger (default: no max)
latestFirst: whether to processs the latest new files first, useful when there is a large backlog of files (default: false)
fileNameOnly: whether to check new files based on only the filename instead of on the full path (default: false). With this set to true, the following files would be considered as the same file, because their filenames, "dataset.txt", are the same:
· "file:///dataset.txt"
· "s3://a/dataset.txt"
· "s3n://a/b/dataset.txt"
· "s3a://a/b/c/dataset.txt"


For file-format-specific options, see the related methods in DataStreamReader (Scala/Java/Python/R). E.g. for "parquet" format options see DataStreamReader.parquet()
Yes Supports glob paths, but does not support multiple comma-separated paths/globs.
Socket Source host: host to connect to, must be specified
port: port to connect to, must be specified
No
Kafka Source See the Kafka Integration Guide. Yes

See https://spark.apache.org/docs/2.2.0/structured-streaming-programming-guide.html#input-sources.

Schema inference and partition of streaming DataFrames/Datasets

By default, Structured Streaming from file based sources requires you to specify the schema, rather than rely on Spark to infer it automatically (this is what we did with userSchema above). This restriction ensures a consistent schema will be used for the streaming query, even in the case of failures. For ad-hoc use cases, you can reenable schema inference by setting spark.sql.streaming.schemaInference to true.

Partition discovery does occur when subdirectories that are named /key=value/ are present and listing will automatically recurse into these directories. If these columns appear in the user provided schema, they will be filled in by Spark based on the path of the file being read. The directories that make up the partitioning scheme must be present when the query starts and must remain static. For example, it is okay to add /data/year=2016/ when /data/year=2015/ was present, but it is invalid to change the partitioning column (i.e. by creating the directory /data/date=2016-04-17/).

Operations on streaming DataFrames/Datasets

You can apply all kinds of operations on streaming DataFrames/Datasets – ranging from untyped, SQL-like operations (e.g. select, where, groupBy), to typed RDD-like operations (e.g. map, filter, flatMap). See the SQL programming guide for more details. Let’s take a look at a few example operations that you can use.

Basic Operations - Selection, Projection, Aggregation

Most of the common operations on DataFrame/Dataset are supported for streaming. The few operations that are not supported are discussed later in unsupported-operations section.

    case class DeviceData(device: String, deviceType: String, signal: Double, time: DateTime)

    val df: DataFrame = ... // streaming DataFrame with IOT device data with schema { device: string, deviceType: string, signal: double, time: string }
    val ds: Dataset[DeviceData] = df.as[DeviceData]    // streaming Dataset with IOT device data

    // Select the devices which have signal more than 10
    df.select("device").where("signal > 10")      // using untyped APIs
    ds.filter(_.signal > 10).map(_.device)         // using typed APIs

    // Running count of the number of updates for each device type
    df.groupBy("deviceType").count()                          // using untyped API

    // Running average signal for each device type
    import org.apache.spark.sql.expressions.scalalang.typed
    ds.groupByKey(_.deviceType).agg(typed.avg(_.signal))    // using typed API

A Quick Mixture Example

We will work below with a file stream that simulates random animal names or a simple mixture of two Normal Random Variables.

The two file streams can be acieved by running the codes in the following two databricks notebooks in the same cluster:

  • 037a_AnimalNamesStructStreamingFiles
  • 037b_Mix2NormalsStructStreamingFiles

You should have the following set of csv files (it won't be exactly the same names depending on when you start the stream of files).

display(dbutils.fs.ls("/datasets/streamingFilesNormalMixture/"))
dbfs:/datasets/streamingFilesNormalMixture/20_12/20_12/0
dbfs:/datasets/streamingFilesNormalMixture/20_20/20_20/0
dbfs:/datasets/streamingFilesNormalMixture/20_27/20_27/0
dbfs:/datasets/streamingFilesNormalMixture/20_34/20_34/0
dbfs:/datasets/streamingFilesNormalMixture/20_42/20_42/0

Static and Streaming DataFrames

Let's check out the files and their contents both via static as well as streaming DataFrames.

This will also cement the fact that structured streaming allows interoperability between static and streaming data and can be useful for debugging.

val peekIn = spark.read.format("csv").load("/datasets/streamingFilesNormalMixture/*/*.csv")
peekIn.count() // total count of all the samples in all the files
peekIn: org.apache.spark.sql.DataFrame = [_c0: string, _c1: string] res12: Long = 500
peekIn.show(5, false) // let's take a quick peek at what's in the CSV files
+-----------------------+--------------------+ |_c0 |_c1 | +-----------------------+--------------------+ |2019-05-31 15:20:26.901|0.21791376679544772 | |2019-05-31 15:20:26.906|0.011291967445604012| |2019-05-31 15:20:26.911|-0.30293144696154806| |2019-05-31 15:20:26.916|0.4303254534802833 | |2019-05-31 15:20:26.922|1.5521304466388752 | +-----------------------+--------------------+ only showing top 5 rows
// Read all the csv files written atomically from a directory
import org.apache.spark.sql.types._

//make a user-specified schema - this is needed for structured streaming from files
val userSchema = new StructType()
                      .add("time", "timestamp")
                      .add("score", "Double")

// a static DF is convenient 
val csvStaticDF = spark
  .read
  .option("sep", ",") // delimiter is ','
  .schema(userSchema) // Specify schema of the csv files as pre-defined by user
  .csv("/datasets/streamingFilesNormalMixture/*/*.csv")    // Equivalent to format("csv").load("/path/to/directory")

// streaming DF
val csvStreamingDF = spark
  .readStream
  .option("sep", ",") // delimiter is ','
  .schema(userSchema) // Specify schema of the csv files as pre-defined by user
  .option("MaxFilesPerTrigger", 1) //  maximum number of new files to be considered in every trigger (default: no max) 
  .csv("/datasets/streamingFilesNormalMixture/*/*.csv")    // Equivalent to format("csv").load("/path/to/directory")
import org.apache.spark.sql.types._ userSchema: org.apache.spark.sql.types.StructType = StructType(StructField(time,TimestampType,true), StructField(score,DoubleType,true)) csvStaticDF: org.apache.spark.sql.DataFrame = [time: timestamp, score: double] csvStreamingDF: org.apache.spark.sql.DataFrame = [time: timestamp, score: double]
csvStreamingDF.isStreaming    // Returns True for DataFrames that have streaming sources
res16: Boolean = true
csvStreamingDF.printSchema
root |-- time: timestamp (nullable = true) |-- score: double (nullable = true)
display(csvStreamingDF) // if you want to see the stream coming at you as csvDF
display_query_2(id: 8c606f6b-9a66-4dee-8be1-f47494269788)
Last updated: 2132 days ago
2019-05-31T15:20:11.885+00000.2576188264990721
2019-05-31T15:20:11.890+0000-0.13149698512045327
2019-05-31T15:20:11.896+00001.4139063973267458
2019-05-31T15:20:11.901+0000-0.023833875968513496
2019-05-31T15:20:11.906+00000.7274784426774964
2019-05-31T15:20:11.912+0000-1.0658630481235276
2019-05-31T15:20:11.917+00000.746959841932221
2019-05-31T15:20:11.922+00000.30477096247050206
2019-05-31T15:20:11.927+0000-0.06407620682061621
2019-05-31T15:20:11.932+00001.8464307210258604
2019-05-31T15:20:11.937+00002.0786529531264355
2019-05-31T15:20:11.943+00000.685838993990332
2019-05-31T15:20:11.948+00002.3056211153362485
2019-05-31T15:20:11.953+0000-0.7435548094085835
2019-05-31T15:20:11.958+0000-0.36946067155650786
2019-05-31T15:20:11.963+00001.1178132434092503
2019-05-31T15:20:11.968+00001.0672400098827672
2019-05-31T15:20:11.974+00002.403799182291664
2019-05-31T15:20:11.979+00002.7905949803662926
2019-05-31T15:20:11.984+00002.3901047303648846
2019-05-31T15:20:11.990+00002.2391322699010967
2019-05-31T15:20:11.995+00000.7102559487906945
2019-05-31T15:20:12.000+0000-0.1875570296359037
2019-05-31T15:20:12.005+00002.0036998039560725
2019-05-31T15:20:12.010+00002.028162246705019
2019-05-31T15:20:12.016+0000-1.1084782237141253
2019-05-31T15:20:12.021+00002.7320985336302965
2019-05-31T15:20:12.026+00001.7953021498619885
2019-05-31T15:20:12.031+00001.3332433299615185
2019-05-31T15:20:12.036+00001.2842120504662247
2019-05-31T15:20:12.041+00002.0013530061962186
2019-05-31T15:20:12.047+00001.2596569236824775
2019-05-31T15:20:12.052+00002.46479668588018
2019-05-31T15:20:12.057+0000-0.7015927727061835
2019-05-31T15:20:12.062+0000-0.510611131534981
2019-05-31T15:20:12.067+00000.9403812557496112
2019-05-31T15:20:12.072+00002.2306482205877427
2019-05-31T15:20:12.078+0000-0.29781070820511246
2019-05-31T15:20:12.083+00004.107241990001628
2019-05-31T15:20:12.088+00000.7420568724108764
2019-05-31T15:20:12.093+00001.4652231673746594
2019-05-31T15:20:12.098+00000.8793849318247119
2019-05-31T15:20:12.103+00001.7671614106752898
2019-05-31T15:20:12.108+00001.1995772213743607
2019-05-31T15:20:12.113+00001.1351566745099897
2019-05-31T15:20:12.119+00000.16150528245701323
2019-05-31T15:20:12.124+00002.459849452657596
2019-05-31T15:20:12.129+00001.0796739450956971
2019-05-31T15:20:12.134+0000-1.2079899446434252
2019-05-31T15:20:12.139+00000.7019279468450133
2019-05-31T15:20:12.144+0000-0.025906759976580096
2019-05-31T15:20:12.149+00001.025799236502406
2019-05-31T15:20:12.155+00002.423754193708396
2019-05-31T15:20:12.160+00001.0100073192180106
2019-05-31T15:20:12.165+00001.2308412912433588
2019-05-31T15:20:12.170+00002.2142939785873326
2019-05-31T15:20:12.175+00009.639219241219372
2019-05-31T15:20:12.180+00000.8964067897832677
2019-05-31T15:20:12.185+00002.583753664296168
2019-05-31T15:20:12.191+00001.7326439212827238
2019-05-31T15:20:12.196+00000.7516388863094139
2019-05-31T15:20:12.201+00000.8725633940449549
2019-05-31T15:20:12.206+0000-0.9407676766254014
2019-05-31T15:20:12.211+00001.0542712925875175
2019-05-31T15:20:12.216+00000.794535189312687
2019-05-31T15:20:12.221+00000.5813794557982226
2019-05-31T15:20:12.227+00000.4891368786472011
2019-05-31T15:20:12.232+00002.3296394918008474
2019-05-31T15:20:12.237+00001.425296303524094
2019-05-31T15:20:12.242+00001.9276679925454094
2019-05-31T15:20:12.247+00000.6178050147872097
2019-05-31T15:20:12.252+00001.135269636375052
2019-05-31T15:20:12.257+00001.3074367248762568
2019-05-31T15:20:12.263+00000.6105659268751382
2019-05-31T15:20:12.268+00001.7812955395572572
2019-05-31T15:20:12.273+0000-1.3547368916771827
2019-05-31T15:20:12.278+00001.580412775615275
2019-05-31T15:20:12.283+00001.5731144914401023
2019-05-31T15:20:12.288+0000-0.05725067553082108
2019-05-31T15:20:12.293+00000.19580347035995105
2019-05-31T15:20:12.299+0000-0.021501122555202867
2019-05-31T15:20:12.304+00001.5783579658949254
2019-05-31T15:20:12.309+00001.371796305513024
2019-05-31T15:20:12.314+00000.648919899258448
2019-05-31T15:20:12.319+0000-0.7875773550339058
2019-05-31T15:20:12.324+00001.3233945353130716
2019-05-31T15:20:12.329+00002.5685224032022127
2019-05-31T15:20:12.334+00002.7331317575905807
2019-05-31T15:20:12.340+00000.2521381731074053
2019-05-31T15:20:12.345+00002.2408918489807905
2019-05-31T15:20:12.350+00001.4924862197354933
2019-05-31T15:20:12.355+00001.194657083531184
2019-05-31T15:20:12.360+00000.7067352811215412
2019-05-31T15:20:12.365+00000.027701718519244745
2019-05-31T15:20:12.370+00000.279797547315617
2019-05-31T15:20:12.376+0000-0.21953266770586133
2019-05-31T15:20:12.381+00001.1402931320647434
2019-05-31T15:20:12.386+00000.904724947360263
2019-05-31T15:20:12.391+00000.6677145203694429
2019-05-31T15:20:12.396+00002.019977647420342
import org.apache.spark.sql.functions._

// Start running the query that prints the running counts to the console
val query = csvStreamingDF
                 // bround simply rounds the double to the desired decimal place - 0 in our case here. 
                   // see https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/functions.html#bround-org.apache.spark.sql.Column-
                   // we are using bround to simply coarsen out data into bins for counts
                 .select(bround($"score", 0).as("binnedScore")) 
                 .groupBy($"binnedScore")
                 .agg(count($"binnedScore") as "binnedScoreCounts")
                 .orderBy($"binnedScore")
                 .writeStream
                 .outputMode("complete")
                 .format("console")
                 .start()
                 
query.awaitTermination() // hit cancel to terminate
Stream initializing...
------------------------------------------- Batch: 0 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -1.0| 9| | 0.0| 18| | 1.0| 41| | 2.0| 25| | 3.0| 5| | 4.0| 1| | 10.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 1 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 1| | -1.0| 13| | 0.0| 44| | 1.0| 83| | 2.0| 46| | 3.0| 10| | 4.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 2 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 2| | -1.0| 20| | 0.0| 74| | 1.0| 118| | 2.0| 70| | 3.0| 12| | 4.0| 1| | 9.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 3 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 4| | -1.0| 27| | 0.0| 104| | 1.0| 144| | 2.0| 96| | 3.0| 21| | 4.0| 1| | 9.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 4 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 4| | -1.0| 32| | 0.0| 125| | 1.0| 179| | 2.0| 125| | 3.0| 30| | 4.0| 2| | 9.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+
Cancelled

Once the above streaming job has processed all the files in the directory, it will continue to "listen" in for new files in the directory. You could for example return to the other notebook 037b_Mix2NormalsStructStreamingFiles and rerun the cell that writes another lot of newer files into the directory and return to this notebook to watch the above streaming job continue with additional batches.

Static and Streaming DataSets

These examples generate streaming DataFrames that are untyped, meaning that the schema of the DataFrame is not checked at compile time, only checked at runtime when the query is submitted. Some operations like map, flatMap, etc. need the type to be known at compile time. To do those, you can convert these untyped streaming DataFrames to typed streaming Datasets using the same methods as static DataFrame. See the SQL Programming Guide for more details. Additionally, more details on the supported streaming sources are discussed later in the document.

Let us make a dataset version of the streaming dataframe.

But first let us try it make the datset from the static dataframe and then apply it to the streming dataframe.

csvStaticDF.printSchema // schema of the static DF
root |-- time: timestamp (nullable = true) |-- score: double (nullable = true)
import org.apache.spark.sql.types._
import java.sql.Timestamp

// create a case class to make the datset
case class timedScores(time: Timestamp, score: Double)

val csvStaticDS = csvStaticDF.as[timedScores] // create a dataset from the dataframe
import org.apache.spark.sql.types._ import java.sql.Timestamp defined class timedScores csvStaticDS: org.apache.spark.sql.Dataset[timedScores] = [time: timestamp, score: double]
csvStaticDS.show(5,false) // looks like we got the dataset we want with strong typing
+-----------------------+--------------------+ |time |score | +-----------------------+--------------------+ |2019-05-31 15:20:26.901|0.21791376679544772 | |2019-05-31 15:20:26.906|0.011291967445604012| |2019-05-31 15:20:26.911|-0.30293144696154806| |2019-05-31 15:20:26.916|0.4303254534802833 | |2019-05-31 15:20:26.922|1.5521304466388752 | +-----------------------+--------------------+ only showing top 5 rows

Now let us use the same code for making a streaming dataset.

import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
import java.sql.Timestamp

// create a case class to make the datset
case class timedScores(time: Timestamp, score: Double)

val csvStreamingDS = csvStreamingDF.as[timedScores] // create a dataset from the dataframe

// Start running the query that prints the running counts to the console
val query = csvStreamingDS
                  // bround simply rounds the double to the desired decimal place - 0 in our case here. 
                   // see https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/functions.html#bround-org.apache.spark.sql.Column-
                   // we are using bround to simply coarsen out data into bins for counts
                 .select(bround($"score", 0).as("binnedScore")) 
                 .groupBy($"binnedScore")
                 .agg(count($"binnedScore") as "binnedScoreCounts")
                 .orderBy($"binnedScore")
                 .writeStream
                 .outputMode("complete")
                 .format("console")
                 .start()

query.awaitTermination() // hit cancel to terminate
Stream initializing...
------------------------------------------- Batch: 0 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -1.0| 9| | 0.0| 18| | 1.0| 41| | 2.0| 25| | 3.0| 5| | 4.0| 1| | 10.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 1 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 1| | -1.0| 13| | 0.0| 44| | 1.0| 83| | 2.0| 46| | 3.0| 10| | 4.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 2 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 2| | -1.0| 20| | 0.0| 74| | 1.0| 118| | 2.0| 70| | 3.0| 12| | 4.0| 1| | 9.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 3 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 4| | -1.0| 27| | 0.0| 104| | 1.0| 144| | 2.0| 96| | 3.0| 21| | 4.0| 1| | 9.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+ ------------------------------------------- Batch: 4 ------------------------------------------- +-----------+-----------------+ |binnedScore|binnedScoreCounts| +-----------+-----------------+ | -2.0| 4| | -1.0| 32| | 0.0| 125| | 1.0| 179| | 2.0| 125| | 3.0| 30| | 4.0| 2| | 9.0| 1| | 10.0| 1| | 12.0| 1| +-----------+-----------------+
Cancelled

Window Operations on Event Time

Aggregations over a sliding event-time window are straightforward with Structured Streaming and are very similar to grouped aggregations. In a grouped aggregation, aggregate values (e.g. counts) are maintained for each unique value in the user-specified grouping column. In case of window-based aggregations, aggregate values are maintained for each window the event-time of a row falls into. Let’s understand this with an illustration.

Imagine our quick example is modified and the stream now contains lines along with the time when the line was generated. Instead of running word counts, we want to count words within 10 minute windows, updating every 5 minutes. That is, word counts in words received between 10 minute windows 12:00 - 12:10, 12:05 - 12:15, 12:10 - 12:20, etc. Note that 12:00 - 12:10 means data that arrived after 12:00 but before 12:10. Now, consider a word that was received at 12:07. This word should increment the counts corresponding to two windows 12:00 - 12:10 and 12:05 - 12:15. So the counts will be indexed by both, the grouping key (i.e. the word) and the window (can be calculated from the event-time).

The result tables would look something like the following.

Window
Operations

Since this windowing is similar to grouping, in code, you can use groupBy() and window() operations to express windowed aggregations. You can see the full code for the below examples in Scala/Java/Python.

Make sure the streaming job with animal names is running (or finished running) with files in /datasets/streamingFiles directory - this is the Quick Example in 037a_FilesForStructuredStreaming notebook.

display(dbutils.fs.ls("/datasets/streamingFiles"))
dbfs:/datasets/streamingFiles/00_01.log00_01.log35
dbfs:/datasets/streamingFiles/00_03.log00_03.log35
dbfs:/datasets/streamingFiles/00_05.log00_05.log35
dbfs:/datasets/streamingFiles/00_07.log00_07.log35
dbfs:/datasets/streamingFiles/00_09.log00_09.log35
dbfs:/datasets/streamingFiles/00_11.log00_11.log35
dbfs:/datasets/streamingFiles/00_13.log00_13.log35
dbfs:/datasets/streamingFiles/00_15.log00_15.log35
dbfs:/datasets/streamingFiles/00_17.log00_17.log35
dbfs:/datasets/streamingFiles/00_19.log00_19.log35
dbfs:/datasets/streamingFiles/00_21.log00_21.log35
dbfs:/datasets/streamingFiles/00_23.log00_23.log35
dbfs:/datasets/streamingFiles/00_25.log00_25.log35
dbfs:/datasets/streamingFiles/00_27.log00_27.log35
dbfs:/datasets/streamingFiles/00_29.log00_29.log35
dbfs:/datasets/streamingFiles/00_31.log00_31.log35
dbfs:/datasets/streamingFiles/00_33.log00_33.log35
dbfs:/datasets/streamingFiles/00_35.log00_35.log35
dbfs:/datasets/streamingFiles/00_37.log00_37.log35
dbfs:/datasets/streamingFiles/00_39.log00_39.log35
dbfs:/datasets/streamingFiles/00_41.log00_41.log35
dbfs:/datasets/streamingFiles/00_43.log00_43.log35
dbfs:/datasets/streamingFiles/00_45.log00_45.log35
dbfs:/datasets/streamingFiles/00_47.log00_47.log35
dbfs:/datasets/streamingFiles/00_49.log00_49.log35
dbfs:/datasets/streamingFiles/00_51.log00_51.log35
dbfs:/datasets/streamingFiles/00_53.log00_53.log35
dbfs:/datasets/streamingFiles/00_55.log00_55.log35
dbfs:/datasets/streamingFiles/00_57.log00_57.log35
dbfs:/datasets/streamingFiles/00_59.log00_59.log35
dbfs:/datasets/streamingFiles/01_01.log01_01.log35
dbfs:/datasets/streamingFiles/01_03.log01_03.log35
dbfs:/datasets/streamingFiles/01_05.log01_05.log35
dbfs:/datasets/streamingFiles/01_07.log01_07.log35
dbfs:/datasets/streamingFiles/01_09.log01_09.log35
dbfs:/datasets/streamingFiles/01_11.log01_11.log35
dbfs:/datasets/streamingFiles/01_13.log01_13.log35
dbfs:/datasets/streamingFiles/01_15.log01_15.log35
dbfs:/datasets/streamingFiles/01_17.log01_17.log35
dbfs:/datasets/streamingFiles/01_19.log01_19.log35
dbfs:/datasets/streamingFiles/01_21.log01_21.log35
dbfs:/datasets/streamingFiles/01_23.log01_23.log35
dbfs:/datasets/streamingFiles/01_25.log01_25.log35
dbfs:/datasets/streamingFiles/01_27.log01_27.log35
dbfs:/datasets/streamingFiles/01_29.log01_29.log35
dbfs:/datasets/streamingFiles/01_31.log01_31.log35
dbfs:/datasets/streamingFiles/01_33.log01_33.log35
dbfs:/datasets/streamingFiles/01_35.log01_35.log35
dbfs:/datasets/streamingFiles/01_37.log01_37.log35
dbfs:/datasets/streamingFiles/01_39.log01_39.log35
dbfs:/datasets/streamingFiles/01_41.log01_41.log35
dbfs:/datasets/streamingFiles/01_43.log01_43.log35
dbfs:/datasets/streamingFiles/01_45.log01_45.log35
dbfs:/datasets/streamingFiles/01_47.log01_47.log35
dbfs:/datasets/streamingFiles/01_49.log01_49.log35
dbfs:/datasets/streamingFiles/01_51.log01_51.log35
dbfs:/datasets/streamingFiles/01_53.log01_53.log35
dbfs:/datasets/streamingFiles/01_55.log01_55.log35
dbfs:/datasets/streamingFiles/01_57.log01_57.log35
dbfs:/datasets/streamingFiles/01_59.log01_59.log35
dbfs:/datasets/streamingFiles/02_01.log02_01.log35
dbfs:/datasets/streamingFiles/02_03.log02_03.log35
dbfs:/datasets/streamingFiles/02_05.log02_05.log35
dbfs:/datasets/streamingFiles/02_07.log02_07.log35
dbfs:/datasets/streamingFiles/02_09.log02_09.log35
dbfs:/datasets/streamingFiles/02_11.log02_11.log35
dbfs:/datasets/streamingFiles/02_13.log02_13.log35
dbfs:/datasets/streamingFiles/02_15.log02_15.log35
dbfs:/datasets/streamingFiles/02_17.log02_17.log35
dbfs:/datasets/streamingFiles/02_19.log02_19.log35
dbfs:/datasets/streamingFiles/02_21.log02_21.log35
dbfs:/datasets/streamingFiles/02_23.log02_23.log35
dbfs:/datasets/streamingFiles/02_25.log02_25.log35
dbfs:/datasets/streamingFiles/02_27.log02_27.log35
dbfs:/datasets/streamingFiles/02_29.log02_29.log35
dbfs:/datasets/streamingFiles/02_31.log02_31.log35
dbfs:/datasets/streamingFiles/02_33.log02_33.log35
dbfs:/datasets/streamingFiles/02_35.log02_35.log35
dbfs:/datasets/streamingFiles/02_37.log02_37.log35
dbfs:/datasets/streamingFiles/02_39.log02_39.log35
dbfs:/datasets/streamingFiles/02_41.log02_41.log35
dbfs:/datasets/streamingFiles/02_43.log02_43.log35
dbfs:/datasets/streamingFiles/02_45.log02_45.log35
dbfs:/datasets/streamingFiles/02_47.log02_47.log35
dbfs:/datasets/streamingFiles/02_49.log02_49.log35
dbfs:/datasets/streamingFiles/02_51.log02_51.log35
dbfs:/datasets/streamingFiles/02_53.log02_53.log35
dbfs:/datasets/streamingFiles/02_55.log02_55.log35
dbfs:/datasets/streamingFiles/02_57.log02_57.log35
dbfs:/datasets/streamingFiles/02_59.log02_59.log35
dbfs:/datasets/streamingFiles/03_01.log03_01.log35
dbfs:/datasets/streamingFiles/03_03.log03_03.log35
dbfs:/datasets/streamingFiles/03_05.log03_05.log35
dbfs:/datasets/streamingFiles/03_07.log03_07.log35
dbfs:/datasets/streamingFiles/03_09.log03_09.log35
dbfs:/datasets/streamingFiles/03_11.log03_11.log35
dbfs:/datasets/streamingFiles/03_13.log03_13.log35
dbfs:/datasets/streamingFiles/03_15.log03_15.log35
dbfs:/datasets/streamingFiles/03_17.log03_17.log35
dbfs:/datasets/streamingFiles/03_19.log03_19.log35
spark.read.format("text").load("/datasets/streamingFiles").show(5,false) // let's just read five  entries
+----------------------------------+ |value | +----------------------------------+ |2019-05-31 15:00:01+00:00; bat dog| |2019-05-31 15:00:03+00:00; bat owl| |2019-05-31 15:00:05+00:00; rat bat| |2019-05-31 15:00:07+00:00; dog cat| |2019-05-31 15:00:09+00:00; cat pig| +----------------------------------+ only showing top 5 rows
import spark.implicits._
import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
import java.sql.Timestamp

// a static DS is convenient to work with
val csvStaticDS = spark
   .read
   .option("sep", ";") // delimiter is ';'
   .csv("/datasets/streamingFiles/*.log")    // Equivalent to format("csv").load("/path/to/directory")
   .toDF("time","animals")
   .as[(Timestamp, String)]
   .flatMap(
     line => line._2.split(" ")
                 .filter(_ != "") // Gustav's improvement
                 .map(animal => (line._1, animal))
    )
   //.filter(_._2 != "") // remove empty strings from the leading whitespaces
   .toDF("timestamp", "animal")
   .as[(Timestamp, String)]
import spark.implicits._ import org.apache.spark.sql.types._ import org.apache.spark.sql.functions._ import java.sql.Timestamp csvStaticDS: org.apache.spark.sql.Dataset[(java.sql.Timestamp, String)] = [timestamp: timestamp, animal: string]
csvStaticDS.show(5,false)
+-------------------+------+ |timestamp |animal| +-------------------+------+ |2019-05-31 15:00:01|bat | |2019-05-31 15:00:01|dog | |2019-05-31 15:00:03|bat | |2019-05-31 15:00:03|owl | |2019-05-31 15:00:05|rat | +-------------------+------+ only showing top 5 rows
//make a user-specified schema for structured streaming
val userSchema = new StructType()
                      .add("time", "String") // we will read it as String and then convert into timestamp later
                      .add("animals", "String")

// streaming DS
val csvStreamingDS = spark
// the next three lines are needed for structured streaming from file streams
  .readStream // for streaming
  .option("MaxFilesPerTrigger", 1) //  for streaming
  .schema(userSchema) // for streaming
  .option("sep", ";") // delimiter is ';'
  .csv("/datasets/streamingFiles/*.log")    // Equivalent to format("csv").load("/path/to/directory")
  .toDF("time","animals")
  .as[(Timestamp, String)]
  .flatMap(
     line => line._2.split(" ").map(animal => (line._1, animal))
    )
  .filter(_._2 != "")
  .toDF("timestamp", "animal")
  .as[(Timestamp, String)]
userSchema: org.apache.spark.sql.types.StructType = StructType(StructField(time,StringType,true), StructField(animals,StringType,true)) csvStreamingDS: org.apache.spark.sql.Dataset[(java.sql.Timestamp, String)] = [timestamp: timestamp, animal: string]
display(csvStreamingDS) // evaluate to see the animal words with timestamps streaming in
display_query_1(id: cb88a212-1de4-4217-bf1b-5c1151adedf3)
Last updated: 2687 days ago
2017-11-22T09:36:44.000+0000bat
2017-11-22T09:36:44.000+0000dog
2017-11-22T09:39:15.000+0000pig
2017-11-22T09:39:15.000+0000rat
2017-11-22T09:32:46.000+0000dog
2017-11-22T09:32:46.000+0000cat
2017-11-22T09:37:40.000+0000dog
2017-11-22T09:37:40.000+0000rat
2017-11-22T09:40:59.000+0000bat
2017-11-22T09:40:59.000+0000cat
// Group the data by window and word and compute the count of each group
val windowDuration = "180 seconds"
val slideDuration = "90 seconds"
val windowedCounts = csvStreamingDS.groupBy(
      window($"timestamp", windowDuration, slideDuration), $"animal"
    ).count().orderBy("window")

// Start running the query that prints the windowed word counts to the console
val query = windowedCounts.writeStream
      .outputMode("complete")
      .format("console")
      .option("truncate", "false")
      .start()

query.awaitTermination()
Stream initializing...
------------------------------------------- Batch: 0 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 1 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 2 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 3 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 4 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 5 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 6 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 7 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 8 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 9 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 10 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 11 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |2 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 12 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |2 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 13 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 14 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 15 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|bat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows
Cancelled

Handling Late Data and Watermarking

Now consider what happens if one of the events arrives late to the application. For example, say, a word generated at 12:04 (i.e. event time) could be received by the application at 12:11. The application should use the time 12:04 instead of 12:11 to update the older counts for the window 12:00 - 12:10. This occurs naturally in our window-based grouping – Structured Streaming can maintain the intermediate state for partial aggregates for a long period of time such that late data can update aggregates of old windows correctly, as illustrated below.

Handling Late Data

However, to run this query for days, it’s necessary for the system to bound the amount of intermediate in-memory state it accumulates. This means the system needs to know when an old aggregate can be dropped from the in-memory state because the application is not going to receive late data for that aggregate any more. To enable this, in Spark 2.1, we have introduced watermarking, which lets the engine automatically track the current event time in the data and attempt to clean up old state accordingly. You can define the watermark of a query by specifying the event time column and the threshold on how late the data is expected to be in terms of event time. For a specific window starting at time T, the engine will maintain state and allow late data to update the state until (max event time seen by the engine - late threshold > T). In other words, late data within the threshold will be aggregated, but data later than the threshold will be dropped. Let’s understand this with an example. We can easily define watermarking on the previous example using withWatermark() as shown below.

// Group the data by window and word and compute the count of each group
val windowDuration = "180 seconds"
val slideDuration = "90 seconds"
val watermarkDuration = "10 minutes"
val windowedCounts = csvStreamingDS
     .withWatermark("timestamp", watermarkDuration)
     .groupBy(
      window($"timestamp", windowDuration, slideDuration), $"animal"
    ).count().orderBy("window")

// Start running the query that prints the windowed word counts to the console
val query = windowedCounts.writeStream
      .outputMode("complete")
      .format("console")
      .option("truncate", "false")
      .start()

query.awaitTermination()
Stream initializing...
------------------------------------------- Batch: 0 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 1 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 2 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 3 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 4 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|owl |1 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|cat |1 | +------------------------------------------+------+-----+ ------------------------------------------- Batch: 5 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|cat |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|owl |1 | |[2019-05-31 15:04:30, 2019-05-31 15:07:30]|bat |2 | |[2019-05-31 15:06:00, 2019-05-31 15:09:00]|bat |2 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 6 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|owl |1 | |[2019-05-31 15:00:00, 2019-05-31 15:03:00]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|pig |1 | |[2019-05-31 15:01:30, 2019-05-31 15:04:30]|owl |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 7 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 8 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 9 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|bat |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|pig |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|rat |1 | |[2019-05-31 14:55:30, 2019-05-31 14:58:30]|pig |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 10 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|dog |1 | |[2019-05-31 14:54:00, 2019-05-31 14:57:00]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 11 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |2 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 12 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|owl |2 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|dog |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|bat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 13 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|owl |2 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|pig |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|rat |1 | |[2019-05-31 14:52:30, 2019-05-31 14:55:30]|pig |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 14 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:51:00, 2019-05-31 14:54:00]|rat |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 15 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|bat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 16 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|bat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 17 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|bat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|pig |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |2 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|rat |2 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 18 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|pig |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|cat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:45:00, 2019-05-31 14:48:00]|pig |1 | |[2019-05-31 14:45:00, 2019-05-31 14:48:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |2 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 19 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|pig |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|cat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:45:00, 2019-05-31 14:48:00]|cat |1 | |[2019-05-31 14:45:00, 2019-05-31 14:48:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |3 | +------------------------------------------+------+-----+ only showing top 20 rows ------------------------------------------- Batch: 20 ------------------------------------------- +------------------------------------------+------+-----+ |window |animal|count| +------------------------------------------+------+-----+ |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|rat |2 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|cat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|bat |1 | |[2019-05-31 14:40:30, 2019-05-31 14:43:30]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|cat |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|dog |1 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|pig |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|rat |2 | |[2019-05-31 14:42:00, 2019-05-31 14:45:00]|bat |2 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|pig |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|cat |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|dog |1 | |[2019-05-31 14:43:30, 2019-05-31 14:46:30]|bat |1 | |[2019-05-31 14:45:00, 2019-05-31 14:48:00]|cat |1 | |[2019-05-31 14:45:00, 2019-05-31 14:48:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|pig |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|owl |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|cat |1 | |[2019-05-31 14:48:00, 2019-05-31 14:51:00]|rat |1 | |[2019-05-31 14:49:30, 2019-05-31 14:52:30]|cat |3 | +------------------------------------------+------+-----+ only showing top 20 rows
Cancelled

In this example, we are defining the watermark of the query on the value of the column “timestamp”, and also defining “10 minutes” as the threshold of how late is the data allowed to be. If this query is run in Update output mode (discussed later in Output Modes section), the engine will keep updating counts of a window in the Result Table until the window is older than the watermark, which lags behind the current event time in column “timestamp” by 10 minutes. Here is an illustration.

Watermarking in Update
Mode

As shown in the illustration, the maximum event time tracked by the engine is the blue dashed line, and the watermark set as (max event time - '10 mins') at the beginning of every trigger is the red line For example, when the engine observes the data (12:14, dog), it sets the watermark for the next trigger as 12:04. This watermark lets the engine maintain intermediate state for additional 10 minutes to allow late data to be counted. For example, the data (12:09, cat) is out of order and late, and it falls in windows 12:05 - 12:15 and 12:10 - 12:20. Since, it is still ahead of the watermark 12:04 in the trigger, the engine still maintains the intermediate counts as state and correctly updates the counts of the related windows. However, when the watermark is updated to 12:11, the intermediate state for window (12:00 - 12:10) is cleared, and all subsequent data (e.g. (12:04, donkey)) is considered “too late” and therefore ignored. Note that after every trigger, the updated counts (i.e. purple rows) are written to sink as the trigger output, as dictated by the Update mode.

Some sinks (e.g. files) may not supported fine-grained updates that Update Mode requires. To work with them, we have also support Append Mode, where only the final counts are written to sink. This is illustrated below.

Note that using withWatermark on a non-streaming Dataset is no-op. As the watermark should not affect any batch query in any way, we will ignore it directly.

Watermarking in Append
Mode

Similar to the Update Mode earlier, the engine maintains intermediate counts for each window. However, the partial counts are not updated to the Result Table and not written to sink. The engine waits for “10 mins” for late date to be counted, then drops intermediate state of a window < watermark, and appends the final counts to the Result Table/sink. For example, the final counts of window 12:00 - 12:10 is appended to the Result Table only after the watermark is updated to 12:11.

Conditions for watermarking to clean aggregation state It is important to note that the following conditions must be satisfied for the watermarking to clean the state in aggregation queries (as of Spark 2.1.1, subject to change in the future).

  • Output mode must be Append or Update. Complete mode requires all aggregate data to be preserved, and hence cannot use watermarking to drop intermediate state. See the Output Modes section for detailed explanation of the semantics of each output mode.

  • The aggregation must have either the event-time column, or a window on the event-time column.

  • withWatermark must be called on the same column as the timestamp column used in the aggregate. For example, df.withWatermark("time", "1 min").groupBy("time2").count() is invalid in Append output mode, as watermark is defined on a different column from the aggregation column.

  • withWatermark must be called before the aggregation for the watermark details to be used. For example, df.groupBy("time").count().withWatermark("time", "1 min") is invalid in Append output mode.

Join Operations

Streaming DataFrames can be joined with static DataFrames to create new streaming DataFrames. Here are a few examples.

val staticDf = spark.read. ...
val streamingDf = spark.readStream. ...

streamingDf.join(staticDf, "type")          // inner equi-join with a static DF
streamingDf.join(staticDf, "type", "right_join")  // right outer join with a static DF

Streaming Deduplication

You can deduplicate records in data streams using a unique identifier in the events. This is exactly same as deduplication on static using a unique identifier column. The query will store the necessary amount of data from previous records such that it can filter duplicate records. Similar to aggregations, you can use deduplication with or without watermarking.

  • With watermark - If there is a upper bound on how late a duplicate record may arrive, then you can define a watermark on a event time column and deduplicate using both the guid and the event time columns. The query will use the watermark to remove old state data from past records that are not expected to get any duplicates any more. This bounds the amount of the state the query has to maintain.

  • Without watermark - Since there are no bounds on when a duplicate record may arrive, the query stores the data from all the past records as state.

    val streamingDf = spark.readStream. ...  // columns: guid, eventTime, ...

    // Without watermark using guid column
    streamingDf.dropDuplicates("guid")

    // With watermark using guid and eventTime columns
    streamingDf
      .withWatermark("eventTime", "10 seconds")
      .dropDuplicates("guid", "eventTime")

Arbitrary Stateful Operations

Many uscases require more advanced stateful operations than aggregations. For example, in many usecases, you have to track sessions from data streams of events. For doing such sessionization, you will have to save arbitrary types of data as state, and perform arbitrary operations on the state using the data stream events in every trigger. Since Spark 2.2, this can be done using the operation mapGroupsWithState and the more powerful operation flatMapGroupsWithState. Both operations allow you to apply user-defined code on grouped Datasets to update user-defined state. For more concrete details, take a look at the API documentation (Scala/Java) and the examples (Scala/Java).

Unsupported Operations

There are a few DataFrame/Dataset operations that are not supported with streaming DataFrames/Datasets. Some of them are as follows.

  • Multiple streaming aggregations (i.e. a chain of aggregations on a streaming DF) are not yet supported on streaming Datasets.

  • Limit and take first N rows are not supported on streaming Datasets.

  • Distinct operations on streaming Datasets are not supported.

  • Sorting operations are supported on streaming Datasets only after an aggregation and in Complete Output Mode.

  • Outer joins between a streaming and a static Datasets are conditionally supported.

    • Full outer join with a streaming Dataset is not supported

    • Left outer join with a streaming Dataset on the right is not supported

    • Right outer join with a streaming Dataset on the left is not supported

  • Any kind of joins between two streaming Datasets is not yet supported.

In addition, there are some Dataset methods that will not work on streaming Datasets. They are actions that will immediately run queries and return results, which does not make sense on a streaming Dataset. Rather, those functionalities can be done by explicitly starting a streaming query (see the next section regarding that).

  • count() - Cannot return a single count from a streaming Dataset. Instead, use ds.groupBy().count() which returns a streaming Dataset containing a running count.

  • foreach() - Instead use ds.writeStream.foreach(...) (see next section).

  • show() - Instead use the console sink (see next section).

If you try any of these operations, you will see an AnalysisException like “operation XYZ is not supported with streaming DataFrames/Datasets”. While some of them may be supported in future releases of Spark, there are others which are fundamentally hard to implement on streaming data efficiently. For example, sorting on the input stream is not supported, as it requires keeping track of all the data received in the stream. This is therefore fundamentally hard to execute efficiently.

Starting Streaming Queries

Once you have defined the final result DataFrame/Dataset, all that is left is for you to start the streaming computation. To do that, you have to use the DataStreamWriter (Scala/Java/Python docs) returned through Dataset.writeStream(). You will have to specify one or more of the following in this interface.

  • Details of the output sink: Data format, location, etc.

  • Output mode: Specify what gets written to the output sink.

  • Query name: Optionally, specify a unique name of the query for identification.

  • Trigger interval: Optionally, specify the trigger interval. If it is not specified, the system will check for availability of new data as soon as the previous processing has completed. If a trigger time is missed because the previous processing has not completed, then the system will attempt to trigger at the next trigger point, not immediately after the processing has completed.

  • Checkpoint location: For some output sinks where the end-to-end fault-tolerance can be guaranteed, specify the location where the system will write all the checkpoint information. This should be a directory in an HDFS-compatible fault-tolerant file system. The semantics of checkpointing is discussed in more detail in the next section.

Output Modes

There are a few types of output modes.

  • Append mode (default) - This is the default mode, where only the new rows added to the Result Table since the last trigger will be outputted to the sink. This is supported for only those queries where rows added to the Result Table is never going to change. Hence, this mode guarantees that each row will be output only once (assuming fault-tolerant sink). For example, queries with only select, where, map, flatMap, filter, join, etc. will support Append mode.

  • Complete mode - The whole Result Table will be outputted to the sink after every trigger. This is supported for aggregation queries.

  • Update mode - (Available since Spark 2.1.1) Only the rows in the Result Table that were updated since the last trigger will be outputted to the sink. More information to be added in future releases.

Different types of streaming queries support different output modes. Here is the compatibility matrix.




Query Type Supported Output Modes Notes
Queries with aggregation Aggregation on event-time with watermark Append, Update, Complete Append mode uses watermark to drop old aggregation state. But the output of a windowed aggregation is delayed the late threshold specified in withWatermark() as by the modes semantics, rows can be added to the Result Table only once after they are finalized (i.e. after watermark is crossed). See the Late Data section for more details.

Update mode uses watermark to drop old aggregation state.

Complete mode does not drop old aggregation state since by definition this mode preserves all data in the Result Table.
Other aggregations Complete, Update Since no watermark is defined (only defined in other category), old aggregation state is not dropped.

Append mode is not supported as aggregates can update thus violating the semantics of this mode.
Queries with mapGroupsWithState Update
Queries with flatMapGroupsWithState Append operation mode Append Aggregations are allowed after flatMapGroupsWithState.
Update operation mode Update Aggregations not allowed after flatMapGroupsWithState.
Other queries Append, Update Complete mode not supported as it is infeasible to keep all unaggregated data in the Result Table.

Output Sinks

There are a few types of built-in output sinks.

  • File sink - Stores the output to a directory.
    writeStream
        .format("parquet")        // can be "orc", "json", "csv", etc.
        .option("path", "path/to/destination/dir")
        .start()
  • Foreach sink - Runs arbitrary computation on the records in the output. See later in the section for more details.
    writeStream
        .foreach(...)
        .start()
  • Console sink (for debugging) - Prints the output to the console/stdout every time there is a trigger. Both, Append and Complete output modes, are supported. This should be used for debugging purposes on low data volumes as the entire output is collected and stored in the driver’s memory after every trigger.
    writeStream
        .format("console")
        .start()
  • Memory sink (for debugging) - The output is stored in memory as an in-memory table. Both, Append and Complete output modes, are supported. This should be used for debugging purposes on low data volumes as the entire output is collected and stored in the driver’s memory. Hence, use it with caution.
    writeStream
        .format("memory")
        .queryName("tableName")
        .start()

Some sinks are not fault-tolerant because they do not guarantee persistence of the output and are meant for debugging purposes only. See the earlier section on fault-tolerance semantics. Here are the details of all the sinks in Spark.

Sink Supported Output Modes Options Fault-tolerant Notes
File Sink Append path: path to the output directory, must be specified.

For file-format-specific options, see the related methods in DataFrameWriter (Scala/Java/Python/R). E.g. for "parquet" format options see DataFrameWriter.parquet()
Yes Supports writes to partitioned tables. Partitioning by time may be useful.
Foreach Sink Append, Update, Compelete None Depends on ForeachWriter implementation More details in the next section
Console Sink Append, Update, Complete numRows: Number of rows to print every trigger (default: 20)
truncate: Whether to truncate the output if too long (default: true)
No
Memory Sink Append, Complete None No. But in Complete Mode, restarted query will recreate the full table. Table name is the query name.

Note that you have to call start() to actually start the execution of the query. This returns a StreamingQuery object which is a handle to the continuously running execution. You can use this object to manage the query, which we will discuss in the next subsection. For now, let’s understand all this with a few examples.

    // ========== DF with no aggregations ==========
    val noAggDF = deviceDataDf.select("device").where("signal > 10")   

    // Print new data to console
    noAggDF
      .writeStream
      .format("console")
      .start()

    // Write new data to Parquet files
    noAggDF
      .writeStream
      .format("parquet")
      .option("checkpointLocation", "path/to/checkpoint/dir")
      .option("path", "path/to/destination/dir")
      .start()

    // ========== DF with aggregation ==========
    val aggDF = df.groupBy("device").count()

    // Print updated aggregations to console
    aggDF
      .writeStream
      .outputMode("complete")
      .format("console")
      .start()

    // Have all the aggregates in an in-memory table
    aggDF
      .writeStream
      .queryName("aggregates")    // this query name will be the table name
      .outputMode("complete")
      .format("memory")
      .start()

    spark.sql("select * from aggregates").show()   // interactively query in-memory table

Using Foreach

The foreach operation allows arbitrary operations to be computed on the output data. As of Spark 2.1, this is available only for Scala and Java. To use this, you will have to implement the interface ForeachWriter (Scala/Java docs), which has methods that get called whenever there is a sequence of rows generated as output after a trigger. Note the following important points.

  • The writer must be serializable, as it will be serialized and sent to the executors for execution.

  • All the three methods, open, process and close will be called on the executors.

  • The writer must do all the initialization (e.g. opening connections, starting a transaction, etc.) only when the open method is called. Be aware that, if there is any initialization in the class as soon as the object is created, then that initialization will happen in the driver (because that is where the instance is being created), which may not be what you intend.

  • version and partition are two parameters in open that uniquely represent a set of rows that needs to be pushed out. version is a monotonically increasing id that increases with every trigger. partition is an id that represents a partition of the output, since the output is distributed and will be processed on multiple executors.

  • open can use the version and partition to choose whether it needs to write the sequence of rows. Accordingly, it can return true (proceed with writing), or false (no need to write). If false is returned, then process will not be called on any row. For example, after a partial failure, some of the output partitions of the failed trigger may have already been committed to a database. Based on metadata stored in the database, the writer can identify partitions that have already been committed and accordingly return false to skip committing them again.

  • Whenever open is called, close will also be called (unless the JVM exits due to some error). This is true even if open returns false. If there is any error in processing and writing the data, close will be called with the error. It is your responsibility to clean up state (e.g. connections, transactions, etc.) that have been created in open such that there are no resource leaks.

Managing Streaming Queries

The StreamingQuery object created when a query is started can be used to monitor and manage the query.

    val query = df.writeStream.format("console").start()   // get the query object

    query.id          // get the unique identifier of the running query that persists across restarts from checkpoint data

    query.runId       // get the unique id of this run of the query, which will be generated at every start/restart

    query.name        // get the name of the auto-generated or user-specified name

    query.explain()   // print detailed explanations of the query

    query.stop()      // stop the query

    query.awaitTermination()   // block until query is terminated, with stop() or with error

    query.exception       // the exception if the query has been terminated with error

    query.recentProgress  // an array of the most recent progress updates for this query

    query.lastProgress    // the most recent progress update of this streaming query