Click to copy

• Reviewed for ksqlDB 0.29

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 the SOURCE modifier in CREATE 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.

Mutable tables

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.

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