Number of commits and offset in each partition of a kafka topic

后端 未结 5 1557
既然无缘
既然无缘 2020-12-28 15:07

How to find the number of commits and current offset in each partition of a known kafka topic. I am using kafka v0.8.1.1

5条回答
  •  时光取名叫无心
    2020-12-28 16:07

    This information was also helpful in creating a script to view the number of messages on a partition for a topic (from the command line). While tools like Kafka-Web-Console are nice, some of us live in a non-GUI world.

    Here is the script ... use and modify it at your own risk :-)

    #!/bin/bash
    
    topic=$1
    
    if [[ -z "${topic}" ]] ; then
    
        echo "Usage: ${0} "
        exit 1
    
    fi
    
    
    if [[ -z "${KAFKA_HOME}" ]] ; then
    
        # $KAFKA_HOME not set, using default /kafka
        KAFKA_HOME="/kafka"
    
    fi
    
    if [ ! -d ${KAFKA_HOME} ] ; then
    
        echo "\$KAFKA_HOME does not point to a valid directory [$KAFKA_HOME]"
        exit 1
    
    fi
    
    cd $KAFKA_HOME
    
    echo
    echo "Topic: ${topic}: "
    
    #
    printf "Partition  Count\n"
    printf "~~~~~~~~~~ ~~~~~~~~~~~~\n"
    
    idx=0
    for msg in `bin/kafka-run-class.sh kafka.tools.GetOffsetShell --topic ${topic} --broker-list localhost:9092 --time -1` ; do
    
        name=`echo ${msg} | awk -F ":" '{print $1}'`
        partition=`echo ${msg} | awk -F ":" '{print $2}'`
        total=`echo ${msg} | awk -F ":" '{print $3}'`
    
        printf "%10d %12d\n" ${partition} ${total}
        idx=$((idx + 1))
    
    done
    
    if [ ${idx} -eq 0 ] ; then
    
        echo "Topic name not found!"
        exit 1
    
    fi
    
    echo
    exit ${rc}
    

提交回复
热议问题