How can I create a KSQL table from a topic using a composite key?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Creating a KSQL table from a Kafka topic using a composite key involves several steps, mainly due to the need to define how multiple fields combine to form a unique key for table records. KSQL, a stream processing query engine built on top of Apache Kafka, simplifies the processing of Kafka streams but managing keys for proper table creation requires specific syntax and considerations. This write-up guides you through creating a KSQL table from a Kafka topic with a composite key.
Understanding KSQL Tables and Composite Keys
KSQL tables represent a view of your data as a series of updates, where each update is reflected as a row in the table. In Kafka, every message in a topic is a key-value pair. When you create a table in KSQL, rows in the table are uniquely identified by the message key.
A composite key involves combining two or more fields to form a unique key. Composite keys are particularly useful in scenarios where a single field isn't enough to uniquely identify a record.
Step 1: Ensure Your Topic has the Correct Data Format
KSQL supports various data formats like AVRO, JSON, or Protobuf. Ensure the topic's data is in a format that KSQL can interpret, and it includes the fields you intend to use as a composite key. For JSON, which we'll use in this example, you should have messages structured as:
Step 2: Creating the Stream with a Composite Key
Before creating a table, you usually need to create a stream from the topic:
Step 3: Derive a New Stream with a Structured Key
Since the composite key involves multiple values, you need to create an intermediate stream where you structure these values into a single key:
Step 4: Create the Table
With the stream having a composite key structured, you can now define a table based on this stream:
This SQL statement creates a table where each unique orderKey (combined userId and productId) is linked to the latest quantity and timestamp values.
Summary Table of Key Commands
| Operation | SQL Command | Description |
| Create Stream | CREATE STREAM orders_stream ... | Creates a stream from Kafka topic |
| Structure Key | SELECT STRUCT(...) AS orderKey ... | Structures multiple fields into a single composite key |
| Create Keyed Stream | CREATE STREAM orders_with_key ... PARTITION BY orderKey | Derives a new stream with a structured key |
| Create Table | CREATE TABLE orders_table ... | Defines a table with unique rows based on composite key |
Considerations and Best Practices
- Performance: Structuring keys and constantly updating a table can lead to higher computation overhead. Monitor system performance and consider streamlining keys or adjusting query strategies as needed.
- Data Evolution: If the structure of your input data changes, such as additional fields in the composite key, you'll need to update your KSQL queries and potentially redefine tables and streams.
Creating KSQL tables with composite keys offers powerful flexibility for handling complex data relationships, providing deeper insights into distinct sets of data elements within your Kafka streams.

