kafka-基本操作
操作zookeeper
验证zookeeper集群的运行状态
$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader
查找zookeeper的进程pid
jps
47877 QuorumPeerMain
操作kafka集群
查看kafka集群是否启动:
jps
管理主题
kafka-topics.sh 脚本用来管理主题
主题:将不同类型的消息存储到不同的主题中,例如,充值记录,消费记录分别作为一个主题
主题创建分为自动创建和手动创建
手动创建主题:
$ kafka-topics.sh --create -zookeeper node1:2181,node2:2181,node3:2181 --replication-factor 3 --partitions 6 --topic ip_login
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic ip_login.
创建一个名称为ip_login的主题,3个副本,6个分区。
/usr/local/kafka/kafka_2.11-2.2.0/data 下面可以看到6个分区的信息的文件
主题创建成功后,访问zookeeper :
zkCli.sh -server node1:2181,node2:2181,node3:2181
执行ls命令可以查看到分区数
[zk: node1:2181,node2:2181,node3:2181(CONNECTED) 1] ls /brokers/topics/ip_login/partitions
[0, 1, 2, 3, 4, 5]
执行get命令可以看到分区的元数据信息
[zk: kafka_node1:2181,kafka_node2:2181,kafka_node3:2181(CONNECTED) 2] get /brokers/topics/ip_login
{"version":1,"partitions":{"4":[0,2,1],"5":[1,0,2],"1":[0,1,2],"0":[2,0,1],"2":[1,2,0],"3":[2,1,0]}}
cZxid = 0x3000000da
ctime = Mon May 27 20:06:16 CST 2019
mZxid = 0x3000000da
mtime = Mon May 27 20:06:16 CST 2019
pZxid = 0x3000000dc
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 100
numChildren = 1
查看主题:
$ kafka-topics.sh --describe -zookeeper node1:2181,node2:2181,node3:2181
Topic:ip_login PartitionCount:6 ReplicationFactor:3 Configs:
Topic: ip_login Partition: 0 Leader: 2 Replicas: 2,0,1 Isr: 0,1,2
Topic: ip_login Partition: 1 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
Topic: ip_login Partition: 2 Leader: 1 Replicas: 1,2,0 Isr: 0,1,2
Topic: ip_login Partition: 3 Leader: 2 Replicas: 2,1,0 Isr: 0,1,2
Topic: ip_login Partition: 4 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
Topic: ip_login Partition: 5 Leader: 1 Replicas: 1,0,2 Isr: 0,1,2
列出所有主题的描述信息,共有6个分区,3个副本
其中分区0的第一个副本在节点2上,分区1的第一个副本在节点2上...
分区0的第一个副本在代理节点2上,第2个副本在代理节点0上,第3个副本在代理节点1上
列出所有的主题:
$ kafka-topics.sh --list -zookeeper node1:2181,node2:2181,node3:2181
ip_login
查看正在同步的主题; 查看正在同步或者同步发生异常的主题。
kafka-topics.sh --describe --zookeeper node1:2181,node2:2181,node3:2181 --under-replicated-partitions
查看主题中哪些分区不可用:
kafka-topics.sh --describe --zookeeper node1:2181,node2:2181,node3:2181 --unavailable-partitions
生成配置主题:
# root @ kafka_node1 in ~ [9:01:57] C:1
$ kafka-topics.sh --create --zookeeper node1:2181,node2:2181,node3:2181 --replication-factor 1 --partitions 1 --topic user_order2 --config max.message.bytes=102400
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic user_order2.
查看被覆写的配置
# root @ kafka_node1 in ~ [9:05:59] C:1
$ kafka-topics.sh --describe --zookeeper node1:2181,node2:2181,node3:2181 --topic user_order2 --topics-with-overrides
Topic:user_order2 PartitionCount:1 ReplicationFactor:1 Configs:max.message.bytes=102400
删除主题:
需要在server.propertis 文件中配置
vim /usr/local/kafka/kafka_2.11-2.2.0/config/server.properties
delete.topic.enable=true
在zookeeper为leader的的节点上删除主题
# root @ localhost in ~ [9:07:04]
$ kafka-topics.sh --zookeeper node1:2181,node2:2181,node3:2181 -delete --topic user_order2
Topic user_order2 is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
管理分区与副本
主题的分区只能增加不能减少
增加分区数
增加分区数
$ kafka-topics.sh --partitions 7 --alter --zookeeper node1:2181,node2:2181,node3:2181 --topic ip_login
查看主题的信息
kafka-topics.sh --describe --zookeeper node1:2181,node2:2181,node3:2181 --topic ip_logi