Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka 计算存储在卡夫卡主题中的消息数_Apache Kafka_Kafka Consumer Api_Jms Topic - Fatal编程技术网

Apache kafka 计算存储在卡夫卡主题中的消息数

Apache kafka 计算存储在卡夫卡主题中的消息数,apache-kafka,kafka-consumer-api,jms-topic,Apache Kafka,Kafka Consumer Api,Jms Topic,我使用的是0.9.0.0版本的Kafka,我希望在不使用管理脚本Kafka-console-consumer.sh的情况下计算主题中的邮件数 我试过答案中的所有命令 但没有一个会产生结果。 有人能帮我吗?您可以尝试执行以下命令: bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092,localhost:9093,localhost:9094 --topic test-topic --time

我使用的是0.9.0.0版本的Kafka,我希望在不使用管理脚本Kafka-console-consumer.sh的情况下计算主题中的邮件数

我试过答案中的所有命令 但没有一个会产生结果。
有人能帮我吗?

您可以尝试执行以下命令:

bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092,localhost:9093,localhost:9094 --topic test-topic --time -1
然后,将每个分区的所有计数相加

更新:Java实现

Properties props = new Properties();
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
......
try (final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
    consumer.subscribe(Arrays.asList("your_topic"));
    Set<TopicPartition> assignment;
    while ((assignment = consumer.assignment()).isEmpty()) {
        consumer.poll(Duration.ofMillis(100));
    }
    final Map<TopicPartition, Long> endOffsets = consumer.endOffsets(assignment);
    final Map<TopicPartition, Long> beginningOffsets = consumer.beginningOffsets(assignment);
    assert (endOffsets.size() == beginningOffsets.size());
    assert (endOffsets.keySet().equals(beginningOffsets.keySet()));

    Long totalCount = beginningOffsets.entrySet().stream().mapToLong(entry -> {
            TopicPartition tp = entry.getKey();
            Long beginningOffset = entry.getValue();
            Long endOffset = endOffsets.get(tp);
            return endOffset - beginningOffset;
        }).sum();
    System.out.println(totalCount);
}
Properties=newproperties();
put(ConsumerConfig.ENABLE\u AUTO\u COMMIT\u CONFIG,false);
......
试一试(最终卡夫卡消费者=新卡夫卡消费者(道具)){
consumer.subscribe(Arrays.asList(“您的_主题”);
设定作业;
while((assignment=consumer.assignment()).isEmpty()){
消费者调查(持续时间为百万(100));
}
最终地图内偏移=消费者。内偏移(分配);
最终地图开始偏移=消费者。开始偏移(分配);
断言(endOffsets.size()==beginingoffsets.size());
断言(endOffsets.keySet().equals(beginingoffsets.keySet());
Long totalCount=BeginingOffsets.entrySet().stream().mapToLong(条目->{
TopicPartition tp=entry.getKey();
Long BeginingOffset=entry.getValue();
Long endOffset=endOffset.get(tp);
返回endOffset-开始偏移;
}).sum();
系统输出打印项次(总计数);
}

您可以使用以下方法汇总所有计数:

.../bin/kafka-run-class kafka.tools.GetOffsetShell --broker-list <<broker_1>>:9092,<<broker_2:9092>>... --topic <<your_topic_name>> --time -1 | while IFS=: read topic_name partition_id number; do echo "$number"; done | paste -sd+ - | bc
../bin/kafka运行类kafka.tools.GetOffsetShell--代理列表:9092--topic--time-1 |而IFS=:读取topic_name partition_id number;不回显“$number”;完成|粘贴-sd+-| bc

从技术上讲,您可以简单地使用该主题中的所有消息并计算它们:

示例:

kafka-run-class.sh kafka.tools.SimpleConsumerShell --broker-list localhost:9092 --topic XYZ --partition 0*
但是,
kafka.tools.GetOffsetShell
方法将为您提供偏移量,而不是主题中的实际消息数。这意味着,如果主题被压缩,那么如果您通过使用消息或读取偏移量来计算消息,您将得到两个不同的数字


主题压缩:

您还可以使用awk和一个简单的循环来完成此操作

for i in `kafka-run-class kafka.tools.GetOffsetShell --broker-list broker:9092 --time -1 --topic topic_name| awk -F : '{print $3}'`; do sum=$(($sum+$i)); done

获取主题中的记录数的步骤

brokers="<broker1:port>"
topic=<topic-name>
sum_1=$(/usr/hdp/current/kafka-broker/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list $brokers --topic $topic --time -1 | grep -e ':[[:digit:]]*:' | awk -F  ":" '{sum += $3} END {print sum}')
sum_2=$(/usr/hdp/current/kafka-broker/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list $brokers --topic $topic --time -2 | grep -e ':[[:digit:]]*:' | awk -F  ":" '{sum += $3} END {print sum}')
echo "Number of records in topic ${topic}: "$((sum_1 - sum_2))
brokers=“”
话题=
sum|u 1=$(/usr/hdp/current/kafka broker/bin/kafka-run-class.sh kafka.tools.GetOffsetShell——经纪人列表$brokers——主题$topic——time-1 | grep-e’:[:digit:]*:'| awk-F:“{sum+=$3}END{print sum}”)
sum|u 2=$(/usr/hdp/current/kafka broker/bin/kafka-run-class.sh kafka.tools.GetOffsetShell——经纪人列表$brokers——主题$topic——time-2 | grep-e’:[[:digit:]*:'| awk-F:“{sum+=$3}END{print sum}”)
echo“主题${topic}中的记录数:$((sum_1-sum_2))

如果你不想卷入围绕“原创”卡夫卡剧本的纷争,还有一个

基本思想是

  • 使用每个分区的最后一条消息,然后
  • 将偏移相加(校正基于零的偏移)
让我们开发这个

kafkacat-C-b-t-o-1-f'%p\t%o\n'
这将输出如下内容(加上stderr上的“到达分区结束”通知):

现在,
kafkacat
没有终止,而是一直在等待新消息。我们可以通过添加超时(选择一个足够大的值,以便获得给定环境中的所有分区)来避免这种情况:

超时--保留状态1卡夫卡
现在我们可以继续添加第二列(+1),但如果在该超时间隔期间有新消息,我们可能会得到如下结果:

所以我们必须考虑到这一点,这很容易用一点
awk

超时--保留状态1 kafkacat 2>/dev/null\
|awk“{lastOffsets[$1]=$2}END{count=0;对于(lastOffsets中的i){count+=lastOffsets[i]+1};打印计数}”
请注意,我们如何使用(哈希)映射来记住每个分区最后看到的偏移量,直到超时触发,然后在数组上循环以计算总和。

我们可以使用命令来计算主题中的消息数。命令如下。请注意,即使您的消息是多行的,此命令也会起作用

kafkacat -b <broker_1_ip:port>,<broker_2_ip:port> -t <topic-name> -C -e -q -f 'Offset: %o\n' | wc -l
kafkacat-b,-t-C-e-q-f'偏移量:%o\n'| wc-l

从控制台上打印的数字中减去1,这就是答案。

您是否希望它也适用于压缩主题,因为这样可以消除一系列选项,例如比较开始偏移量和持续偏移量。有关使用Java客户端的解决方案,请参阅我的答案。从Kafka中的主题中读取可能无法计数(数百万?)的消息(在清除之前是持久的-不像JMS-在读取之前是持久的)除非时间不是相对的,否则不可行。哪个计数可能更高,偏移量或消耗的邮件数?我猜是第一个?谢谢!简单一点的求和:kafka-run-class.sh kafka.tools.GetOffsetShell--代理列表$kafka_集群_主机--topic$topic_NAME--time-1 | tr:““| awk'{sum+=$3}END{print sum}”“@ozma而不是
tr
你也可以使用
awk-F:
:你应该计算最新偏移量和最早偏移量之间的差值之和。(--time-2)param给出最早的偏移量。你能提供同样的java实现吗?
0    77
1    75
2    78
1    76
kafkacat -b <broker_1_ip:port>,<broker_2_ip:port> -t <topic-name> -C -e -q -f 'Offset: %o\n' | wc -l