CREATE INDEX (Apache Kafka)
A Kafka index is a custom index that uses the KafkaReplicator class to replicate row mutations from a HCD table to a Apache Kafka® topic in real time.
Unlike SAI and 2i indexes, Kafka indexes don’t store index data with the table’s SSTables, and you can’t query the table through them. Instead, the index intercepts row mutations at the coordinator and publishes them to your Kafka cluster, where downstream consumers read them.
KafkaReplicator captures all write types, including standard writes, BATCH writes, and lightweight transactions (LWT).
For more information, see Linearizable consistency.
|
Before you create a Kafka index, you must enable the Kafka integration and configure the connection to your Kafka cluster on each node that you want to replicate from. Kafka indexes require HCD 2.0.8 or later. For prerequisites and setup instructions, see Integrate Apache Kafka® with Hyper-Converged Database (HCD). |
Create a Kafka index
To create a Kafka index, define the table name, the optional columns to replicate, and the KafkaReplicator index class.
If the table already contains data, the existing data is replicated to the Kafka topic during the execution of this statement, unless you set skipBuildIfTableNotEmpty to true.
After the index has been created, HCD publishes every subsequent change to the table.
|
This index type cannot be altered. If you need to modify an index, you must drop the index, and then create a new index. |
CREATE CUSTOM INDEX [ IF NOT EXISTS ] <index_name>
ON [ <keyspace_name>.]<table_name> ( [ <column_name> [, <column_name> ... ] ] )
USING 'KafkaReplicator'
[ WITH OPTIONS = { <option_map> } ] ;
| Syntax conventions | Description |
|---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
|
Set, list, map, or tuple.
Angle brackets ( |
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
Options
- IF NOT EXISTS
-
Allows silent skip by checking for existing indexes with the same name. If a match is found, the operation silently does nothing (no error is thrown). If no match is found, the operation creates the index.
- index_name
-
Unique identifier for the index.
Enclose in quotes to preserve capitalization.
Must be unique across all indexes in the keyspace. Although indexes apply to a specific table, they are created at the keyspace level.
- keyspace_name
-
Optional. The name of the keyspace that contains the table to replicate. If no name is specified, the current keyspace is used.
- table_name
-
The name of the table to replicate.
You can’t create a Kafka index on a table that has static columns or counter columns. For all restrictions, see Tables that cannot be indexed.
- column_name
-
Optional. The non-primary-key columns to include in the message value. Leave the parentheses empty to include every non-primary-key column.
Primary key columns are always included in the message key.
You can also select columns with the
columnsToIndexoption. - WITH OPTIONS = { <option_map> }
-
The
<option_map>is a map of options and their values defined in JSON simple format. Pass all values as strings.Options aren’t required. For the supported options, see Index options.
Index options
The following options are specific to Kafka indexes. For the complete reference, see Index options.
| Option | Default | Description |
|---|---|---|
|
|
The Kafka topic to publish messages to. Must be non-blank if supplied. |
|
|
When |
|
|
Number of partitions when HCD creates the topic.
Also the default for |
|
|
Replication factor when HCD creates the topic.
Also the default for |
|
|
Serialization format for Kafka messages.
Accepted values: |
|
All non-primary-key columns |
Comma-separated list of non-primary-key columns to include in the message value. |
|
|
When |
|
|
Name of the intermediate events topic.
Used only when |
|
Value of |
Number of partitions for the events topic when HCD creates it. |
|
Value of |
Replication factor for the events topic when HCD creates it. |
|
|
Kafka consumer group ID shared by all deduplication workers for this table across the cluster. |
|
|
When |
|
|
When |
Replicated messages
HCD publishes one message for each row mutation.
The message key contains the primary key columns, and the message value contains the replicated columns.
A delete publishes a message with a null value (a tombstone).
For example, for a table with a primary key column id and the columns name and price:
-- INSERT: key: {"id":42} value: {"name":"Widget","price":9.99}
INSERT INTO ks.products (id, name, price) VALUES (42, 'Widget', 9.99);
-- DELETE: key: {"id":42} value: null (tombstone)
DELETE FROM ks.products WHERE id = 42;
For the complete message format, including Avro schema mapping, see Message format.
Kafka index examples
The examples use the following table. Each example is an alternative configuration for the table, so don’t run them in sequence.
CREATE TABLE ks.products (
id int PRIMARY KEY,
name text,
price float
);
- Basic index
-
Create an index with an explicit topic name. This example assumes that you enabled the Kafka integration and configured the broker connection.
CREATE CUSTOM INDEX products_kafka ON ks.products (name, price) USING 'KafkaReplicator' WITH OPTIONS = {'topicName': 'ks.products'}; - Create the topic automatically
-
Create the Kafka topic at index creation time, with custom partition and replication settings.
CREATE CUSTOM INDEX products_kafka ON ks.products (name, price) USING 'KafkaReplicator' WITH OPTIONS = { 'topicName': 'ks.products', 'createTopicIfNotExists': 'true', 'partitions': '6', 'replicationFactor': '3' }; - Avro format
-
Use Avro serialization to derive schemas and register them in the Confluent Schema Registry. This example requires the
cassandra.kafka.schema.registry.urlsystem property. For more information, see Configuration.CREATE CUSTOM INDEX products_avro ON ks.products (name, price) USING 'KafkaReplicator' WITH OPTIONS = { 'topicName': 'ks.products', 'format': 'avro' }; - Replicate selected columns
-
List only the columns that you want in the message value to reduce message size.
CREATE CUSTOM INDEX products_kafka ON ks.products (price) USING 'KafkaReplicator' WITH OPTIONS = {'topicName': 'ks.products'}; - Deduplication
-
Publish only the latest version of each row to the destination topic. HCD creates the destination topic and the events topic if they don’t exist.
CREATE CUSTOM INDEX products_dedup ON ks.products (name, price) USING 'KafkaReplicator' WITH OPTIONS = { 'topicName': 'ks.products', 'deduplicationEnabled': 'true', 'eventsTopic': 'ks.products-events', 'eventsTopicPartitions': '6', 'eventsConsumerGroupId': 'hcd-dedup-ks-products', 'createTopicIfNotExists': 'true' }; - Skip the initial backfill
-
Replicate only writes made after
CREATE INDEX, and leave existing rows out of Kafka until you run a separate backfill.CREATE CUSTOM INDEX products_kafka ON ks.products (name, price) USING 'KafkaReplicator' WITH OPTIONS = { 'topicName': 'ks.products', 'skipBuildIfTableNotEmpty': 'true' };