Producer console
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic --partition 0
Consumer console
./bin/kafka-console-consumer.sh --broker-list localhost:9092 --topic testTopic --partition 0
Describe a topic
./bin/kafka-topic.sh --describe --zookeeper localhost:2181 --topic <topicName>
Broker Version
./bin/kafka-broker-api-versions --bootstrap-server localhost:9092 --version
Create a topic
/bin/kafka-topics.sh --zookeeper $ZK_HOSTS --create --topic $TOPIC_NAME --partitions 3 --replication-factor 3
Create a topic if it doesn't exist/bin/kafka-topics.sh --zookeeper $ZK_HOSTS --create --topic $TOPIC_NAME --partitions 3 --replication-factor 3 --if-not-exists
List brokers
./usr/bin/kafka-topics --zookeeper zookeeper:2181 --list
Show Kafka topic details
./usr/bin/kafka-topics --zookeeper zookeeper:2181 --topic my-first-topic --describe
Increase partitions in the topic
./usr/bin/kafka-topics --zookeeper zookeeper:2181 --alter --topic my-first-topic --partitions 5
Get partition offset of a topic
./bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic $TOPIC_NAME
Format is: topicname:partition-id:offset
Show offset of a specific partition
/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic $TOPIC_NAME --partitions partition-id, another-partition-id
Delete a topic
/bin/kafka-topics.sh — zookeeper $ZK_HOSTS --delete --topic $TOPIC_NAME
Produce messages from a file
/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-first-topic < topic-input.txt
Produce message in Kafka with key and value
/usr/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic $TOPIC_NAME --property parse.key=true --property key.separator=:
Example:
>key:value
>foo:bar
>anotherKey:another value
By default messages sent to a Kafka topic will result in messages with null keys. In this example, the separator between the key and the value is: :
Consume message from Kafka with key-value and timestamp
/bin/kafka-console-consumer --bootstrap-server localhost:9092 --topic some-topic --formatter kafka.tools.DefaultMessageFormatter --property print.timestamp=true --property print.key=true --property print.value=true
Consume message from the beginning
/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-first-topic --from-beginning
Comments
Post a Comment