Kafka常用操作

添加和删除主题(Topic)

你可能使用过手动创建主题或发送消息时自动创建主题这样的操作。但是你并不了解其中的详情。Kafka中的主题可以在主题配置中配置为自动创建的。

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 10 --topic my-favourite-topic
Created topic "my-favourite-topic".

复制因子“replication-factor”决定了有多少台服务器会复制消息的次数。通过建议有2~3台服务器进行容错,防止一台服务器宕机后数据就会丢失的问题。

分区数决定了同一个主题内的消息被分成多少个日志。分区数对于主题有以下影响。首先,每个服务器上的每个分区必须是完整的。比如你有10个分区组成的数据集,只能有不超过10个的服务器(集群服务器不算)处理。其次,分区数会影响并行的消费者数。详情可参考Kafka消费者部分的描述。

 

修改主题

将my-favourite-topic的主题分区修改为40个

 ./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-favourite-topic --partitions 20
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!

 

注意,改变分区数量并不会改变分区中的数据,但可能会对依赖这些分区的消费者有影响。如果把分区数当作哈希(hash-key)的话,增加分区可能会打乱数据,但是Kafka在任何情况下都不会自动重排数据。

增加配置

 ./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-favourite-topic --config x=y

 

移除配置

 ./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-favourite-topic --deleteConfig x=y

 

删除主题

 ./kafka-topics.sh --zookeeper localhost:2181 --delete --topic my-favourite-topic
Topic my-favourite-topic is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true

 

为防止活动的主题被删除,主题的删除方法默认是禁用的。如果想要删除主题的话,在server.properties中将delete.topic.enable设置为true即可。

delete.topic.enable=true

 

 优雅的停止服务器

 

当集群中的leader服务器停止服务后,Kafka集群会自动检测其它的服务器并选举出新的Leader服务器。因此每次使用Kill武断的停止服务器并不是明智的做法,当服务器停止后,它还会做两件事:

 

1. 它会将所有日志同步到磁盘避免下次重启时需要进行日志恢复。

 

2. 在迁移其它副本的数据前,将leader服务器的所有分区数据进行迁移。这样使得领导关系传递的更快,将每个分区的不可用时间缩小到毫秒。

 

如果不是使用kill结束服务的话,当服务器停止时同步日志是自动的。但是必须将以下属性设置为true,才能主动停止服务。

controlled.shutdown.enable=true

 

记住只有代理托管的所有分区都有副本时,关闭控制器才是有效的。

平衡领导力

当代理服务器停止或宕机时,这种领导关系就会自动转移到其它副本。即便该代理以后重启,它也只是一个从服务器,这样它就不能用于客户端读写数据。

 

为避免这种情况,Kafka有一个预定义副本的概念。如果副本集中定义的分区号是1,5,9,那么会按照顺序节点1优先成为leader服务器,节点5和9次之。

 

我们可以使用下面的命令试一试。如果不想每次都这么麻烦的话,就把设置auto.leader.rebalance.enable=true。

./kafka-preferred-replica-election.sh --zookeeper localhost:2181
Successfully started preferred replica election for partitions Set([__consumer_offsets,32], [__consumer_offsets,16], [__consumer_offsets,49], [__consumer_offsets,44], [my-favourite-topic,17], [my-favourite-topic,8], [test,0], [__consumer_offsets,28], [my-topic,0], [my-favourite-topic,11], [__consumer_offsets,17], [__consumer_offsets,23], [__consumer_offsets,7], [my-favourite-topic,4], [my-favourite-topic,15], [bar,0], [__consumer_offsets,4], [__consumer_offsets,29], [my-favourite-topic,2], [__consumer_offsets,35], [my-favourite-topic,7], [__consumer_offsets,3], [__consumer_offsets,24], [my-favourite-topic,16], [__consumer_offsets,41], [my-favourite-topic,0], [__consumer_offsets,0], [__consumer_offsets,38], [__consumer_offsets,13], [__consumer_offsets,8], [my-favourite-topic,10], [__consumer_offsets,5], [__consumer_offsets,39], [__consumer_offsets,36], [fav-topic,0], [my-favourite-topic,1], [__consumer_offsets,40], [__consumer_offsets,45], [__consumer_offsets,15], [my-favourite-topic,5], [__consumer_offsets,33], [__consumer_offsets,37], [my-favourite-topic,12], [__consumer_offsets,21], [my-replicated-topic,0], [__consumer_offsets,6], [my-favourite-topic,6], [__consumer_offsets,11], [__consumer_offsets,20], [__consumer_offsets,47], [myfavourite,0], [__consumer_offsets,2], [__consumer_offsets,27], [my-favourite-topic,3], [my-favourite-topic,13], [__consumer_offsets,34], [__consumer_offsets,9], [__consumer_offsets,22], [__consumer_offsets,42], [__consumer_offsets,14], [__consumer_offsets,25], [my-favourite-topic,14], [__consumer_offsets,10], [__consumer_offsets,48], [__consumer_offsets,31], [__consumer_offsets,18], [__consumer_offsets,19], [__consumer_offsets,12], [my-favourite-topic,9], [__consumer_offsets,46], [__consumer_offsets,43], [__consumer_offsets,1], [my-favourite-topic,19], [__consumer_offsets,26], [my-favourite-topic,18], [__consumer_offsets,30])

(文/genuinecx)

本文出自 “这个人的IT世界” 博客,请务必保留此出除http://favccxx.blog.51cto.com/2890523/1764122


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。