
Kafka 主题(Topic)是 Kafka 中用于组织和存储消息的逻辑单元。在 Kafka 中,生产者(Producer)将消息发布到主题,消费者(Consumer)从主题中订阅消息。
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic my_topic --partitions 3 --replication-factor 2
bootstrap-server:指定 Kafka 集群的地址和端口。create:表示创建主题的操作。topic:指定要创建的主题名称。partitions:指定主题的分区数。replication-factor:指定每个分区的副本数。
kafka-topics.sh --bootstrap-server localhost:9092 --list
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic my_topic
kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic my_topic
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name my_topic --alter --add-config max.message.bytes=1048576
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic my_topic --partitions 5
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name my_topic --describe
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list --all-groups --topic my_topic
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic my_topic

bootstrap-server:指定 Kafka 集群的地址和端口。topic:指定要发送消息的目标主题。 kafka-console-producer.sh --bootstrap-server localhost:9092 --topic my_topic --property "parse.key=true" --property "key.separator=:"
property "parse.key=true":表示消息包含键。property "key.separator=:":指定键值对中键和值的分隔符。 kafka-console-producer.sh --bootstrap-server localhost:9092 --topic my_topic < messages.txt
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic my_topic --property "partitioner.class=my.custom.Partitioner"
property "partitioner.class=my.custom.Partitioner":指定自定义的分区器类。10.设置消息发送速率(Setting Message Sending Rate):
kafka-producer-perf-test.sh --topic my_topic --throughput 100 --num-records 1000000 --record-size 100 --producer-props bootstrap.servers=localhost:9092
throughput:指定消息发送速率(消息/秒)。num-records:指定要发送的消息总数。record-size:指定每条消息的大小。示例:启动一个交互式的控制台消费者,从名为 my_topic 的主题消费消息,并从起始处开始。
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --from-beginning
bootstrap-server:指定 Kafka 集群的地址和端口。topic:指定要消费消息的目标主题。from-beginning:从主题的起始处开始消费消息。 kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --group my_group
group:指定消费者组的名称。 kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --partition 0
partition:指定要消费消息的分区。示例:启动一个交互式的控制台消费者,从名为 my_topic 的主题的偏移量 1234 开始消费消息。
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --offset 1234
offset:指定要消费消息的偏移量。
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --from-beginning > output.txt