Click to copy
How to Create a Table from Existing Topic in ksqlDB
Example of creating a table from an existing topic:
CREATE SOURCE TABLE scores_table (
student_id BIGINT PRIMARY KEY,
name STRING,
score DOUBLE
) WITH (
KAFKA_TOPIC = 'scores_topic',
VALUE_FORMAT = 'JSON'
);
Note theSOURCE
modifier inCREATE SOURCE TABLE
.
This statement creates a materialized table that you can run pull queries on. This is likely to be the preferred way to create tables that need to have a fast query response, especially if the underlying topic is not compacted.
A table created with the statement above is read-only, which means you can’t run INSERT
statements on it. Insert statements are used to insert, update and delete rows (using tombstones) from a table.
CREATE TABLE scores_table (
student_id BIGINT PRIMARY KEY,
name STRING,
score DOUBLE
) WITH (
KAFKA_TOPIC = 'scores_topic',
VALUE_FORMAT = 'JSON'
);
This statement will create a non-materialized table, no data will be pulled or cached from the topic when the table is created. A non-materialized table is not queryable. In ksqlDB, you can only query materialized tables.