KSQL
Composite Key
Topic Conversion
Table Creation
Database Management

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:

json
1{
2   "userId": "user1",
3   "productId": "prod2",
4   "quantity": 3,
5   "timestamp": "2022-12-01T12:34:56"
6}

Step 2: Creating the Stream with a Composite Key

Before creating a table, you usually need to create a stream from the topic:

sql
1CREATE STREAM orders_stream (
2    userId STRING,
3    productId STRING,
4    quantity INT,
5    timestamp STRING
6) WITH (
7    KAFKA_TOPIC='orders_topic',
8    VALUE_FORMAT='JSON'
9);

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:

sql
1CREATE STREAM orders_with_key AS
2  SELECT STRUCT(userId AS userId, productId AS productId) AS orderKey,
3         quantity,
4         timestamp 
5  FROM orders_stream
6  PARTITION BY orderKey;

Step 4: Create the Table

With the stream having a composite key structured, you can now define a table based on this stream:

sql
1CREATE TABLE orders_table WITH (
2    KEY_FORMAT='JSON'
3) AS
4  SELECT orderKey,
5         LATEST_BY_OFFSET(quantity) AS latest_quantity,
6         LATEST_BY_OFFSET(timestamp) AS latest_timestamp
7  FROM orders_with_key
8  GROUP BY orderKey;

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

OperationSQL CommandDescription
Create StreamCREATE STREAM orders_stream ...Creates a stream from Kafka topic
Structure KeySELECT STRUCT(...) AS orderKey ...Structures multiple fields into a single composite key
Create Keyed StreamCREATE STREAM orders_with_key ... PARTITION BY orderKeyDerives a new stream with a structured key
Create TableCREATE 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.


Course illustration
Course illustration

All Rights Reserved.