目录
创建topic
$KAFKA_HOME/bin/kafka-topics.sh --create --topic channel.test.HelloWorld --zookeeper node01:2181,node02:2181,node03:2181/kafka --partitions 1 --replication-factor 3
查看topic的列表
$KAFKA_HOME/bin/kafka-topics.sh --list --zookeeper node01:2181,node02:2181,node03:2181/kafka
新版本生产者(支持0.9版本+)
$KAFKA_HOME/bin/kafka-console-producer.sh --broker-list node01:9092,node02:9092,node03:9092 --topic channel.test.HelloWorld --producer.config $KAFKA_HOME/config/producer.properties
新版本消费者(支持0.9版本+)
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server node01:9092,node02:9092,node03:9092 --topic channel.test.HelloWorld --consumer.config config/consumer.properties
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server node01:9092,node02:9092,node03:9092 --topic channel.test.HelloWorld --from-beginning
注: 使用"--from-beginning"的时候,后面不能跟"--consumer.config config/consumer.properties"
查看某一个具体的Topic
kafka-topics.sh --describe xxx --zookeeper uplooking01:2181
Topic:xxx PartitionCount:3 ReplicationFactor:1
Configs:
Topic: xxx Partition: 0 Leader: 10 Replicas: 10 Isr: 10
Topic: xxx Partition: 1 Leader: 10 Replicas: 10 Isr: 10
Topic: xxx Partition: 2 Leader: 10 Replicas: 10 Isr: 10
PartitionCount:topic对应的partition的个数
ReplicationFactor:topic对应的副本因子,说白就是副本个数
Partition:partition编号,从0开始递增
Leader:当前partition起作用的breaker.id
Replicas: 当前副本数据坐在的breaker.id,是一个列表,排在最前面的起作用
Isr:当前kakfa集群中可用的breaker.id列表
修改Topic
不能修改replication-factor,以及只能对partition个数进行增加,不能减少
kafka-topics.sh --alter --topic hive --zookeeper node01:2181,node02:2181,node03:2181/kafka --partitions 3
partition由3变为2的时,抛出的异常:
ERROR kafka.admin.AdminOperationException: The number of partitions for a topic can only be increased
删除Topic
$KAFKA_HOME/bin/kafka-topics.sh --delete --topic channel.test.HelloWorld --zookeeper node01:2181,node02:2181,node03:2181/kafka
Topic hbase is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
彻底删除一个topic,需要在server.properties中配置delete.topic.enable=true,否则只是标记删除
配置完成之后,需要重启kafka服务。
kafka-server-stop.sh
kill -9
$ZOOKEEPER_HOME/bin/zkCli.sh -server node01:2181,node02:2181,node03:2181/kafka
# 删除zookeeper上的topic存储信息
rmr /kafka/brokers/topics/channel.test.HelloWorld
rmr /kafka/config/topics/channel.test.HelloWorld
# 删除每个broker所在服务器上的所有数据文件
rm -rf /data/kafka/logs/data/channel.test.HelloWorld*
rm -rf /data/kafka/logs/channel.test.HelloWorld*
# 重启kafka
$KAFKA_HOME/bin/kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties
查询topic每个partition的偏移量
$KAFKA_HOME/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list node01:9092,node02:9092,node03:9092 --topic channel.test.HelloWorld --time -1