【发布时间】:2019-03-03 11:39:05
【问题描述】:
尝试在 Spark 结构化流方面理解 SparkSql。
Spark Session 从 kafka 主题中读取事件,将数据聚合到按不同列名分组的计数并将其打印到控制台。
原始输入数据结构如下:
+--------------+--------------------+----------+----------+-------+-------------------+--------------------+----------+
|. sourceTypes| Guid| platform|datacenter|pagesId| eventTimestamp| Id1234| Id567890|
+--------------+--------------------+----------+----------+-------+-------------------+--------------------+----------+
| Notififcation|....................| ANDROID| dev| aa|2018-09-27 09:41:29|fce81f05-a085-392...|{"id":...|
| Notififcation|....................| ANDROID| dev| ab|2018-09-27 09:41:29|fce81f05-a085-392...|{"id":...|
| Notififcation|....................| WEBOS| dev| aa|2018-09-27 09:42:46|0ee089c1-d5da-3b3...|{"id":...|
| Notififcation|....................| WEBOS| dev| aa|2018-09-27 09:42:48|57c18964-40c9-311...|{"id":...|
| Notififcation|....................| WEBOS| dev| aa|2018-09-27 09:42:48|5ecf1d77-321a-379...|{"id":...|
| Notififcation|....................| WEBOS| dev| aa|2018-09-27 09:42:48|5ecf1d77-321a-379...|{"id":...|
| Notififcation|....................| WEBOS| dev| aa|2018-09-27 09:42:52|d9fc4cfa-0934-3e9...|{"id":...|
+--------------+--------------------+----------+----------+-------+-------------------+--------------------+---------+
sourceTypes、platform、datacenter 和 pageId 需要计数。
使用以下代码聚合数据:
Dataset<Row> query = sourceDataset
.withWatermark("eventTimestamp", watermarkInterval)
.select(
col("eventTimestamp"),
col("datacenter"),
col("platform"),
col("pageId")
)
.groupBy(
window(col("eventTimestamp"), windowInterval),
col("datacenter"),
col("platform"),
col("pageId")
)
.agg(
max(col("eventTimestamp"))
);
这里是watermarkInterval=45seconds、windowInterval=15seconds 和triggerInterval=15seconds。
使用新的聚合数据集:
aggregatedDataset
.writeStream()
.outputMode(OutputMode.Append())
.format("console")
.trigger(Trigger.ProcessingTime(triggerInterval))
.start();
有几个问题:
输出数据未打印每个
groupBy的计数,例如平台、pageId 等。如何以 json 格式打印输出?我尝试在控制台输出数据时使用
select(to_json(struct("*")).as("value")),但它不起作用。
【问题讨论】:
标签: java apache-spark apache-spark-sql spark-streaming spark-structured-streaming