Kafka安装配置、常用命令

时间:2023-02-25 07:30:19
下载安装:
tar zxf kafka-<VERSION>.tgz
cd kafka-<VERSION>

启动zookeeper:(略)

修改配置文件:server.properties

config/server-0.properties:
    broker.id=0
    port=9092
    log.dir=/opt/kafka/kafka-logs-0

config/server-1.properties:
    broker.id=1
    port=9093
    log.dir=/opt/kafka/kafka-logs-1
 
config/server-2.properties:
    broker.id=2
    port=9094
    log.dir=/opt/kafka/kafka-logs-2

其他常用的通用配置:
log.retention.hours=24
log.retention.bytes=1073741824
zookeeper.connect=192.168.0.9:2181,192.168.0.9:2182,192.168.0.9:2183
delete.topic.enable=true

常用命令:
启动:
bin/kafka-server-start.sh -daemon config/server-0.properties
bin/kafka-server-start.sh -daemon config/server-1.properties  (如果有多个server/broker)
bin/kafka-server-start.sh -daemon config/server-2.properties   (如果有多个server/broker)

停止:
bin/kafka-server-stop.sh

查看已经创建的topic列表:
bin/kafka-topics.sh --list --zookeeper 192.168.0.9:2181

创建topic:
  • bin/kafka-topics.sh --create --zookeeper 192.168.0.9:2181 --replication-factor 1 --partitions 1 --topic test
查看topic明细:
  • bin/kafka-topics.sh --describe --zookeeper 192.168.0.9:2181 --topic test
修改topic参数:
  • bin/kafka-topics.sh --zookeeper 192.168.0.9:2181 --partition 4 --topic test --alter
删除topic:
  • bin/kafka-topics.sh --zookeeper 192.168.0.9:2181 --delete --topic test
永久删除topic:
  • 删除kafka存储目录(server.properties文件log.dirs配置,默认为"/tmp/kafka-logs")相关topic目录 
  • 删除zookeeper "/brokers/topics/"目录下相关topic节点
    • cd /usr/lib/zookeeper,或 cd <zookeeper安装目录>
    • bin/zkCli.sh -server 192.168.0.9
    • ls /brokers/topics/test
    • rmr /brokers/topics/test
    • rmr /admin/delete_topics/test

启动producer脚本:
  • bin/kafka-console-producer.sh --broker-list 192.168.0.31:9092 --topic test
启动consumer脚本:
  • bin/kafka-console-consumer.sh --zookeeper 192.168.0.9:2181 --topic test --from-beginning
退出:ctrl+C

附:

几张有用的图:

Kafka安装配置、常用命令

Kafka安装配置、常用命令

Kafka安装配置、常用命令