kafka总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kafka总结相关的知识,希望对你有一定的参考价值。
1、启动kafka
sh kafka-server-start.sh ../config/server.properties &
2、zookeeper上查看节点信息
sh zkCli.sh -server 127.0.0.1:2181 ls /brokers/ids
3、创建topic(2个分区,3个副本)
./kafka-topics.sh --create --topic topic1 --replication-factor 3 --partitions 2 --zookeeper localhost:2181
4、查看topic list
./kafka-topics.sh --list --zookeeper localhost:2181
5、zookeeper上查看topic list信息
ls /brokers/topics
6、zookeeper上查看节点具体信息
[zk: 127.0.0.1:2181(CONNECTED) 9] get /brokers/topics/topic1 {"version":1,"partitions":{"1":[1,2,3],"0":[3,1,2]}} cZxid = 0x200000179 ctime = Wed Mar 30 23:24:25 EDT 2016 mZxid = 0x200000179 mtime = Wed Mar 30 23:24:25 EDT 2016 pZxid = 0x20000017d cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 52 numChildren = 1
7、kafka目录中查看节点信息
cd /tmp/kafka-logs-1 #kafka server.config中配置的目录,topic1-0 和topic1-1分别记录topic1的2个分区的日志数据。查看另外2个kafka日志目录,也会存在这样的2个文件夹,对应的是topic创建的3个副本 drwxr-xr-x 2 root root 4096 Mar 30 23:24 topic1-0 drwxr-xr-x 2 root root 4096 Mar 30 23:24 topic1-1
8、创建消费者
./kafka-console-consumer.sh --zookeeper localhost:2181 --topic topic1 --from-beginning
9、zookeeper中查看对应的节点信息
#自动创建group_id [zk: 127.0.0.1:2181(CONNECTED) 18] ls /consumers [console-consumer-75348] #对应三个节点 [zk: 127.0.0.1:2181(CONNECTED) 19] ls /consumers/console-consumer-75348 [offsets, owners, ids] #查看刚刚创建的消费者ID:consumerId,用来标记一个消费者 [zk: 127.0.0.1:2181(CONNECTED) 15] ls /consumers/console-consumer-75348/ids [console-consumer-75348_e2ef967443a3-1459395856932-a0028f17] [zk: 127.0.0.1:2181(CONNECTED) 17] get /consumers/console-consumer-75348/ids/console-consumer-75348_e2ef967443a3-1459395856932-a0028f17 {"version":1,"subscription":{"topic1":1},"pattern":"white_list","timestamp":"1459395857222"} cZxid = 0x200000190 ctime = Wed Mar 30 23:44:17 EDT 2016 mZxid = 0x200000190 mtime = Wed Mar 30 23:44:17 EDT 2016 pZxid = 0x200000190 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x153413bc26e002d dataLength = 92 numChildren = 0 [zk: 127.0.0.1:2181(CONNECTED) 20] ls /consumers/console-consumer-75348/owners [topic1] [zk: 127.0.0.1:2181(CONNECTED) 21] ls /consumers/console-consumer-75348/owners/topic1 [1, 0] #topic1的第一个分区正被xxx消费 [zk: 127.0.0.1:2181(CONNECTED) 23] get /consumers/console-consumer-75348/owners/topic1/1 console-consumer-75348_e2ef967443a3-1459395856932-a0028f17-0 cZxid = 0x200000195 ctime = Wed Mar 30 23:44:18 EDT 2016 mZxid = 0x200000195 mtime = Wed Mar 30 23:44:18 EDT 2016 pZxid = 0x200000195 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x153413bc26e002d dataLength = 60 numChildren = 0 [zk: 127.0.0.1:2181(CONNECTED) 24] get /consumers/console-consumer-75348/owners/topic1/0 console-consumer-75348_e2ef967443a3-1459395856932-a0028f17-0 cZxid = 0x200000196 ctime = Wed Mar 30 23:44:18 EDT 2016 mZxid = 0x200000196 mtime = Wed Mar 30 23:44:18 EDT 2016 pZxid = 0x200000196 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x153413bc26e002d dataLength = 60 numChildren = 0 [zk: 127.0.0.1:2181(CONNECTED) 26] ls /consumers/console-consumer-75348/offsets [topic1] [zk: 127.0.0.1:2181(CONNECTED) 27] ls /consumers/console-consumer-75348/offsets/topic1 [1, 0] #topic1的1分区被消费的offset位置。和consumer_id无关,和group_id关联 #就是说,每个分区的信息可以被不同的group_id消费,但是同一时间只能由一个group的其中一个consumer消费 [zk: 127.0.0.1:2181(CONNECTED) 29] get /consumers/console-consumer-75348/offsets/topic1/1 0 cZxid = 0x20000019e ctime = Wed Mar 30 23:45:17 EDT 2016 mZxid = 0x20000019e mtime = Wed Mar 30 23:45:17 EDT 2016 pZxid = 0x20000019e cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0 [zk: 127.0.0.1:2181(CONNECTED) 30] get /consumers/console-consumer-75348/offsets/topic1/0 0 cZxid = 0x20000019b ctime = Wed Mar 30 23:45:17 EDT 2016 mZxid = 0x20000019b mtime = Wed Mar 30 23:45:17 EDT 2016 pZxid = 0x20000019b cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0
10、创建生产者
sh kafka-console-producer.sh --broker-list localhost:9092 --topic topic1
11、随便输入消息,可以在消费者控制台看到对应的信息。
12、zk上查看对应的信息
[zk: localhost:2181(CONNECTED) 42] get /consumers/console-consumer-19167/offsets/topic1/0 3 cZxid = 0x2000001fc ctime = Thu Mar 31 01:50:52 EDT 2016 mZxid = 0x200000204 mtime = Thu Mar 31 01:52:51 EDT 2016 pZxid = 0x2000001fc cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0
可以看到offset有变化
好了,初步进行了总结,还是多动手才能有更深刻的体会
以上是关于kafka总结的主要内容,如果未能解决你的问题,请参考以下文章