@@ -58,11 +58,21 @@ do is as follows.
58
58
59
59
<div class =" codetabs " >
60
60
<div data-lang =" scala " markdown =" 1 " >
61
+ First, we import the names of the Spark Streaming classes, and some implicit
62
+ conversions from StreamingContext into our environment, to add useful methods to
63
+ other classes we need (like DStream).
61
64
62
- First, we create a
63
- [ StreamingContext] ( api/streaming/index.html#org.apache.spark.streaming.StreamingContext ) object,
64
- which is the main entry point for all streaming
65
- functionality. Besides Spark's configuration, we specify that any DStream will be processed
65
+ [ StreamingContext] ( api/streaming/index.html#org.apache.spark.streaming.StreamingContext ) is the
66
+ main entry point for all streaming functionality.
67
+
68
+ {% highlight scala %}
69
+ import org.apache.spark.streaming._
70
+ import org.apache.spark.streaming.StreamingContext._
71
+ {% endhighlight %}
72
+
73
+ Then we create a
74
+ [ StreamingContext] ( api/streaming/index.html#org.apache.spark.streaming.StreamingContext ) object.
75
+ Besides Spark's configuration, we specify that any DStream will be processed
66
76
in 1 second batches.
67
77
68
78
{% highlight scala %}
@@ -88,7 +98,7 @@ val words = lines.flatMap(_.split(" "))
88
98
{% endhighlight %}
89
99
90
100
` flatMap ` is a one-to-many DStream operation that creates a new DStream by
91
- generating multiple new records from each record int the source DStream. In this case,
101
+ generating multiple new records from each record in the source DStream. In this case,
92
102
each line will be split into multiple words and the stream of words is represented as the
93
103
` words ` DStream. Next, we want to count these words.
94
104
@@ -98,7 +108,7 @@ val pairs = words.map(word => (word, 1))
98
108
val wordCounts = pairs.reduceByKey(_ + _ )
99
109
100
110
// Print a few of the counts to the console
101
- wordCount .print()
111
+ wordCounts .print()
102
112
{% endhighlight %}
103
113
104
114
The ` words ` DStream is further mapped (one-to-one transformation) to a DStream of `(word,
@@ -178,7 +188,7 @@ JavaPairDStream<String, Integer> wordCounts = pairs.reduceByKey(
178
188
return i1 + i2;
179
189
}
180
190
});
181
- wordCount .print(); // Print a few of the counts to the console
191
+ wordCounts .print(); // Print a few of the counts to the console
182
192
{% endhighlight %}
183
193
184
194
The ` words ` DStream is further mapped (one-to-one transformation) to a DStream of `(word,
@@ -262,6 +272,24 @@ Time: 1357008430000 ms
262
272
</td >
263
273
</table >
264
274
275
+ If you plan to run the Scala code for Spark Streaming-based use cases in the Spark
276
+ shell, you should start the shell with the SparkConfiguration pre-configured to
277
+ discard old batches periodically:
278
+
279
+ {% highlight bash %}
280
+ $ SPARK_JAVA_OPTS=-Dspark.cleaner.ttl=10000 bin/spark-shell
281
+ {% endhighlight %}
282
+
283
+ ... and create your StreamingContext by wrapping the existing interactive shell
284
+ SparkContext object, ` sc ` :
285
+
286
+ {% highlight scala %}
287
+ val ssc = new StreamingContext(sc, Seconds(1))
288
+ {% endhighlight %}
289
+
290
+ When working with the shell, you may also need to send a ` ^D ` to your netcat session
291
+ to force the pipeline to print the word counts to the console at the sink.
292
+
265
293
***************************************************************************************************
266
294
267
295
# Basics
@@ -428,9 +456,9 @@ KafkaUtils.createStream(javaStreamingContext, kafkaParams, ...);
428
456
</div >
429
457
</div >
430
458
431
- For more details on these additional sources, see the corresponding [ API documentation]
432
- (#where-to-go-from-here). Furthermore, you can also implement your own custom receiver
433
- for your sources. See the [ Custom Receiver Guide] ( streaming-custom-receivers.html ) .
459
+ For more details on these additional sources, see the corresponding [ API documentation] ( #where-to-go-from-here ) .
460
+ Furthermore, you can also implement your own custom receiver for your sources. See the
461
+ [ Custom Receiver Guide] ( streaming-custom-receivers.html ) .
434
462
435
463
## Operations
436
464
There are two kinds of DStream operations - _ transformations_ and _ output operations_ . Similar to
@@ -511,7 +539,7 @@ common ones are as follows.
511
539
<td > <b >updateStateByKey</b >(<i >func</i >) </td >
512
540
<td > Return a new "state" DStream where the state for each key is updated by applying the
513
541
given function on the previous state of the key and the new values for the key. This can be
514
- used to maintain arbitrary state data for each ket .</td >
542
+ used to maintain arbitrary state data for each key .</td >
515
543
</tr >
516
544
<tr ><td ></td ><td ></td ></tr >
517
545
</table >
@@ -520,9 +548,8 @@ The last two transformations are worth highlighting again.
520
548
521
549
<h4 >UpdateStateByKey Operation</h4 >
522
550
523
- The ` updateStateByKey ` operation allows
524
- you to main arbitrary stateful computation, where you want to maintain some state data and
525
- continuously update it with new information. To use this, you will have to do two steps.
551
+ The ` updateStateByKey ` operation allows you to maintain arbitrary state while continuously updating
552
+ it with new information. To use this, you will have to do two steps.
526
553
527
554
1 . Define the state - The state can be of arbitrary data type.
528
555
1 . Define the state update function - Specify with a function how to update the state using the
@@ -925,7 +952,7 @@ exception saying so.
925
952
## Monitoring
926
953
Besides Spark's in-built [ monitoring capabilities] ( monitoring.html ) ,
927
954
the progress of a Spark Streaming program can also be monitored using the [ StreamingListener]
928
- (streaming/index.html#org.apache.spark.scheduler.StreamingListener) interface,
955
+ (api/ streaming/index.html#org.apache.spark.scheduler.StreamingListener) interface,
929
956
which allows you to get statistics of batch processing times, queueing delays,
930
957
and total end-to-end delays. Note that this is still an experimental API and it is likely to be
931
958
improved upon (i.e., more information reported) in the future.
@@ -1000,11 +1027,11 @@ Since all data is modeled as RDDs with their lineage of deterministic operations
1000
1027
for output operations.
1001
1028
1002
1029
## Failure of the Driver Node
1003
- To allows a streaming application to operate 24/7, Spark Streaming allows a streaming computation
1030
+ For a streaming application to operate 24/7, Spark Streaming allows a streaming computation
1004
1031
to be resumed even after the failure of the driver node. Spark Streaming periodically writes the
1005
1032
metadata information of the DStreams setup through the ` StreamingContext ` to a
1006
1033
HDFS directory (can be any Hadoop-compatible filesystem). This periodic
1007
- * checkpointing* can be enabled by setting a the checkpoint
1034
+ * checkpointing* can be enabled by setting the checkpoint
1008
1035
directory using ` ssc.checkpoint(<checkpoint directory>) ` as described
1009
1036
[ earlier] ( #rdd-checkpointing ) . On failure of the driver node,
1010
1037
the lost ` StreamingContext ` can be recovered from this information, and restarted.
@@ -1105,8 +1132,8 @@ classes. So, if you are using `getOrCreate`, then make sure that the checkpoint
1105
1132
explicitly deleted every time recompiled code needs to be launched.
1106
1133
1107
1134
This failure recovery can be done automatically using Spark's
1108
- [ standalone cluster mode] ( spark-standalone.html ) , which allows any Spark
1109
- application's driver to be as well as, ensures automatic restart of the driver on failure (see
1135
+ [ standalone cluster mode] ( spark-standalone.html ) , which allows the driver of any Spark application
1136
+ to be launched within the cluster and be restarted on failure (see
1110
1137
[ supervise mode] ( spark-standalone.html#launching-applications-inside-the-cluster ) ). This can be
1111
1138
tested locally by launching the above example using the supervise mode in a
1112
1139
local standalone cluster and killing the java process running the driver (will be shown as
@@ -1123,7 +1150,7 @@ There are two different failure behaviors based on which input sources are used.
1123
1150
1 . _ Using HDFS files as input source_ - Since the data is reliably stored on HDFS, all data can
1124
1151
re-computed and therefore no data will be lost due to any failure.
1125
1152
1 . _ Using any input source that receives data through a network_ - The received input data is
1126
- replicated in memory to multiple nodes. Since, all the data in the Spark worker's memory is lost
1153
+ replicated in memory to multiple nodes. Since all the data in the Spark worker's memory is lost
1127
1154
when the Spark driver fails, the past input data will not be accessible and driver recovers.
1128
1155
Hence, if stateful and window-based operations are used
1129
1156
(like ` updateStateByKey ` , ` window ` , ` countByValueAndWindow ` , etc.), then the intermediate state
@@ -1133,11 +1160,11 @@ In future releases, we will support full recoverability for all input sources. N
1133
1160
non-stateful transformations like ` map ` , ` count ` , and ` reduceByKey ` , with _ all_ input streams,
1134
1161
the system, upon restarting, will continue to receive and process new data.
1135
1162
1136
- To better understand the behavior of the system under driver failure with a HDFS source, lets
1163
+ To better understand the behavior of the system under driver failure with a HDFS source, let's
1137
1164
consider what will happen with a file input stream. Specifically, in the case of the file input
1138
1165
stream, it will correctly identify new files that were created while the driver was down and
1139
1166
process them in the same way as it would have if the driver had not failed. To explain further
1140
- in the case of file input stream, we shall use an example. Lets say, files are being generated
1167
+ in the case of file input stream, we shall use an example. Let's say, files are being generated
1141
1168
every second, and a Spark Streaming program reads every new file and output the number of lines
1142
1169
in the file. This is what the sequence of outputs would be with and without a driver failure.
1143
1170
0 commit comments