4.1主题管理
4.1.1 创建主题
不建议将auto.create.topics.enable参数设置为true
创建主题
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --create --topic topicId --partitions 4 --replication-factory 2
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --create --topic topicId --replica-assignment 0:1,1:2,2:0
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --create --topic topicId --partitions 1 --replication-factory 1 --config cleanup.policy=compact --config max.message.bytes=10000
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --create --topic topicId --partitions 4 --replication-factory 2 --if-not-exists
命名埋点
4.1.2 分区副本的分配
4.1.3 查看主题
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --list
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --describe --topic topicId
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --describe --topics-with-overrides (只展示非默认配置的主题)
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --describe --topic topicId --under-replicated-partitions (只展示失效副本的分区)
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --describe --topic topicId --unavailable-partitions (只展示离线分区)
4.1.4 修改主题
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --alter --topic topicId --partitions 3 (不支持减少分区)
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --alter --topic topicId --partitions 3 --if-exists
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --alter --topic topicId --config max.message.bytes=20000 (使用kafka-topics.sh增、改配置已过期,推荐使用kafka-configs.sh)
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --alter --topic topicId --delete-config max.message.bytes (使用kafka-topics.sh删除配置已过期,推荐使用kafka-configs.sh)
4.1.5 配置管理
bin/kafka-configs.sh --zookeeper localhost:2181/kafka --alter --entity-type topics --entity-name topic-config --add-config max.message.bytes=20000
bin/kafka-configs.sh --zookeeper localhost:2181/kafka --alter --entity-type topics --entity-name topic-config --delete-config max.message.bytes
bin/kafka-configs.sh --zookeeper localhost:2181/kafka --describe --entity-type topics (查看所有关于topics的配置)
--entity-type包含topics、brokers、users、clients
--entity-name即为zk中的目录名
4.1.6 主题端参数
cleanup.policy
compression.type
delete.retention.ms
file.delete.delay.ms
flush.messages
flush.ms
follower.replication.throttled.
replicas
index.interval.bytes
leader.replication.throttled.replicas
max.compaction.lag.ms
max.message.bytes
message.downconversion.enable
message.format.version
message.timestamp.difference.max.ms
message.timestamp.type
min.cleanable.dirty.ratio
min.compaction.lag.ms
min.insync.replicas
preallocate
retention.bytes
retention.ms
segment.bytes
segment.index.bytes
segment.jitter.ms
segment.ms
unclean.leader.election.enable
4.1.7 删除主题
1.broker端参数必须将delete.topic.enable配置为true
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --delete --topic topicId
bin/kafka-topics.sh --zookeeper localhost:2181/kafka --delete --topic topicId --if-exists
2.在zkCli中创建 create /admin/delete_topics/topicId
3.手动删除zkCli的/brokers/topics和/config/topics对应的topicId的内容,手动删除broker端log.dir或log.dirs配置的目录对应的topicId的内容
rmr /config/topics/topicId
delete /brokers/topics/topicId
rm -rf /tmp/kafka-logs/topicId*
4.2初始KafkaAdminClient
4.2.1 基本使用
创建主题、删除主题、列出所有可用主题、查看主题的信息、查询配置信息、修改配置信息、增加分区
注:基于kafka2.5.1发现,API中并没有直接增加副本的方法,只能通过指定分配方案的方式增加副本
4.2.2 主题合法性验证
create.topic.policy.class.name默认为null,指定类名,实现CreateTopicPolicy接口
4.3分区的管理
4.3.1 优先副本的选举
broker节点中leader副本个数的多少决定了节点负载
broker端参数是auto.leader.rebalance.enable,此参数的默认值为true。建议设置为false。在空闲时间手动分区平衡
如果开启,则kafka的控制器会启动一个定时任务,这个定时任务会轮询所有的broker节点
计算每个节点的分区不平衡率(非优先副本的leader个数/分区总数)是否超过leader.imbalance.per.broker.percentage参数配置的比例,默认10%
执行周期由参数leader.imbalance.check.interval.seconds控制,默认为300秒
bin/kafka-preferred-replica-election.sh --zookeeper localhost:2181/kafka (手动重新平衡)
bin/kafka-preferred-replica-election.sh --zookeeper localhost:2181/kafka --path-to-json-file election.json (指定json文件,文件内容如下)
{
"partitions":[
{"partition":0,"topic":"topicId"},{"partition":1,"topic":"topicId"},{"partition":2,"topic":"topicId"}
]
}
4.3.2 分区重分配
- 创建json文件,内容如下: {"topics":[{"topic":"topic-reassign"}], "version":1}
- bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafka --generate --topic-to-move-json-file reassign.json --broker-list 0,2
打印出2个json,第一个为当前,第二个为预期,将第二个json保存为project.json - bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafka --execute --reassignment-json-file project.json
- (可选)查看进度 bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafka --verify --reassignment-json-file project.json
4.3.3 复制限流
broker级别有两个与复制限流相关的配置参数:follower.replication.throttled.rate和leader.replication.throttled.rate,单位B/s
设置leader副本和follower副本的复制速度限制在1KB之内,命令如下:
bin/kafka-configs.sh --zookeeper localhost:2181/kafka –entity-type brokers –entity-name 1 –alter –add-config follower.replication.throttled.rate=1024,leader.replication.throttled.rate=1024
kafka-reassign-partitions.sh脚本也提供了限流的功能
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafka --execute --reassignment-json-file project.json --throttle 1024
json文件如下:
{"version":1, "partitions":[
{"topic":"topicId","partitions":0,"replicas":[0,1],"log_dirs":["any","any"]},
{"topic":"topicId","partitions":1,"replicas":[1,2],"log_dirs":["any","any"]},
{"topic":"topicId","partitions":2,"replicas":[2,0],"log_dirs":["any","any"]}
]}
4.3.4 修改副本因子
使用4.3.3中的json文件,在json文件中设置副本因子,project.json如下:
{"version":1, "partitions":[
{"topic":"topicId","partitions":0,"replicas":[0,1,2],"log_dirs":["any","any","any"]},
{"topic":"topicId","partitions":1,"replicas":[0,1,2],"log_dirs":["any","any","any"]},
{"topic":"topicId","partitions":2,"replicas":[0,1,2],"log_dirs":["any","any","any"]}
]}
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafka --execute --reassignment-json-file project.json
4.4如何选择合适的分区数
4.4.1 性能测试工具
4.2.2 分区数越多吞吐量就越高吗
4.4.3 分区数的上限
修改分区数的上限:ulimit –n 65535