Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print count table query of bigquery in python?

I am trying to print count of rows available in table of bigquery in python,I have written below code:

from google.cloud import bigquery def main():
    myquery = "select count(*) from `myproject.mydataset.mytable`"
    client = bigquery.Client()
    job = client.query(myquery)
    result = job.result()
    print("Total rows available: ",result)

When I execute above code it gives me output as

"Total rows available: google.cloud.bigquery.table.RowIterator object at 0x00000247B65F29E8>".

It means I am getting object value as my output. But I want to print actual count of rows available in my table(query result).

like image 745
Kaustubh Ghole Avatar asked Oct 14 '25 15:10

Kaustubh Ghole


2 Answers

RowIterator has an attribute called total_rows.

Simply change your last statement to

 print("Total rows available: ", result.total_rows)
like image 90
Nico Griffioen Avatar answered Oct 17 '25 04:10

Nico Griffioen


Try changing your query to

myquery = "select count(*) size from `myproject.mydataset.mytable`"
client = bigquery.Client()
job = client.query(myquery)
result = job.result()
for row in results:
    print("Total rows available: ",row.size)
like image 45
Bobbylank Avatar answered Oct 17 '25 05:10

Bobbylank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!