Kafka的Topic操作

Kafka shell topic操作
    创建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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值