Creating & deleting Kafka topics
This page describes how to create and delete topics in the Lenses SQL Studio.
Lenses supports the typical SQL commands supported by a relational database:
CREATE
DROP
TRUNCATE
DELETE
SHOW TABLES
DESCRIBE TABLE
DESCRIBE FORMATTED
CREATE TABLE
The CREATE
statement has the following parts:
CREATE TABLE - Instructs the construction of a table
$Table - The actual name given to the table.
Schema - Constructed as a list of (field, type) tuple, it describes the data each record in the table contains
FORMAT - Defines the storage format. Since it is an Apache Kafka topic, both the Key and the Value formats are required. Valid values are STRING, INT, LONG, JSON, AVRO.
PROPERTIES - Specifies the number of partitions the final Kafka topic should have, the replication factor in order to ensure high availability (it cannot be a number higher than the current Kafka Brokers number) and if the topic should be compacted.
A Kafka topic which is compacted is a special type of topic with a finer-grained retention mechanism that retains the last update record for each key.
A compacted topic (once the compaction has been completed) contains a full snapshot of the final record values for every record key and not just the recently changed keys. They are useful for in-memory services, persistent data stores, reloading caches, etc.
For more details on the subject, you should look at Kafka Documentation.
Example:
Best practices dictate to use Avro as a storage format over other formats. In this case, the key can still be stored as STRING but the value can be Avro.
SHOW TABLES
To list all tables:
DESCRIBE TABLE
To examine the schema an metadata for a topic:
The $tableName
should contain the name of the table to describe.
Given the two tables created earlier, a user can run the following SQL to get the information on each table:
the following information will be displayed:
DROP TABLE
To drop a table:
Dropping a table results in the underlying Kafka topics being removed.
System virtual tables (__tables, __fields)
Lenses provides a set of virtual tables that contain information about all the fields in all the tables.
Using the virtual table, you can quickly search for a table name but also see the table type.
The __table
has a table_name
column containing the table name, and a table_type
column describing the table type (system, user, etc).
To see all the tables fields select from the _fields
virtual table.
Record metadata
Each Kafka message contains information related to partition, offset, timestamp, and topic. Additionally, the engine adds the key and value raw byte size.
Create a topic and insert a few entries.
Now we can query for specific metadata related to the records.
To query for metadata such as the underlying Kafka topic offset, partition and timestamp prefix your desired fields with _meta
.
Run the following query to see each tutorial name along with its metadata information:
Last updated