pythongoogle-bigqueryselectcommand

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).


Solution

  • 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)