Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
Showing results for 
Search instead for 
Did you mean: 

How to get the count of dataframe rows when reading through spark.readstream using batch jobs?

Contributor III

I am trying to read messages from kafka topic using spark.readstream, I am using the following code to read it.


df = spark.readStream


.option("kafka.bootstrap.servers", "192.1xx.1.1xx:9xx")

.option("subscribe", "json_topic")

.option("startingOffsets", "earliest") // From starting


Now i just want to get the count of df like we can get from df.count() method when we use

I need to place some conditions if i didn't get any messages from the Topic. I am running this code as a batch and its a business requirement, i don't want to use

Please suggest what would be the best approach to get the count.

Thanks in advance!


Esteemed Contributor

You can try this approach:

ReadStream is running a thread in background so there's no easy way like

Thanks for the suggestion. I will check.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!