Tags: kafka
kcat (formerly known as kafkacat) is a versatile command-line tool for Apache Kafka. It allows you to consume and produce Kafka messages and interact with Kafka clusters efficiently. This guide will help you set up and use kcat with SASL authentication to consume and produce messages.
Ensure that kcat is installed on your system. If not, you can find installation instructions in the kcat GitHub repository.
You'll need the following SASL authentication details:
To consume messages from a Kafka topic using kcat with SASL authentication, use the following command:
$ kcat -C -b broker1:9092,broker2:9092 -t <topic> \
-X security.protocol=sasl_ssl \
-X sasl.mechanisms=SCRAM-SHA-512 \
-X sasl.username=<username> \
-X sasl.password=<password> \
-X ssl.ca.location=<path_to_ca_cert> \
-o -1 -e -J
Options Explained:
When consuming messages with the above command, the output will be in JSON format, including metadata such as offset, partition, and timestamp.
{
"topic": "sample-topic",
"partition": 0,
"offset": 12345,
"tstype": "create",
"ts": 1626894728000,
"key": null,
"payload": "{"eventType":"EVENT_TYPE","source":"SOURCE_SYSTEM"}"
}
$ kcat -C -b broker1:9092,broker2:9092 -t your-topic \
-X security.protocol=sasl_ssl \
-X sasl.mechanisms=SCRAM-SHA-512 \
-X sasl.username=your-username \
-X sasl.password=your-password \
-X ssl.ca.location=/path/to/ca.pem \
-o beginning -e -J
$ kcat -C -b broker1:9092,broker2:9092 -t your-topic \
-X security.protocol=sasl_ssl \
-X sasl.mechanisms=SCRAM-SHA-512 \
-X sasl.username=your-username \
-X sasl.password=your-password \
-X ssl.ca.location=/path/to/ca.pem \
-o end -e -J
To publish messages to a Kafka topic, use the following command:
echo '{"key":"value", "anotherKey":123}' | kcat -P -b broker1:9092,broker2:9092 -t <topic> \
-X security.protocol=sasl_ssl \
-X sasl.mechanisms=SCRAM-SHA-512 \
-X sasl.username=your-username \
-X sasl.password=your-password \
-X ssl.ca.location=/path/to/ca.pem
For larger or more complex JSON payloads, consider using a file or another program to generate the JSON and pipe it into kcat.
{
"key": "value",
"anotherKey": 123
}
$ kcat -P -b broker1:9092,broker2:9092 -t your-topic \
-X security.protocol=sasl_ssl \
-X sasl.mechanisms=SCRAM-SHA-512 \
-X sasl.username=your-username \
-X sasl.password=your-password \
-X ssl.ca.location=/path/to/ca.pem \
-l message.json
SASL (Simple Authentication and Security Layer) is a framework for authentication and data security in internet protocols. In the context of Kafka, SASL provides a way to authenticate clients to Kafka brokers.
Kafka supports multiple SASL mechanisms, including:
How Kafka Uses SASL Internally:
Using `kcat` with SASL authentication enables secure communication with Kafka clusters. By understanding and utilizing the different options and scenarios, you can efficiently consume and produce messages while ensuring the security of your Kafka interactions. Whether you're dealing with simple messages or complex JSON payloads, `kcat` provides a robust toolset for working with Kafka.