Reputation: 577
I have a client API in python which executes BigQuery job to trigger a query and write query result into respective BigQuery table. How to determine whether that query result returns zero records at any execution or not?
Python code:
def main(request):
query = "select * from `myproject.mydataset.mytable`"
client = bigquery.Client()
job_config = bigquery.QueryJobConfig()
dest_dataset = client.dataset(destination_dataset, destination_project)
dest_table = dest_dataset.table(destination_table)
job_config.destination = dest_table
job_config.create_disposition = 'CREATE_IF_NEEDED'
job_config.write_disposition = 'WRITE_APPEND'
job = client.query(query, location='US', job_config=job_config)
job.result()
I want If query result is having none record then it should print some message for me. Can anybody suggest how to get this done.
Upvotes: 5
Views: 11595
Reputation: 15018
QueryJob.result()
returns a RowIterator
, which has a property called total_rows
.
So, something like:
result = job.result()
if result.total_rows == 0:
print('no results')
RowIterator.total_rows
Updated per @Dom Zippilli's comment: . total_rows
is what you're looking for.
Upvotes: 11