5

假设我有一个温度预测数据的主题,如下:

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}

但我不知道如何将它与“最新”阅读结合起来。

4

1 回答 1

3

您需要实现一个 UDAF,我们称之为 UDAF,它LATEST保留给定列和键的最新值。这非常简单,您可以了解如何在 KSQL 文档中添加自定义 UDAF:https ://docs.confluent.io/current/ksql/docs/developer-guide/udf.html#udafs

假设您有LATEST可用的 UDAF,您可以编写以下查询:

CREATE TABLE foo AS
SELECT
  date,
  city,
  MIN(temperature) AS minValue,
  MAX(temperature) AS maxValue,
  LATEST(temperature) AS latestValue
FROM forecasts_csv
GROUP BY date, city;
于 2018-10-25T05:35:14.590 回答