kafka主题与分区

kafka主题与分区

​ 主题和分区时kafka的两个核心概念,主题作为消息的归类,可以在细分为一个或者多个分区,分区可以看作是对消息的二次归类。分区的划分不仅可以为kafka提供了可伸缩性,水平扩展能力,还可以通过副本机制来为kafka提供数据冗余以提高数据的可高性。

从底层上来说,主题和分区都是逻辑上的概念。分区可以有一个或多个副本,每个副本对应一个日志文件,每个日志文件对应一个或多个日志段文件。

主题的管理

一、创建主题

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --create -zookeeper 127.0.0.1:2181 -replication-factor 3 --partitions 3 --topic topic-create
Created topic topic-create.

这里我们使用kafka-topic.sh脚本来创建主题。

–create: 表示创建主题

–zookeeper: 后面表示kafka集群中的使用的zookpeer集群地址

–replication-factor:代表副本数

–partitions:代表分区数

–topic:代表主题的名称

二、查看主题

使用list来查看所有的主题

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 -list
__consumer_offsets
topic-create
topic-demo
topic-demo2
topic-demo3

使用describe指令来查看单个主题的信息

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topic-create
Topic: topic-create	PartitionCount: 3	ReplicationFactor: 3	Configs: 
	Topic: topic-create	Partition: 0	Leader: 1	Replicas: 1,2,0	Isr: 0,1,2
	Topic: topic-create	Partition: 1	Leader: 2	Replicas: 2,0,1	Isr: 0,1,2
	Topic: topic-create	Partition: 2	Leader: 0	Replicas: 0,1,2	Isr: 0,1,2

–describe : 表示用来描述主题的详细信息。

–topic:指定要查看的主题名称

上面的主题是一个3个节点,3个副本。并且其运行在一个有三个broke节点上的kafka集群上。

三、修改主题

我们现在将topic-create的分区数量增加一个。

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic-create --partitions 4
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!

上面的警告信息显示,当主题中的消息包含key时,根据key进行分区行为就会受到影响。

我们尝试将分区数减少一个:

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic-create --partitions 3
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Error while executing topic command : The number of partitions for a topic can only be increased. Topic topic-create currently has 4 partitions, 3 would not be an increase.
[2020-04-07 20:28:12,017] ERROR org.apache.kafka.common.errors.InvalidPartitionsException: The number of partitions for a topic can only be increased. Topic topic-create currently has 4 partitions, 3 would not be an increase.
 (kafka.admin.TopicCommand$)

可以看到有如下的错误信息,说明目前kafka只支持增加分区而不支持减少分区。

org.apache.kafka.common.errors.InvalidPartitionsException: The number of partitions for a topic can only be increased. 

四、配置管理

kafka-config.sh脚本时专门用来对配置进行操作的,这里的操作时只在运行状态下修改原有的配置。如此可以达到动态变更的目的。kafka-configs.sh脚本包含变更配置和查看配置这两种类型的指令。

​ kafka-configs.sh 脚本使用entity-type参数来指定操作配置的类型,并且使用entity-name参数来指定操作配置的名称。比如查看主题的topic-create的配置可以按如下方式执行:

[root@localhost kafka_2.12-2.4.1]# bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type topics --entity-name topic-create
Configs for topic 'topic-create' are 

这里由于使用全是默认设置,所以没有显示:

接下来我们时测试修改主题配置:

[root@localhost kafka_2.12-2.4.1]# bin/kafka-configs.sh --zookeeper localhost:2181 --alter --entity-type topics --entity-name topic-create --add-config cleanup.policy=compact,max.message.bytes=10000
Completed Updating config for entity: topic 'topic-create'.

然后再次查看:

[root@localhost kafka_2.12-2.4.1]# bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type topics --entity-name topic-create
Configs for topic 'topic-create' are max.message.bytes=10000,cleanup.policy=compact

五、kafka主题端参数

与主题端相关的参数在broker层面都有对应的参数,比如主题端参数cleanup.policy对应broker层面的log.cleanup.policy.如果没有修改主题的任何配置参数那么就会使用broker端对应的参数作为其默认值。具体的参数配置请参考官网

六、删除主题

如果确定不再使用某个主题,那么最好的方式就是将其删除,这样可以释放一些资源。这里演示删除主题’topic-create’

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic topic-create
Topic topic-create is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

现在我们再次查看所有的主题,发现主题topic-create确实已经被删除。

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 -list
__consumer_offsets
topic-demo
topic-demo2
topic-demo3

再次尝试删除topic-create

[root@localhost kafka_2.12-2.4.1]# bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic topic-create
Error while executing topic command : Topic 'topic-create' does not exist as expected
[2020-04-07 23:07:46,996] ERROR java.lang.IllegalArgumentException: Topic 'topic-create' does not exist as expected
	at kafka.admin.TopicCommand$.kafka$admin$TopicCommand$$ensureTopicExists(TopicCommand.scala:484)
	at kafka.admin.TopicCommand$ZookeeperTopicService.deleteTopic(TopicCommand.scala:442)
	at kafka.admin.TopicCommand$.main(TopicCommand.scala:69)
	at kafka.admin.TopicCommand.main(TopicCommand.scala)
 (kafka.admin.TopicCommand$)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值