spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anabranch <...@git.apache.org>
Subject [GitHub] spark pull request #13945: [SPARK-16256][SQL][STREAMING] Added Structured St...
Date Tue, 28 Jun 2016 16:18:10 GMT
Github user anabranch commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13945#discussion_r68790342
  
    --- Diff: docs/structured-streaming-programming-guide.md ---
    @@ -0,0 +1,888 @@
    +---
    +layout: global
    +displayTitle: Structured Streaming Programming Guide [Alpha]
    +title: Structured Streaming Programming Guide
    +---
    +
    +* This will become a table of contents (this text will be scraped).
    +{:toc}
    +
    +# Overview
    +Structured Streaming is a scalable and fault-tolerant stream processing engine 
    +built on the Spark SQL engine. You can express your streaming computation by 
    +thinking you are running a batch computation on a static dataset, and the 
    +Spark SQL engine takes care of running it incrementally and continuously 
    +updating the final result as streaming data keeps arriving. You can use the 
    +[Dataset/DataFrame API](sql-programming-guide.html) in Scala, Java or Python 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, *Stuctured Streaming provides 
    +fast, scalable, fault-tolerant, end-to-end exactly-once stream processing 
    +without the user having to reason about streaming.*
    +
    +**Spark 2.0 is the ALPHA RELEASE of Structured Streaming** and the APIs are still experimental.
In this guide, we are going to walk you through the programming model and the APIs. First,
lets start with a simple example - a streaming word count. 
    +
    +# Quick Example
    +Let’s say you want maintain a running word count of text data received from a data
server listening on a TCP socket. Let’s see how you can express this using Structured Streaming.
You can see the full code in Scala/Java/Python. And if you download Spark, you can directly
run the example. In any case, let’s walk through the example step-by-step and understand
how it is works. First, we have to import the names of the necessary classes and create a
local SparkSession, the starting point of all functionalities related to Spark.
    +
    +<div class="codetabs">
    +<div data-lang="scala"  markdown="1">
    +
    +{% highlight scala %}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.SparkSession
    +
    +val spark = SparkSession
    +  .builder
    +  .appName("StructuredNetworkWordCount")
    +  .getOrCreate()
    +{% endhighlight %}
    +
    +</div>
    +<div data-lang="java"  markdown="1">
    +
    +{% highlight java %}
    +import org.apache.spark.sql.*;
    +import org.apache.spark.sql.streaming.StreamingQuery;
    +
    +SparkSession spark = SparkSession
    +    .builder()
    +    .appName("JavaStructuredNetworkWordCount")
    +    .getOrCreate();
    +{% endhighlight %}
    +
    +</div>
    +<div data-lang="python"  markdown="1">
    +
    +{% highlight python %}
    +from pyspark.sql import SparkSession
    +from pyspark.sql.functions import explode
    +from pyspark.sql.functions import split
    +
    +spark = SparkSession\
    +    .builder()\
    +    .appName("StructuredNetworkWordCount")\
    +    .getOrCreate()
    +{% endhighlight %}
    +
    +</div>
    +</div>
    +
    +Next, let’s create a streaming DataFrame that represents text data received from a
server listening on localhost:9999, and transform the DataFrame to calculate word counts.
    +
    +<div class="codetabs">
    +<div data-lang="scala"  markdown="1">
    +
    +{% highlight scala %}
    +val lines = spark.readStream
    +  .format("socket")
    +  .option("host", "localhost")
    +  .option("port", 9999)
    +  .load()
    +
    +val words = lines.select(
    +  explode(
    +    split(lines.col("value"), " ")
    +  ).alias("word")
    +)
    +
    +val wordCounts = words.groupBy("word").count()
    +{% endhighlight %}
    +
    +</div>
    +<div data-lang="java"  markdown="1">
    +
    +{% highlight java %}
    +Dataset<Row> lines = spark
    + .readStream()
    + .format("socket")
    + .option("host", "localhost")
    + .option("port", 9999)
    + .load();
    +
    +Dataset<Row> words = lines.select(
    + functions.explode(
    +   functions.split(lines.col("value"), " ")
    + ).alias("word")
    +);
    +
    +Dataset<Row> wordCounts = words.groupBy("word").count();
    +{% endhighlight %}
    +
    +</div>
    +<div data-lang="python"  markdown="1">
    +
    +{% highlight python %}
    +lines = spark\
    +   .readStream\
    +   .format('socket')\
    +   .option('host', 'localhost')\
    +   .option('port', 9999)\
    +   .load()
    +
    +words = lines.select(
    +   explode(
    +       split(lines.value, ' ')
    +   ).alias('word')
    +)
    +
    +wordCounts = words.groupBy('word').count()
    +{% endhighlight %}
    +
    +</div>
    +</div>
    +
    +This `lines` DataFrame is like an unbounded table containing the streaming 
    +text data. This table contains one column of string named “value”, and each 
    +line in the streaming text data is like a row in this table. Note, that this 
    +is not currently receiving any data as we are just setting up the 
    +transformation, and have not yet started it. Next, we have used to built-in 
    +SQL functions - split and explode, to split each line into multiple rows with 
    +a word each. In addition, we use the function `alias` to name the new column 
    +as “word”. Finally, we have defined the running counts, by grouping the `words`
    +DataFrame by the column `word` and count on that grouping. 
    +
    +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 output the counts to the console every time they are updated. In 
    --- End diff --
    
    Think we can clean up these last two sentences.
    
    > There are other details that will be mentioned on in the guide that are relevant
for this problem like the specific checkpoint location where checkpoint data will be stored.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message