以新版kafka为例
- topic.sh脚本路径:kafka根目录/bin/kafka-topics.sh 注意:zookeeper需要知道kafka文件目录位置,不然无法效果 默认kafka目录在zookeeper的根目录 有些会在kafka目录,具体查看配置./config/server.properties配置文件里面的zookeeper.connect
- 查询topic列表
./kafka-topics.sh --list --zookeeper localhost:2181/kafka
- 创建topic
格式: ./kafka-topics.sh --create --topic topicname --replication-factor 1 --partitions 1 --zookeeper localhost:2181
例子: ./kafka-topics.sh --create --topic 'NULL_TOPIC' --replication-factor 1 --partitions 1 --zookeeper localhost:2181/kafka
创建格式问题: 命名中存在‘_’会提示warning,但可以创建成功
问题提示: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 "NULL_TOPIC".
不建议使用‘_’命名方式
- 删除topic
格式: ./kafka-topics.sh --delete --topic topicname --zookeeper localhost:2181/kafka
例子: ./kafka-topics.sh --delete --topic 'NULL_TOPIC' --zookeeper localhost:2181/kafka
删除问题: delete.topic.enable设置false时,会伪删除,并非真正删除。
需要设置在./config/server.properties配置文件中设置delete.topic.enable = true
Topic NULL_TOPIC is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
彻底删除方式:
1.设置server.properties配置文件中的delete.topic.enable = true
2.找server.properties配置文件中的log.dir位置 进行删除目标topic的目录
3.删除zookeeper的kafka目录/brokers/topics/topicName
- 查看topic具体信息
格式: ./kafka-topics.sh --describe --topic topicname --zookeeper localhost:2181/kafka
例子: ./kafka-topics.sh --describe --topic 'NULL_TOPIC' --zookeeper localhost:2181/kafka
响应结果: 分区数量 副本数量 Topic配置
Topic:NULL_TOPIC PartitionCount:1 ReplicationFactor:1 Configs:
Topic:NULL_TOPIC Partition: 0 Leader: 0 Replicas: 0 Isr: 0
多个副本的情况下才会有显示: Leader、Replicas、Isr信息
Leader:负责读取写入的节点
Replicas:显示给定partiton所有副本所存储节点的节点列表,不管该节点是否是leader或者是否存活。
Isr:副本都已同步的的节点集合,这个集合中的所有节点都是存活状态,并且跟leader同步
响应结果:(多个副本的)分区数量 副本数量 Topic配置
Topic:NULL_TOPIC PartitionCount:6 ReplicationFactor:3 Configs:
Topic: NULL_TOPIC Partition: 0 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1
Topic: NULL_TOPIC Partition: 1 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
Topic: NULL_TOPIC Partition: 2 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
Topic: NULL_TOPIC Partition: 3 Leader: 2 Replicas: 2,1,3 Isr: 2,1,3
Topic: NULL_TOPIC Partition: 4 Leader: 3 Replicas: 3,2,1 Isr: 3,2,1
Topic: NULL_TOPIC Partition: 5 Leader: 1 Replicas: 1,3,2 Isr: 1,3,2
须知:每个kafka有可有多个broker(作用:副本),每个broker又有多个topic,每个topic又有多个partition,每个partition又有多个副本,partition会选择Leader节点进行读取写入,其他则负责同步
- 发送消息到topic
格式: ./kafka-console-producer.sh --topic topicname --property parse.key=true --broker-list brokerIp:9092
例子: ./kafka-console-producer.sh --topic NULL_TOPIC --property parse.key=true --broker-list localhost:9092
参数:
1.--property parse.key=true:是否使用key
2.--property key.separator=':':key和value之间的分隔符
问题1:WARN [Producer clientId=console-producer] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
需要查看server.proerties 中listeners=PLAINTEXT:ip:9092 地址 brokerIp需要填写该配置的ip
问题2: --bootstrap-server 使用提示 bootstrap-server is not a recognized option
版本问题: Kafka_2.12-2.5.0开始使用
问题3:ERROR Error when sending message to topic NULL_TOPIC with key: null, value: 0 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback) org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
可能性一:kafka版本0.8.1.1升级为0.10.0.1后 topic不通用导致
可能性二:设置log.cleanup.policy=compact key不能为空 delete-> 按照时间保留的策略进行删除 删除时刻:不活跃的segment的时间戳是大于设置的时间 compact-> 根据Key进行去重清理
可能性三:数据无法与其他broker进行同步 需查看server.log日志
- 消息topic中消息
格式: ./kafka-console-consumer.sh --topic topicname --bootstrap-server brokerIp:9092
例子: ./kafka-console-consumer.sh --topic NULL_TOPIC --bootstrap-server localhost:9092
参数:
--from-beginning 从头开始消费
--consumer-property client.id=消费者ID
--consumer-property group.id=消费组ID
- 查看消费组消费情况
格式: ./kafka-consumer-groups.sh --describe --group client.id --bootstrap-server brokerIp:9092
例子: ./kafka-consumer-groups.sh --describe --group NULL_TOPIC_CONSUMER --bootstrap-sever localhost:9092
响应结果:主题 分区 当前offset 日志最后offset 积压数量 消费者ID ip 消费者
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
NULL_TOPIC 4 6737 6737 0 consumer-2-158028e0-1531-4b77-a9f8-3c9ee45327bf /172.25.23.12 consumer-2
- 查看所有消费组情况
格式: ./kafka-consumer-groups.sh --list --bootstrap-server brokerIp:9092
例子: ./kafka-consumer-groups.sh --list --bootstrap-server localhost:9092