• Kafka:ZK+Kafka+Spark Streaming集群环境搭建(九)安装kafka_2.11-1.1.0


    如何搭建配置centos虚拟机请参考《Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网。

    如何安装hadoop2.9.0请参考《Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二)安装hadoop2.9.0

    如何配置hadoop2.9.0 HA 请参考《Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十)安装hadoop2.9.0搭建HA

    如何安装spark2.2.1请参考《Kafka:ZK+Kafka+Spark Streaming集群环境搭建(三)安装spark2.2.1

    如何安装zookeeper-3.4.12请参考《Kafka:ZK+Kafka+Spark Streaming集群环境搭建(八)安装zookeeper-3.4.12

    常识科普:Kafka的存储与安装不依赖与hdfs/spark,从下边安装过程你可以得知这个信息。

    安装kafka的服务器:

    192.168.0.120      master
    192.168.0.121      slave1
    192.168.0.122      slave2
    192.168.0.123      slave3

    备注:只在slave1,slave2,slave3三个节店上安装zookeeper,master节店不安装(其实前边hadoop中master不作为datanode节店,spark中master不作为worker节店)。

    下载解压

    官网上下载kafka,并上传到slave1(192.168.0.121)的/opt目录下。这里kafka下载的是:kafka_2.11-1.1.0.tgz

    在slave1上解压kafka_2.11-1.1.0.tgz

    [root@slave1 opt]# tar -zxvf kafka_2.11-1.1.0.tgz 

    配置kafka

    1)配置文件位置

    路径:/opt/kafka_2.11-1.1.0/config/server.properties

    [root@slave1 config]# ls
    connect-console-sink.properties    connect-distributed.properties  connect-file-source.properties  connect-standalone.properties  log4j.properties     server.properties       zookeeper.properties
    connect-console-source.properties  connect-file-sink.properties    connect-log4j.properties        consumer.properties            producer.properties  tools-log4j.properties
    [root@slave1 config]# 

    2)server.properties默认配置

    [root@slave1 config]# cd /opt/kafka_2.11-1.1.0/config
    [root@slave1 config]# ls
    connect-console-sink.properties    connect-distributed.properties  connect-file-source.properties  connect-standalone.properties  log4j.properties     server.properties       zookeeper.properties
    connect-console-source.properties  connect-file-sink.properties    connect-log4j.properties        consumer.properties            producer.properties  tools-log4j.properties
    [root@slave1 config]# more server.properties 
    # Licensed to the Apache Software Foundation (ASF) under one or more
    # contributor license agreements.  See the NOTICE file distributed with
    # this work for additional information regarding copyright ownership.
    # The ASF licenses this file to You under the Apache License, Version 2.0
    # (the "License"); you may not use this file except in compliance with
    # the License.  You may obtain a copy of the License at
    #
    #    http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    # see kafka.server.KafkaConfig for additional details and defaults
    
    ############################# Server Basics #############################
    
    # The id of the broker. This must be set to a unique integer for each broker.
    broker.id=0
    
    ############################# Socket Server Settings #############################
    
    # The address the socket server listens on. It will get the value returned from 
    # java.net.InetAddress.getCanonicalHostName() if not configured.
    #   FORMAT:
    #     listeners = listener_name://host_name:port
    #   EXAMPLE:
    #     listeners = PLAINTEXT://your.host.name:9092
    #listeners=PLAINTEXT://:9092
    
    # Hostname and port the broker will advertise to producers and consumers. If not set, 
    # it uses the value for "listeners" if configured.  Otherwise, it will use the value
    # returned from java.net.InetAddress.getCanonicalHostName().
    #advertised.listeners=PLAINTEXT://your.host.name:9092
    
    # Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
    #listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
    
    # The number of threads that the server uses for receiving requests from the network and sending responses to the network
    num.network.threads=3
    
    # The number of threads that the server uses for processing requests, which may include disk I/O
    num.io.threads=8
    
    # The send buffer (SO_SNDBUF) used by the socket server
    socket.send.buffer.bytes=102400
    
    # The receive buffer (SO_RCVBUF) used by the socket server
    socket.receive.buffer.bytes=102400
    
    # The maximum size of a request that the socket server will accept (protection against OOM)
    socket.request.max.bytes=104857600
    
    
    ############################# Log Basics #############################
    
    # A comma separated list of directories under which to store log files
    log.dirs=/tmp/kafka-logs
    
    # The default number of log partitions per topic. More partitions allow greater
    # parallelism for consumption, but this will also result in more files across
    # the brokers.
    num.partitions=1
    
    # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
    # This value is recommended to be increased for installations with data dirs located in RAID array.
    num.recovery.threads.per.data.dir=1
    
    ############################# Internal Topic Settings  #############################
    # The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
    # For anything other than development testing, a value greater than 1 is recommended for to ensure availability such as 3.
    offsets.topic.replication.factor=1
    transaction.state.log.replication.factor=1
    transaction.state.log.min.isr=1
    
    ############################# Log Flush Policy #############################
    
    # Messages are immediately written to the filesystem but by default we only fsync() to sync
    # the OS cache lazily. The following configurations control the flush of data to disk.
    # There are a few important trade-offs here:
    #    1. Durability: Unflushed data may be lost if you are not using replication.
    #    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
    #    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
    # The settings below allow one to configure the flush policy to flush data after a period of time or
    # every N messages (or both). This can be done globally and overridden on a per-topic basis.
    
    # The number of messages to accept before forcing a flush of data to disk
    #log.flush.interval.messages=10000
    
    # The maximum amount of time a message can sit in a log before we force a flush
    #log.flush.interval.ms=1000
    
    ############################# Log Retention Policy #############################
    
    # The following configurations control the disposal of log segments. The policy can
    # be set to delete segments after a period of time, or after a given size has accumulated.
    # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
    # from the end of the log.
    
    # The minimum age of a log file to be eligible for deletion due to age
    log.retention.hours=168
    
    # A size-based retention policy for logs. Segments are pruned from the log unless the remaining
    # segments drop below log.retention.bytes. Functions independently of log.retention.hours.
    #log.retention.bytes=1073741824
    
    # The maximum size of a log segment file. When this size is reached a new log segment will be created.
    log.segment.bytes=1073741824
    
    # The interval at which log segments are checked to see if they can be deleted according
    # to the retention policies
    log.retention.check.interval.ms=300000
    
    ############################# Zookeeper #############################
    
    # Zookeeper connection string (see zookeeper docs for details).
    # This is a comma separated host:port pairs, each corresponding to a zk
    # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
    # You can also append an optional chroot string to the urls to specify the
    # root directory for all kafka znodes.
    zookeeper.connect=localhost:2181
    
    # Timeout in ms for connecting to zookeeper
    zookeeper.connection.timeout.ms=6000
    
    
    ############################# Group Coordinator Settings #############################
    
    # The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
    # The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
    # The default value for this is 3 seconds.
    # We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
    # However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
    group.initial.rebalance.delay.ms=0
    View Code

    3)在slave1上,修改server.properties后配置内容

    # Licensed to the Apache Software Foundation (ASF) under one or more
    # contributor license agreements.  See the NOTICE file distributed with
    # this work for additional information regarding copyright ownership.
    # The ASF licenses this file to You under the Apache License, Version 2.0
    # (the "License"); you may not use this file except in compliance with
    # the License.  You may obtain a copy of the License at
    #
    #    http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    # see kafka.server.KafkaConfig for additional details and defaults
    
    ############################# Server Basics #############################
    
    # The id of the broker. This must be set to a unique integer for each broker.
    broker.id=0
    
    ############################# Socket Server Settings #############################
    
    # The address the socket server listens on. It will get the value returned from 
    # java.net.InetAddress.getCanonicalHostName() if not configured.
    #   FORMAT:
    #     listeners = listener_name://host_name:port
    #   EXAMPLE:
    #     listeners = PLAINTEXT://your.host.name:9092
    
    listeners=PLAINTEXT://:9092
    port=9092
    host.name=192.168.0.121
    advertised.host.name=192.168.0.121
    advertised.port=9092
    
    
    # Hostname and port the broker will advertise to producers and consumers. If not set, 
    # it uses the value for "listeners" if configured.  Otherwise, it will use the value
    # returned from java.net.InetAddress.getCanonicalHostName().
    #advertised.listeners=PLAINTEXT://your.host.name:9092
    
    # Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
    #listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
    
    # The number of threads that the server uses for receiving requests from the network and sending responses to the network
    num.network.threads=3
    
    # The number of threads that the server uses for processing requests, which may include disk I/O
    num.io.threads=8
    
    # The send buffer (SO_SNDBUF) used by the socket server
    socket.send.buffer.bytes=102400
    
    # The receive buffer (SO_RCVBUF) used by the socket server
    socket.receive.buffer.bytes=102400
    
    # The maximum size of a request that the socket server will accept (protection against OOM)
    socket.request.max.bytes=104857600
    
    
    ############################# Log Basics #############################
    
    # A comma separated list of directories under which to store log files
    #log.dirs=/tmp/kafka-logs
    log.dirs=/opt/kafka_2.11-1.1.0/logs
    
    # The default number of log partitions per topic. More partitions allow greater
    # parallelism for consumption, but this will also result in more files across
    # the brokers.
    num.partitions=1
    
    # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
    # This value is recommended to be increased for installations with data dirs located in RAID array.
    num.recovery.threads.per.data.dir=1
    
    ############################# Internal Topic Settings  #############################
    # The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
    # For anything other than development testing, a value greater than 1 is recommended for to ensure availability such as 3.
    offsets.topic.replication.factor=1
    transaction.state.log.replication.factor=1
    transaction.state.log.min.isr=1
    
    ############################# Log Flush Policy #############################
    
    # Messages are immediately written to the filesystem but by default we only fsync() to sync
    # the OS cache lazily. The following configurations control the flush of data to disk.
    # There are a few important trade-offs here:
    #    1. Durability: Unflushed data may be lost if you are not using replication.
    #    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
    #    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
    # The settings below allow one to configure the flush policy to flush data after a period of time or
    # every N messages (or both). This can be done globally and overridden on a per-topic basis.
    
    # The number of messages to accept before forcing a flush of data to disk
    #log.flush.interval.messages=10000
    
    # The maximum amount of time a message can sit in a log before we force a flush
    #log.flush.interval.ms=1000
    
    ############################# Log Retention Policy #############################
    
    # The following configurations control the disposal of log segments. The policy can
    # be set to delete segments after a period of time, or after a given size has accumulated.
    # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
    # from the end of the log.
    
    # The minimum age of a log file to be eligible for deletion due to age
    log.retention.hours=168
    
    # A size-based retention policy for logs. Segments are pruned from the log unless the remaining
    # segments drop below log.retention.bytes. Functions independently of log.retention.hours.
    #log.retention.bytes=1073741824
    
    # The maximum size of a log segment file. When this size is reached a new log segment will be created.
    log.segment.bytes=1073741824
    
    # The interval at which log segments are checked to see if they can be deleted according
    # to the retention policies
    log.retention.check.interval.ms=300000
    
    ############################# Zookeeper #############################
    
    # Zookeeper connection string (see zookeeper docs for details).
    # This is a comma separated host:port pairs, each corresponding to a zk
    # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
    # You can also append an optional chroot string to the urls to specify the
    # root directory for all kafka znodes.
    zookeeper.connect=192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181
    
    # Timeout in ms for connecting to zookeeper
    zookeeper.connection.timeout.ms=6000
    
    
    ############################# Group Coordinator Settings #############################
    
    # The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
    # The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
    # The default value for this is 3 seconds.
    # We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
    # However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
    group.initial.rebalance.delay.ms=0

    配置的详细说明请参考官方文档:http://kafka.apache.org/documentation.html#brokerconfigs

    注意:按照官方文档的说法,advertised.host.name 和 advertised.port 这两个参数用于定义集群向 Producer 和 Consumer 广播的节点 host 和 port,如果不定义,会默认使用 host.name 和 port 的定义。但在实际应用中,发现如果不定义 advertised.host.name 参数,使用 Java 客户端从远端连接集群时,会发生连接超时,抛出异常:org.apache.kafka.common.errors.TimeoutException: Batch Expired

    经过过 debug 发现,连接到集群是成功的,但连接到集群后更新回来的集群 meta 信息却是错误的。metadata 中的 Cluster 信息中节点的 hostname 是一串字符,而不是实际的ip地址。这串其实是远端主机的 hostname,这说明在没有配置 advertised.host.name 的情况下,Kafka 并没有像官方文档宣称的那样改为广播我们配置的 host.name,而是广播了主机配置的 hostname 。远端的客户端并没有配置 hosts,所以自然是连接不上这个 hostname 的。要解决这一问题,把 host.name 和 advertised.host.name 都配置成绝对的 ip 地址就可以了。

    将配置后的kafka文件拷贝到slave2,slave3服务器上,并修改server.properties配置文件

    1)将配置后的kafka文件拷贝到slave2,slave3服务器上

    在slave1上执行以下命令,将拷贝kafka文件到slave2,slave3节点

    在执行拷贝之前,需要在slave2,slave3上新建文件/opt/kafka_2.11-1.1.0目录,以slave3执行为例:

    [spark@slave3 ~]$ su root
    Password: 
    [root@slave3 spark]# mkdir /opt/kafka_2.11-1.1.0
    [root@slave3 spark]# ls
    [root@slave3 spark]# cd /opt/
    hadoop-2.9.0  jdk1.8.0_171  jdk-8u171-linux-x64.tar.gz  kafka_2.11-1.1.0  scala-2.11.0  scala-2.11.0.tgz  spark-2.2.1-bin-hadoop2.7
    [root@slave3 opt]# chmod 777 /opt/kafka_2.11-1.1.0
    [root@slave3 opt]# 

    在slave1执行拷贝:

    scp -r /opt/kafka_2.11-1.1.0 spark@slave2:/opt/
    scp -r /opt/kafka_2.11-1.1.0 spark@slave3:/opt/

    2)并修改server.properties配置文件

    修改1:slave2,slave3上的/opt/kafka_2.11-1.1.0/config/server.properties

    主要修改:

    host.name=192.168.0.121
    advertised.host.name=192.168.0.121

    确保ip修改为自己的ip。

    修改2:slave2,slave3上/opt/kafka_2.11-1.1.0/config/server.properties的broker.id配置项,使得slave2的broker.id=1,slave3的broker.id=2。否则会出现下边的错误broker.id重复抛出异常,导致启动kafka失败。

    在slave1,slave2,slave3主机上分别启动 Kafka 服务

    cd /opt/kafka_2.11-1.1.0/
    bin/kafka-server-start.sh -daemon config/server.properties

    官方给出的启动方法是:

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

    1)启动失败:此时启动slave2,slave3启动一会后,自动杀掉kafka进程,从/opt/kafka_2.11-1.1.0/logs/server.log日志中查找到抛出了异常:

    [2018-07-01 07:10:45,198] INFO Creating /brokers/ids/0 (is it secure? false) (kafka.zk.KafkaZkClient)
    [2018-07-01 07:10:45,204] ERROR Error while creating ephemeral at /brokers/ids/0, node already exists and owner '144115199316656129' does not match current session '72057669184061443' (kafka.zk.KafkaZkCli
    ent$CheckedEphemeral)
    [2018-07-01 07:10:45,204] INFO Result of znode creation at /brokers/ids/0 is: NODEEXISTS (kafka.zk.KafkaZkClient)
    [2018-07-01 07:10:45,208] ERROR [KafkaServer id=0] Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
    org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists
            at org.apache.zookeeper.KeeperException.create(KeeperException.java:119)
            at kafka.zk.KafkaZkClient.checkedEphemeralCreate(KafkaZkClient.scala:1476)
            at kafka.zk.KafkaZkClient.registerBrokerInZk(KafkaZkClient.scala:84)
            at kafka.server.KafkaServer.startup(KafkaServer.scala:254)
            at kafka.server.KafkaServerStartable.startup(KafkaServerStartable.scala:38)
            at kafka.Kafka$.main(Kafka.scala:92)
            at kafka.Kafka.main(Kafka.scala)
    [2018-07-01 07:10:45,209] INFO [KafkaServer id=0] shutting down (kafka.server.KafkaServer)

    错误原因:server.properties文件中的broker.id的值,在集群环境下重复了,即,一个kafka的集群环境下,broker.id的值是不能重复的,必须唯一。就算kafka服务在不同机器上

    解决方案:修改slave2,slave3上/opt/kafka_2.11-1.1.0/config/server.properties的broker.id配置项,使得slave2的broker.id=1,slave3的broker.id=2。

    以slave1启动为例:

    [root@slave1 kafka_2.11-1.1.0]# cd /opt/kafka_2.11-1.1.0/
    [root@slave1 kafka_2.11-1.1.0]# bin/kafka-server-start.sh -daemon config/server.properties
    [root@slave1 kafka_2.11-1.1.0]# jps
    1347 QuorumPeerMain
    2493 Jps
    2431 Kafka

    常用命令

    创建分区和 topic

    1)在slave1(192.168.0.121)上创建一个名为 my-topic,拥有两个分区,两个副本的Topic

    cd /opt/kafka_2.11-1.1.0/
    bin/kafka-topics.sh --create --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --replication-factor 2 --partitions 2 --topic my-topic

    返回信息:

    [root@slave1 kafka_2.11-1.1.0]# cd /opt/kafka_2.11-1.1.0/
    [root@slave1 kafka_2.11-1.1.0]# bin/kafka-topics.sh --create --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --replication-factor 2 --partitions 2 --topic my-topic
    Created topic "my-topic".
    [root@slave1 kafka_2.11-1.1.0]# 

    2)验证:同一个名称的topic,在一个kafka的集群环境下,不能重复创建。

    在slave1(192.168.0.121)上创建一个名为 my-topic,拥有两个分区,两个副本的Topic

    [root@slave1 kafka_2.11-1.1.0]# cd /opt/kafka_2.11-1.1.0/
    [root@slave1 kafka_2.11-1.1.0]# bin/kafka-topics.sh --create --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --replication-factor 2 --partitions 2 --topic my-topic
    Error while executing topic command : Topic 'my-topic' already exists.
    [2018-07-01 07:31:23,274] ERROR org.apache.kafka.common.errors.TopicExistsException: Topic 'my-topic' already exists.
     (kafka.admin.TopicCommand$)
    [root@slave1 kafka_2.11-1.1.0]# 

    在salve2(192.168.0.122)上创建一个名为 my-topic,拥有两个分区,两个副本的Topic

    [root@slave2 kafka_2.11-1.1.0]# cd /opt/kafka_2.11-1.1.0/
    [root@slave2 kafka_2.11-1.1.0]# bin/kafka-topics.sh --create --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --replication-factor 2 --partitions 2 --topic my-topic
    Error while executing topic command : Topic 'my-topic' already exists.
    [2018-07-01 07:32:08,099] ERROR org.apache.kafka.common.errors.TopicExistsException: Topic 'my-topic' already exists.
     (kafka.admin.TopicCommand$)
    [root@slave2 kafka_2.11-1.1.0]# 

    3)查看 Topic 状态

    [root@slave2 kafka_2.11-1.1.0]# cd /opt/kafka_2.11-1.1.0/
    [root@slave2 kafka_2.11-1.1.0]# bin/kafka-topics.sh --describe --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --topic my-topic
    
    Topic:my-topic  PartitionCount:2        ReplicationFactor:2     Configs:
            Topic: my-topic Partition: 0    Leader: 2       Replicas: 2,0   Isr: 2,0
            Topic: my-topic Partition: 1    Leader: 0       Replicas: 0,2   Isr: 0,2

    4)查看当前kafka包含的topics列表

    [spark@slave1 kafka_2.11-1.1.0]$ cd /opt/kafka_2.11-1.1.0/
    [spark@slave1 kafka_2.11-1.1.0]$ bin/kafka-topics.sh --list --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 
    my-topic
    t-my
    t-order

    5)删除某个topic

    [spark@slave1 kafka_2.11-1.1.0]$ cd /opt/kafka_2.11-1.1.0/
    [spark@slave1 kafka_2.11-1.1.0]$ bin/kafka-topics.sh --delete --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --topic my-topic
    Topic my-topic is marked for deletion.
    Note: This will have no impact if delete.topic.enable is not set to true.
    [spark@slave1 kafka_2.11-1.1.0]$ bin/kafka-topics.sh --list --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181                   
    t-my
    t-order
    [spark@slave1 kafka_2.11-1.1.0]$ 

    此时,Kafka 集群的搭建已成功完成!

    其它常用命令:

    查看指定topic信息

    bin/kafka-topics.sh --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --describe --topic t-my

    控制台向topic生产数据

    bin/kafka-console-producer.sh --broker-list 192.168.0.121:9092,192.168.0.122:9092,192.168.0.123:9092 --topic t-my

    控制台消费topic的数据(从头开始消费)

    bin/kafka-console-consumer.sh --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --topic t-my --from-beginning

    控制台消费topic的数据(从头开始消费,最多消费多少)

    bin/kafka-console-consumer.sh --zookeeper 192.168.0.120:2181,192.168.0.121:2181,192.168.0.122:2181 --topic t-my --from-beginning --max-messages 3

    查看topic某分区偏移量最大(小)值

    bin/kafka-run-class.sh kafka.tools.GetOffsetShell --topic t-my --time -1 --broker-list 192.168.0.121:9092,192.168.0.122:9092,192.168.0.123:9092 --partitions 0

    注: time为-1时表示最大值,time为-2时表示最小值

    消费时对topic中的打印key,value,对key,value进行deserializer:

    > bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 
    --topic streams-wordcount-output 
    --from-beginning 
    --formatter kafka.tools.DefaultMessageFormatter 
    --property print.key=true 
    --property print.value=true 
    --property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer 
    --property value.deserializer=org.apache.kafka.common.serialization.LongDeserializer

    备注:如果不设置参数“--from-begining”就是从最新消费,如果设置参数“--from-begining”就是从最早消费。

    查看Kafka上某个topic的位移情况:

    --查看topic消费进度

    kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 192.168.0.120:9092,192.168.0.121:9092,192.168.0.122:9092 --topic s1mmetest --time -1

    -1表示查询test各个分区当前最大的消息位移值(注意,这里的位移不只是consumer端的位移,而是指消息在每个分区的位置) 

    如果你要查询曾经生产过的最大消息数,那么只运行上面这条命令然后把各个分区的结果相加就可以了。但如果你需要查询当前集群中该topic的消息数,那么还需要运行下面这条命令:

    kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 192.168.0.120:9092,192.168.0.121:9092,192.168.0.122:9092 --topic s1mmetest --time -2

    -2表示去获取当前各个分区的最小位移。之后把运行第一条命令的结果与刚刚获取的位移之和相减就是集群中该topic的当前消息总数。

    参考《https://www.cnblogs.com/RUReady/p/6479464.html》

  • 相关阅读:
    向量的内积和外积
    软件姿态解算
    PLC与外接按钮开关接线方法图解
    关于三极管处于临界饱和状态的分析
    C语言中的volatile——让我保持原样
    C语言异或运算在程序设计中的妙用
    各种转PDF
    javax.mail发送邮件功能
    MySQL总结
    idea永久破解
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/9248876.html
Copyright © 2020-2023  润新知