简体   繁体   中英

ksql: create table and filter today's data based on timestamp

I am quite new to KSQL and just wonder how to extract today's data for aggregation? say I have the stream of count data with schema below:

Field     | Type
-----------------------------
 ROWTIME   | BIGINT
 ROWKEY    | VARCHAR(STRING)
 TS        | BIGINT
 COUNT     | BIGINT

How to create a table which would output the snapshot of todays sum of COUNT where TS is the UNIX timestamp using KSQL?

You can use TIMESTAMPTOSTRING udf to change the TS value to your desired date format and then perform the sum. Here is one way to do this:

CREATE STREAM mystream1 AS SELECT TIMESTAMPTOSTRING(TS, 'yyyy-MM-dd') AS TS, COUNT_VAL FROM mystream;

SELECT TS, sum(COUNT_VAL) FROM mystream1 GROUP BY TS;

You may want to rename COUNT column in your stream since it is function name in KSQL. You can also add WINDOW if you want to perform this over window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM