Kafka命令行常用命令说明

##查看topic分布情况kafka-list-topic.sh

bin/kafka-list-topic.sh --zookeeper 192.168.197.170:2181,192.168.197.171:2181  (列出所有topic的分区情况)
bin/kafka-list-topic.sh --zookeeper 192.168.197.170:2181,192.168.197.171:2181 --topic test (查看test的分区情况)

##创建TOPIC kafa-create-topic.sh

bin/kafka-create-topic.sh   --replica 2 --partition 8 --topic test  --zookeeper 192.168.197.170:2181,192.168.197.171:2181

创建名为test的topic, 8个分区分别存放数据,数据备份总共2份

bin/kafka-create-topic.sh    --replica 1 --partition 1 --topic test2    --zookeeper  192.168.197.170:2181,192.168.197.171:2181
结果 topic: test2 partition: 0 leader: 170 replicas: 170 isr: 170

##重新分配分区kafka-reassign-partitions.sh

这个命令可以分区指定到想要的--broker-list上
bin/kafka-reassign-partitions.sh --topics-to-move-json-file topics-to-move.json --broker-list "171" --zookeeper 192.168.197.170:2181,192.168.197.171:2181 --execute 
cat topic-to-move.json
{"topics":
     [{"topic": "test2"}],
     "version":1
}

##为Topic增加 partition数kafka-add-partitions.sh

bin/kafka-add-partitions.sh --topic test --partition 2  --zookeeper  192.168.197.170:2181,192.168.197.171:2181 (为topic test增加2个分区)

##控制台接收消息

bin/kafka-console-consumer.sh --zookeeper  192.168.197.170:2181,192.168.197.171:2181  --from-beginning --topic test

##控制台发送消息

bin/kafka-console-producer.sh --broker-list  192.168.197.170:9092,192.168.197.171: 9092    --topic test 

##手动均衡topic, kafka-preferred-replica-election.sh

bin/kafka-preferred-replica-election.sh --zookeeper 192.168.197.170:2181,192.168.197.171:2181 --path-to-json-file preferred-click.json

cat preferred-click.json
{
 "partitions":
  [
    {"topic": "click", "partition": 0},
    {"topic": "click", "partition": 1},
    {"topic": "click", "partition": 2},
    {"topic": "click", "partition": 3},
    {"topic": "click", "partition": 4},
    {"topic": "click", "partition": 5},
    {"topic": "click", "partition": 6},
    {"topic": "click", "partition": 7},

    {"topic": "play", "partition": 0},
    {"topic": "play", "partition": 1},
    {"topic": "play", "partition": 2},
    {"topic": "play", "partition": 3},
    {"topic": "play", "partition": 4},
    {"topic": "play", "partition": 5},
    {"topic": "play", "partition": 6},
    {"topic": "play", "partition": 7}


  ]
}

 

##删除topic,慎用,只会删除zookeeper中的元数据,消息文件须手动删除

bin/kafka-run-class.sh kafka.admin.DeleteTopicCommand --topic test666 --zookeeper 192.168.197.170:2181 ,192.168.197.171:2181
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值