zoukankan      html  css  js  c++  java
  • kafka-常用脚本

    Kafka常用脚本
    在Kafka安装目录下($KAFKA_HOME/bin),提供了很多内置的脚本供我们使用。使用脚本可以测试Kafka的大多数功能,下面我们就脚本的使用作出说明。

    1. 启动broker
      bin/kafka-server-start.sh脚本提供了启动broker的功能。

    前台启动:

    > bin/kafka-server-start.sh config/server.properties
    

    后台启动:

    > bin/kafka-server-start.sh -daemon config/server.properties
    
    1. 停止broker
      bin/kafka-server-stop.sh脚本提供了停止broker的功能。
    > bin/kafka-server-stop.sh
    
    1. 创建topic
      bin/kafka-topic.sh脚本提供了创建topic的功能。创建topic时,需要指定两个参数:
      • 分区数量
      • 副本数量

    注意,副本数量最多不能超过当前集群中broker节点的数量。

    下面创建一个名为test的topic,具有3个分区,副本数量为2。

    > bin/kafka-topics.sh --zookeeper localhost:2181 --create --partitions 3 --replication-factor 2 --topic test
    
    1. 查看topic列表
      bin/kafka-topic.sh脚本提供了查看topic列表的功能。通过–list参数即可查看当前集群所有的topic列表。
    > bin/kafka-topics.sh --zookeeper localhost:2181 --list
    __consumer_offsets
    test
    
    1. 查看某个topic的详细信息
      bin/kafka-topic.sh脚本提供了查看某个topic详细信息的功能。通过–describe参数和–topic参数指定topic名称即可。
    > bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic test
    Topic:test  PartitionCount:3    ReplicationFactor:2 Configs:
        Topic: test Partition: 0    Leader: 2   Replicas: 2,0   Isr: 2,0
        Topic: test Partition: 1    Leader: 0   Replicas: 0,1   Isr: 0,1
        Topic: test Partition: 2    Leader: 1   Replicas: 1,2   Isr: 1,2
    
    1. 删除topic
      bin/kafka-topic.sh脚本提供了删除topic的功能。通过–delete参数和–topic参数指定topic名称即可。

    将名称为test的topic删除:

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

    注意,broker配置中必须设置delete.topic.enable=true,否则删除操作不会生效。

    1. 命令行生产者
      bin/kafka-console-producer.sh脚本能够通过命令行输入向指定的topic中发送数据。

    向名称为test的topic中发送3条数据:

    > bin/kafka-console-producer.sh --broker-list hostname:9092 --topic test
    This is a message
    This is another message
    hello kafka
    ^C
    
    1. 命令行消费者
      bin/kafka-console-consumer.sh脚本能够消费topic中的数据并打印到控制台。

    消费名称为test的topic中的数据:

    > bin/kafka-console-consumer.sh --bootstrap-server hostname:9092 --topic test --from-beginning
    This is a message
    This is another message
    hello kafka
    ^CProcessed a total of 3 messages
    

    注意,–from-beginning参数表示从topic的最开始位置进行消费,如果没有指定该参数,表示从末尾开始消费,只有当启动消费者后,有新的数据写入,才能够显示到控制台。

    1. 查看消费组
      bin/kafka-consumer-groups.sh脚本能够查看集群中消费组的信息。通过–list参数能够列出当前消费组列表。
    > bin/kafka-consumer-groups.sh --bootstrap-server hostname:9092 --new-consumer --list
    console-consumer-54336
    

    通过–describe参数可以查看消费组的消费详情:

    > bin/kafka-consumer-groups.sh --bootstrap-server company01:9092 --new-consumer --describe --group console-consumer-54336
     
    TOPIC                          PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG        CONSUMER-ID                                       HOST                           CLIENT-ID
    test                           0          1               1               0          consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf   /192.168.100.109               consumer-1
    test                           1          1               1               0          consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf   /192.168.100.109               consumer-1
    test                           2          1               1               0          consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf   /192.168.100.109               consumer-1
    

    CURRENT-OFFSET表示消费者当前消费到该分区的偏移量。

    LOG-END-OFFSET表示当前分区最后的偏移量。

    LAG表示消费者“落后”的消费进度。

  • 相关阅读:
    celery的使用
    DOM操作
    js动画
    列表案例
    背景案例
    背景属性连写
    背景属性
    链接导航案例
    链接伪类
    优先权之权重会叠加
  • 原文地址:https://www.cnblogs.com/shangwei/p/13405661.html
Copyright © 2011-2022 走看看