bin/zookeeper-server-start.sh config/zookeeper.properties 若不想显示日志信息:bin/zookeeper-server-start.sh config/zookeeper.properties > /dev/null 2>&1 & bin/kafka-server-start.sh config/server.properties 若不想显示日志信息:bin/kafka-server-start.sh config/server.properties > /dev/null 2>&1 & bin/kafka-server-stop.sh bin/zookeeper-server-stop.sh 查看Kafka服务的状态可以使用kafka-broker-api-versions.sh:
bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092 重新分配分区通常用于扩展Kafka集群时,将主题的分区重新分配到新的broker上。使用以下步骤进行分区重新分配:
bin/kafka-reassign-partitions.sh --generate --zookeeper localhost:2181 --topics-to-move-json-file topics-to-move.json --broker-list "0,1,2" bin/kafka-reassign-partitions.sh --execute --zookeeper localhost:2181 --reassignment-json-file reassignment.json bin/kafka-reassign-partitions.sh --verify --zookeeper localhost:2181 --reassignment-json-file reassignment.json 你可以使用ZooKeeper的命令行客户端zkCli.sh来查看Kafka在ZooKeeper中的数据:
bin/zookeeper-shell.sh localhost:2181 进入ZooKeeper shell后,可以使用以下命令:
ls / ls /brokers/ids get /brokers/ids/0 --topic:指定主题的名称。--bootstrap-server:Kafka broker的地址。--replication-factor:副本因子,即每个主题分区的副本数。--partitions:分区数。创建主题test-topic:
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --replication-factor 3 --partitions 2 列出所有主题:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092 描述主题test-topic的详细信息:
bin/kafka-topics.sh --describe --topic test-topic --bootstrap-server localhost:9092 删除主题test-topic:
bin/kafka-topics.sh --delete --topic test-topic --bootstrap-server localhost:9092 修改主题test-topic:
bin/kafka-topics.sh --alter --topic test-topic --bootstrap-server your-kafka-bootstrap-server:9092 --config max.message.bytes=10485760 使用kafka-console-producer.sh发送消息到Kafka主题:
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092 启动后可以输入消息并按回车发送。
使用kafka-console-consumer.sh从Kafka主题消费消息:
bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092 参数说明:
--from-beginning:表示从主题的开始位置消费消息。