Spark2.0: Structured Streaming

启动nc:

nc -lk 9999

启动spark-shell:

val lines = spark.readStream.
     | format("socket").
     | option("host","localhost").
     | option("port",9999).
     | load();

lines: org.apache.spark.sql.DataFrame = [value: string]

val words = lines.as[String].flatMap(_.split(" "))
val wordCounts = words.groupBy("value").count()

val query = wordCounts.writeStream.
     | outputMode("complete").
     | format("console").
     | start();

query: org.apache.spark.sql.streaming.StreamingQuery = org.apache.spark.sql.execution.streaming.Streamin          gQueryWrapper@22cf6c9c

query.awaitTermination()

-------------------------------------------
Batch: 0
-------------------------------------------
+-----+-----+
|value|count|
+-----+-----+
|hello|    1|
| word|    1|
+-----+-----+

-------------------------------------------
Batch: 1
-------------------------------------------
+-----+-----+
|value|count|
+-----+-----+
|hello|    2|
| word|    1|
|world|    1|
+-----+-----+

-------------------------------------------
Batch: 2
-------------------------------------------
+-----+-----+
|value|count|
+-----+-----+
|hello|    2|
|  bye|    1|
| word|    1|
|world|    2|
|   ny|    1|
+-----+-----+
-------------------------------------------
Batch: 3
-------------------------------------------
+------+-----+
| value|count|
+------+-----+
| hello|    2|
|   bye|    1|
|  word|    1|
|sounds|    1|
| world|    2|
|  good|    1|
|    ny|    1|
+------+-----+

-------------------------------------------
Batch: 4
-------------------------------------------
+------+-----+
| value|count|
+------+-----+
| hello|    2|
|   bye|    3|
|  word|    1|
|sounds|    1|
| world|    2|
|  good|    1|
|    ny|    1|
+------+-----+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值