如何从与KSQL窗口表相对应的kafka主题获取窗口开始或结束时间?

时间:2018-06-26 10:32:00

标签: apache-kafka ksql

我想从kafka-console-consumer命令获取窗口启动时间戳(此处为1530008520000)。

它与KSQL兼容:

ksql> select * from DEV_MONITOR_RULE_2557_104782_233_2_TABLE;

1530008581051 | 2557 : Window{start=1530008520000 end=-} | 2557 | 2 | 1530008581051

但是不适用于kafka-console-consumer吗?

./bin/kafka-console-consumer --zookeeper 10.12.0.157:2181 --topic DEV_MONITOR_RULE_2557_104782_233_2_TABLE

{"HITCOUNTS":2,"TENANTID":2557,"HITTIME":1530008581051}

如何从kafka-console-consumer打印窗口开始时间(此处为1530008520000)?

谢谢!

2 个答案:

答案 0 :(得分:1)

窗口开始时间反映在KSQL消息的ROWTIME和Kafka消息的时间戳中。您可以使用标准API来访问此时间戳,以使用Kafka消息。

AFAIK kafka-console-consumer不支持显示时间戳。但是,类似kafkacat的东西。

以下是KSQL中的一些汇总数据,其中ROWTIME显示了窗口的开始(并且TIMESTAMPTOSTRING用于漂亮地打印窗口):

ksql> SELECT TIMESTAMPTOSTRING(ROWTIME, 'yyyy-MM-dd HH:mm:ss'), ROWTIME, STARS, STAR_COUNT FROM RATINGS_AGG2;
2018-06-27 09:30:00 | 1530091800000 | 1 | 2
2018-06-27 09:30:00 | 1530091800000 | 4 | 6
2018-06-27 09:30:00 | 1530091800000 | 2 | 2
2018-06-27 09:30:00 | 1530091800000 | 3 | 3

现在在kafkacat中使用相同的主题:

$ kafkacat -b localhost:9092 -C -K: \
  -f '\nTimestamp: %T\t\tValue (%S bytes): %s' \
  -t RATINGS_AGG2

Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":1,"STARS":1}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":2,"STARS":1}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":3,"STARS":3}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":5,"STARS":1}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":6,"STARS":3}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":7,"STARS":1}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":7,"STARS":3}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":9,"STARS":1}
Timestamp: 1530091800000                Value (26 bytes): {"STAR_COUNT":9,"STARS":3}

答案 1 :(得分:0)

您是否尝试过kafka-console-consumer选项print.timestamp=true

$ kafka-console-consumer --property print.timestamp=true ...