Click to copy

• Reviewed for ksqlDB 0.29

How to Count Messages of Stream in ksqlDB

On ksqlDB version 0.27 or above

To count the number of records, you can execute a COUNT(*) aggregation directly on any stream or table:

SELECT COUNT(*)
FROM stream_name;

On ksqlDB version 0.26 or below

Up to ksqlDB 0.26, COUNT is an aggregation function that can only run in conjunction with a GROUP BY clause.

Therefore to run a count, we need a field to aggregate on that remains the same across the topic to avoid having a split count — the trick here is to use a literal value, like a true boolean.

SELECT true, COUNT(*)
FROM users
GROUP BY true
EMIT CHANGES;

Note that to compute SELECT COUNT(*) statement, ksqlDB needs to run a full scan on the underlying topic.

Runtime statistics - the quick way

If all you need is to know how many messages there are in a stream, you can look at the runtime statistics of the stream.

To get the statistic about the total messages in a stream, execute a DESCRIBE EXTENDED statement on the stream

DESCRIBE stream_name EXTENDED;

which returns

...
Runtime statistics by host
-------------------------
 Host             | Metric           | Value | Last Message
------------------------------------------------------------------------
 ksql-server:8088 | messages-per-sec |     0 | 2022-07-18T18:26:59.753Z
 ksql-server:8088 | total-messages   |  1374 | 2022-07-18T18:26:59.753Z
------------------------------------------------------------------------
...

Look at the number right next to total-messages under the column Value to find the number of messages in that stream.

Discover what readers are saying
topictale
Get easy to digest how-tos on ksqlDB
Sign up
Please read our Privacy Policy to understand how we protect and manage your data.
You may also like