You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -267,6 +268,48 @@ Finally, all these tuple helper functions are also baked in:
267
268
-`map`
268
269
-`cast`
269
270
271
+
### Streaming
272
+
273
+
A popular Spark extension is [Spark Streaming](https://spark.apache.org/docs/latest/streaming-programming-guide.html).
274
+
Of course the Kotlin Spark API also introduces a more Kotlin-esque approach to write your streaming programs.
275
+
There are examples for use with a checkpoint, Kafka and SQL in the [examples module](examples/src/main/kotlin/org/jetbrains/kotlinx/spark/examples/streaming).
276
+
277
+
We shall also provide a quick example below:
278
+
```kotlin
279
+
// Automatically provides ssc: JavaStreamingContext which starts and awaits termination or timeout
// create input stream for, for instance, Netcat: `$ nc -lk 9999`
283
+
val lines:JavaReceiverInputDStream<String> = ssc.socketTextStream("localhost", 9999)
284
+
285
+
// split input stream on space
286
+
val words:JavaDStream<String> = lines.flatMap { it.split("").iterator() }
287
+
288
+
// perform action on each formed RDD in the stream
289
+
words.foreachRDD { rdd:JavaRDD<String>, _:Time->
290
+
291
+
// to convert the JavaRDD to a Dataset, we need a spark session using the RDD context
292
+
withSpark(rdd) { // this: KSparkSession
293
+
val dataframe:Dataset<TestRow> = rdd.map { TestRow(word = it) }.toDS()
294
+
dataframe
295
+
.groupByKey { it.word }
296
+
.count()
297
+
.show()
298
+
// +-----+--------+
299
+
// | key|count(1)|
300
+
// +-----+--------+
301
+
// |hello| 1|
302
+
// | is| 1|
303
+
// | a| 1|
304
+
// | this| 1|
305
+
// | test| 3|
306
+
// +-----+--------+
307
+
}
308
+
}
309
+
}
310
+
```
311
+
312
+
270
313
## Examples
271
314
272
315
For more, check out [examples](https://github.com/JetBrains/kotlin-spark-api/tree/master/examples/src/main/kotlin/org/jetbrains/kotlinx/spark/examples) module.
0 commit comments