kafka基本操作

Posted by Clear Blog on May 20, 2018

single partition and only one replica

create topic with a single partition and only one replica

1
	bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

list topic

1
	bin/kafka-topics.sh --list --zookeeper localhost:2181

send message

1
	bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

dump out messages

1
	bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

cluster

启动zk

1
zkServer.sh start

创建broker

1
2
3
nohup bin/kafka-server-start.sh config/server1.properties > logs/kafka1.log 2>&1 &
nohup bin/kafka-server-start.sh config/server2.properties > logs/kafka2.log 2>&1 &
nohup bin/kafka-server-start.sh config/server.properties > logs/kafka.log 2>&1 &

topic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
* 创建topic
bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --topic dummyTopic --partitions 1 --replication-factor 1
bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --topic mytopic --partitions 3 --replication-factor 3 --if-not-exists

* 增加分区,无法减少分区,会导致数据不一致,消息出现乱序
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --alter --topic mytopic --partitions 5

* 删除topic
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --delete --topic mytopic

* 列出topic
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --list

* topic详情
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --describe


生产消费message

1
2
bin/kafka-console-producer.sh --topic mytopic --broker-list 127.0.0.1:9093
bin/kafka-console-consumer.sh --topic mytopic --zookeeper 127.0.0.1:2181  --from-beginning