创建Topic
hadoop
kafka]# bin/kafka-topics.sh --create --topic hadoop --zookeeper master:2181,slave01:2181,slave02:2181 --partitions 1 --replication-factor 1
kafka]# bin/kafka-topics.sh --create --topic hive --zookeeper master:2181,slave01:2181,slave02:2181 --partitions 1 --replication-factor 1
kafka]# bin/kafka-topics.sh --create --topic hbase --zookeeper master:2181,slave01:2181,slave02:2181 --partitions 3 --replication-factor 1
创建topic过程的问题,replication-factor个数不能超过broker的个数
bin/kafka-topics.sh --create --topic sqoop --zookeeper master:2181,slave01:2181,slave02:2181 --partitions 3 --replication-factor 3
Error while executing topic command : replication factor: 3 larger than available brokers: 1
查看Topic列表
kafka]# bin/kafka-topics.sh --list --zookeeper master:2181,slave01:2181,slave02:2181
查看某一个具体的Topic
kafka]# bin/kafka-topics.sh --describe --topic hadoop --zookeeper master:2181,slave01:2181,slave02:2181
Topic:hadoop PartitionCount:1 ReplicationFactor:1
Configs:
Topic: hadoop Partition: 0 Leader: 0 Replicas: 0 Isr: 0
PartitionCount:topic对应的partition的个数
ReplicationFactor:topic对应的副本因子,说白就是副本个数
Partition:partition编号,从0开始递增
Leader:当前partition起作用的breaker.id
Replicas: 当前副本数据坐在的breaker.id,是一个列表,排在最前面的其作用
Isr:当前kakfa集群中可用的breaker.id列表
修改Topic
不能修改replication-factor,以及只能对partition个数进行增加,不能减少
bin/kafka-topics.sh --alter --topic hive --zookeeper master:2181,slave01:2181,slave02:2181 --partitions 3
partition由3变为2的时,抛出的异常:
ERROR kafka.admin.AdminOperationException: The number of partitions for a topic can only be increased
删除Topic
kafka]# bin/kafka-topics.sh --delete --topic hbase --zookeeper master:2181,slave01:2181,slave02:2181
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提供的标准生产消费脚本
生产数据
kafka]# bin/kafka-console-producer.sh --broker-list master:9092 --topic hadoop
生产数据的时候需要指定:当前数据流向哪个broker,以及哪一个topic
消费数据
bin/kafka-console-consumer.sh --topic hadoop --zookeeper master:2181,slave01:2181,slave02:2181
说明:该消费语句,只能获取最新的数据,要想历史数据,需要添加选项--from-beginning
bin/kafka-console-consumer.sh --topic hadoop --zookeeper master:2181,slave01:2181,slave02:2181 --from-beginning
在消费数据的时候,只需要指定topic,以及topic的元数据信息即可(在ZK中存放),所以这里需要使用zk
消费者--黑名单(blacklist)和白名单(whitelist)选项
--blacklist 后面跟需要过滤的topic的列表,使用","隔开,意思是除了列表中的topic之外,都能接收其它topic的数据
--whitelist 后面跟需要过滤的topic的列表,使用","隔开,意思是除了列表中的topic之外,都不能接收其它topic的数据
eg.
bin/kafka-console-consumer.sh --zookeeper master:2181,slave01:2181,slave02:2181 --from-beginning --blacklist hadoop,hive
bin/kafka-console-consumer.sh --zookeeper master:2181,slave01:2181,slave02:2181 --from-beginning --whitelist hadoop,fl