# How do I count the number of messages in a Kafka topic?

**URL:** <https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314>\
**Category:** Clients\
**Created:** [3 December 2020 14:53 UTC](https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314 "2020-12-03T14:53:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)\
**Post date:** [3 December 2020 14:53 UTC](https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314/1 "2020-12-03T14:53:51Z")

</div>

I want to know how many messages are in the topic, how can I do this?

---

<div class="post-metadata">

**Author:** ![shree](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@shree](https://forum.confluent.io/u/shree)\
**Post date:** [16 February 2021 08:46 UTC](https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314/2 "2021-02-16T08:46:16Z")

</div>

@rmoff don’t we all want to know the answer to this 😄  
But I am certainly interested to know what others are doing to handle this.

---

<div class="post-metadata">

**Author:** ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)\
**Post date:** [16 February 2021 09:04 UTC](https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314/3 "2021-02-16T09:04:41Z")

</div>

Here are a few options: [Counting the number of messages in a Kafka topic](https://rmoff.net/2020/09/08/counting-the-number-of-messages-in-a-kafka-topic/) 🙂

Also check out this cool vid from @gAmUssA

[![](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/5653dec0a1b0c3060432e5ed3afc67f6dba5ae45.jpeg "How to Count Messages in a Kafka Topic | Kafka Tutorial") ](https://www.youtube.com/watch?v=g-nYBtnHz9Y)

---

<div class="post-metadata">

**Author:** ![riferrei](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/riferrei/32/1848_2.png) [@riferrei](https://forum.confluent.io/u/riferrei)\
**Post date:** [18 February 2021 21:28 UTC](https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314/5 "2021-02-18T21:28:57Z")

</div>

Another option is using the **kafka.tools.GetOffsetShell** command:

`bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list <BROKER_LIST> --topic <TOPIC_NAME> --time -1 --offsets 1 | awk -F ":" '{sum += $3} END {print sum}'`

[![](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/2X/5/5491c4b34126ee661b1c6c7210714845509a60d8.jpeg "Count the Number of Records in a Kafka Topic") ](https://www.youtube.com/watch?v=Y511Yo6xa6I)

The source-code of this command is not very intuitive (Scala 🤮) but it may provide an idea about how it works:

> <https://github.com/apache/kafka/blob/b58b944e70780029806a6004114bba43ce5b9483/core/src/main/scala/kafka/tools/GetOffsetShell.scala>

---

<div class="post-metadata">

**Author:** ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)\
**Post date:** [25 February 2021 21:29 UTC](https://forum.confluent.io/t/how-do-i-count-the-number-of-messages-in-a-kafka-topic/314/6 "2021-02-25T21:29:05Z")

</div>

Closed on 25 February 2021 21:29 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
