kafka_topic(), reads a bounded range of a topic the way the
message viewer does, so you can count, aggregate and join records without
creating a stream or installing anything. The SQL is DuckDB’s,
run in a locked-down, in-memory engine for each query.
Available from Alginte 0.16.0, in the free Community edition. It is on by default, in the
sidebar’s Kafka group, behind the same login as the rest of the console. Reading files is off
until you configure a folder (see Files).
Your first query
In the playground, open SQL Query and run (Ctrl+Enter):orders topic holds Avro records, so value_format names Avro. The columns come from
the registered orders-value schema, and each answer shows its columns’ types in the header.
To start from a topic you are already reading, use the round Query with SQL button in the
message viewer’s controls. It opens a new tab in SQL Query with a starter query over that
topic, written with the deserializers the viewer shows it with. Pick the right value
deserializer in the viewer first, so the starter names it too.
Reading a topic
kafka_topic('<topic>', ...) is a table function. It goes in FROM, where a table name would,
and returns rows. Each call reads one range of one topic.
Only one of
last, from_offset, after and before may be given. A range larger than
alginte.sql.max-records is refused, not cut.
An answer covers the range read, not the whole topic: a count(*) or a sum() over the default
range counts the newest 1,000 records. When the range does not cover the topic, a note above the
result says so, with the offsets the topic spans and how to read it all, for example
kafka_topic('orders') read 1,000 records, not the whole topic: it spans 25,400 offsets on 3 partitions … followed by To read it all: from_offset := 0, max := 25400. A topic larger than
alginte.sql.max-records takes more than one call, and the note says that instead. A compacted
topic read from its start to its end is covered, though it holds fewer records than offsets.
The columns:
_partition,_offset,_timestamp(UTC),_key, and_value, the whole value as JSON text. When the key or value is one number, as a Streams count or sum is, the format you name types it: withLong,IntegerorShortthe column is BIGINT, withDoubleorFloatDOUBLE. SoORDER BY _value DESCputs 100 above 9, andsum(_value)needs no cast;- one column per top-level field of the value. For Avro, Protobuf and JSON Schema the columns and
their types come from the latest schema under
<topic>-value, soDESCRIBEanswers without reading a record. For other formats they are sampled from the records read.
DESCRIBE SELECT * FROM kafka_topic(...) to see the columns, or SUMMARIZE in its place to
profile them.
A wrong value_format is the usual mistake. A record the formats cannot read is left out, and a
note above the result names its partition and offset, the deserializer’s reason, and the format
to read it with instead. The other records still answer. The message viewer shows such a record
as an error instead.
Nested values
Only the top level becomes columns. A field holding an object or an array is a column of JSON text, and_value holds the whole value, so DuckDB’s JSON functions reach anything inside. Avro
and Protobuf nested records come out the same way. For a value such as:
->> answers text and -> keeps JSON, so cast a number you want to compute with. To get one row
per element of an array, unnest it in FROM:
Joining two topics
A query can read several topics. In the playground,customer-tiers holds a tier per customer:
LEFT JOIN finds
them:
QUALIFY row_number() OVER (PARTITION BY _key ORDER BY _offset DESC) = 1.
Joining a topic with a file
Kafka is the subject of every query, and a file can only add to it: a price list, a mapping, the data that lives in a spreadsheet. The console reads files from one folder, the one you set inalginte.sql.allowed-directory, and from nowhere else.
The folder must be mounted read-only. DuckDB can write where it reads, so when the folder is
writable the console turns SQL off and says why.
files/ folder this way, with products.csv, a price list for the
seeded orders:
read_csv, read_parquet and read_json read a file by its path under the folder.
Querying a state store
kafka_store('<app>', '<store>') reads a state store of a Kafka Streams app this console runs:
what its counts, aggregates and tables hold now, through Kafka’s interactive queries. <app> is
the stream’s application id.
_partition, _key, _window_start and _window_end (UTC) for a windowed or
session store, _value, then one per top-level field when the values are objects. _value is
typed when every value is one kind of number, text or boolean, so a count sums without a cast.
- From a stream’s page. On a stream’s State tab, each store’s name links to SQL Query with this query written for it.
- Recent windows only. A windowed store keeps the windows within its retention (the window size plus its grace, unless the app sets more), so older windows are not there to read.
- This instance’s partitions. When the same application id also runs elsewhere, this console holds only some of the store’s partitions. The answer then carries a note that it is partial, with how many partitions it read.
An app deployed elsewhere
kafka_store() reads only the apps this console runs. A key-value store of any app can still be
read from its changelog topic, <app>-<store>-changelog: the newest record per key is what the
store holds. value_format is the store’s value serde, Long for a count:
SELECT * FROM (...) WHERE _value IS NOT NULL. A WHERE beside the QUALIFY
runs first, and would answer the value from before the delete.
This works for key-value stores only. A windowed store’s changelog key is the key followed by
binary window bytes, so it does not read as the key.
The editor
- Tabs above the editor hold one query each, with its own result. Click + for a new tab, double-click a tab to rename it. A reload keeps your tabs; a new browser tab starts with one. Query with SQL opens its query in a new tab, so it never writes over yours. Up to 10 tabs.
- Run (Ctrl+Enter) sends the active tab’s text, or only the selected text when there is a selection. One tab can hold several queries, run one at a time.
- Completions offer topic names inside
kafka_topic(', the deserializer names afterkey_formatandvalue_format, the apps and their stores insidekafka_store(', and elsewhere the query’s columns, then DuckDB’s functions with their signatures. - Error marks underline the place DuckDB points at as you type, with its message on hover. Nothing runs: the query is only prepared.
- History lists the last 30 queries you ran, in any tab, newest first. A click puts one back in the active tab. The history stays in this browser.
- Help above the editor is a cheat sheet: the functions, their parameters and columns, and the DuckDB features most useful for topic data.
The result
The answer shows each column’s DuckDB type in its header, the row count, and the time taken.- Click a column’s header to sort the rows on the page.
ORDER BYsorts the whole answer. - A cell holding a JSON object or array shows one line. Click it to open the value formatted.
- Export copies the rows to the clipboard or downloads them, as CSV or JSON.
- An answer longer than
alginte.sql.max-rowsis cut, and marked cut at N rows. An export of it is namedsql-result-cut-at-<N>-rows. - A note above the table says when a function left something out: records that could not be read, or a store read in part.
Limits and safety
Each query runs in a fresh in-memory DuckDB, with these switched off: extensions (INSTALL,
LOAD), URLs and other network access, other databases (ATTACH), writing files
(COPY ... TO), files outside the allowed folder, and changing these settings from a query.
The limits are properties, listed under Configuration: the
query timeout (30 s), DuckDB’s memory per query (256 MB), the records one call reads (100,000)
and the rows one answer returns (1,000).
Spilling to disk
With the defaults, a query writes nothing anywhere: not in the allowed folder, not in a temp folder. A query that needs more memory thanalginte.sql.memory-limit fails with DuckDB’s
out-of-memory error, and the console stays up. Such a query is usually a GROUP BY, JOIN,
ORDER BY or window function over many distinct keys, which must hold its whole input before
it answers.
When such a query must finish, you can let it spill to disk, as DuckDB does for
larger-than-memory workloads:
- What it does. A query over the memory limit writes its intermediate results to a folder of
its own under
alginte.sql.spill-directory, and the folder is deleted when the query ends. Without a spill directory, the folders go under the system temp folder. - What it costs. DuckDB lets SQL read and write its spill folder, so with spilling on a query can write files into its own folder until it ends or the timeout cancels it. It never reaches another query’s folder, and nothing outside it. Put the spill directory on a disk with room to spare, and keep the timeout short.
- What it does not fix. Spilling is DuckDB’s, with
its limitations:
a query with several blocking operators, or an aggregate such as
string_agg()that cannot move to disk, may still run out of memory. Raisingalginte.sql.memory-limitis the other lever.
/tmp for it, and, with spilling
on, a writable spill directory.
For agents
The same queries are open to AI agents through the console’s MCP server, as the read-onlyrun_sql tool. See MCP server.
Related
Messages
Browse, filter and decode records; Query with SQL starts here.
ksqlDB
Continuous queries on a ksqlDB server.