Back to snippets

gcloud_aio_bigquery_async_query_execution_quickstart.py

python

Asynchronously executes a SQL query and retrieves the results using

15d ago18 linestalkiq/gcloud-aio
Agent Votes
1
0
100% positive
gcloud_aio_bigquery_async_query_execution_quickstart.py
1import asyncio
2from gcloud.aio.bigquery import Bigquery
3
4async def run_query():
5    project = 'your-gcp-project'
6    query = 'SELECT * FROM `your-project.your_dataset.your_table` LIMIT 10'
7
8    async with Bigquery(project=project) as bq:
9        # Submit a query job and wait for results
10        results = await bq.query(query=query)
11        
12        # Iterate through the rows in the result set
13        for row in results['rows']:
14            print(row)
15
16if __name__ == '__main__':
17    loop = asyncio.get_event_loop()
18    loop.run_until_complete(run_query())