I want to write a results of a BigQuery query, stored in txt file, into BigQuery table. I'm passing query text as a variable into below function, but get the following error:
error_info=method + ' ' + url) google.cloud.exceptions.BadRequest: 400 Required parameter is missing (POST https://www.googleapis.com/bigquery/v2/projects/myproject/jobs)
What am I missing?
The function:
from google.cloud import bigquery
import uuid
def async_query(query, dataset_id, dest_table, project_Id):
# configuration json
query_data = {
"configuration": {
"query": {
"query": query,
"defaultDataset": dataset_id,
"allowLargeResults": True,
"destinationTable": {
"projectId": project_Id,
"datasetId": dataset_id,
"tableId": dest_table
},
"createDisposition": 'CREATE_IF_NEEDED',
"writeDisposition": 'WRITE_TRUNCATE'
}
}
}
client = bigquery.Client()
query_job = client.run_async_query(str(uuid.uuid4()), query_data)
query_job.use_legacy_sql = False
query_job.begin()
wait_for_job(query_job)
# Drain the query results by requesting a page at a time.
query_results = query_job.results()
page_token = None
while True:
rows, total_rows, page_token = query_results.fetch_data(
max_results=10,
page_token=page_token)
for row in rows:
print(row)
if not page_token:
break
def wait_for_job(job):
while True:
job.reload() # Refreshes the state via a GET request.
if job.state == 'DONE':
if job.error_result:
raise RuntimeError(job.errors)
return
time.sleep(1)