假设我有一个温度预测数据的主题,如下:
2018-10-25,Melbourne,21
2018-10-26,Melbourne,17
2018-10-27,Melbourne,21
2018-10-25,Sydney,22
2018-10-26,Sydney,20
2018-10-27,Sydney,23
2018-10-26,Melbourne,18
2018-10-27,Melbourne,22
2018-10-26,Sydney,21
2018-10-27,Sydney,24
每个条目都包含一个日期、一个城市和一个预测温度,并表示该城市在该日期的预测更新。我可以将其描述为这样的 KSQL 流:
CREATE STREAM forecasts_csv ( \
date VARCHAR, \
city VARCHAR, \
temperature INTEGER \
) WITH (kafka_topic='forecasts-csv', value_format='DELIMITED');
现在,我想要一个表格来代表每个城市的当前(即最新)预测温度,以及该预测随时间变化的最小值和最大值。所需输出的示例是:
{ date='2018-10-27', city='Melbourne', latest=22, min=21, max=22 }
我怎样才能做到这一点?
我设法得到聚合(最小/最大)如下:
CREATE STREAM forecasts_keyed \
WITH (partitions=4, value_format='JSON') \
AS SELECT date + '/' + city AS forecast_key, * \
FROM forecasts_csv \
PARTITION BY forecast_key;
CREATE TABLE forecasts_minmax \
WITH (partitions=4, value_format='JSON') \
AS SELECT forecast_key, date, city, \
min(temperature) as min, max(temperature) as max \
FROM forecasts_keyed \
GROUP by forecast_key, date, city;
这给了我输出消息,如:
{"FORECAST_KEY":"2018-10-27/Melbourne","DATE":"2018-10-27","CITY":"Melbourne","MIN":21,"MAX":22}
但我不知道如何将它与“最新”阅读结合起来。