I'm trying to automate JSON data upload to BigQuery using two cloud functions which both deploys successfully and a cloud scheduler which runs successfully. After running the cloud scheduler, data gets uploaded to my cloud storage, but then it doesn't get uploaded to BigQuery.
Below are my code and JSON data:
# function 1 triggered by http
def function(request):
url = "https://api...."
headers = {"Content-Type" : "application/json",
"Authorization" : "..."}
response = requests.get(url, headers=headers)
json_data = response.json()
pretty_json = json.dumps(json_data, indent=4, sort_keys=True)
storage_client = storage.Client()
bucket = storage_client.bucket("bucket_name")
blob = bucket.blob("blob_name")
blob.upload_from_string(pretty_json)
# function 2 triggered by cloud storage -> event type finalize/create
def function_2(data, context):
client = bigquery.Client()
table_id = "booming-post-322920:dataset_name.table_name"
job_config = bigquery.LoadJobConfig()
job_config.schema=[
bigquery.SchemaField("order_items", "INTEGER"),
bigquery.SchemaField("created_at", "TIMESTAMP"),
.....,
bigquery.SchemaField("updated_at", "TIMESTAMP")
]
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
uri = 'gs://bucket_name/blob_name'
load_job = client.load_table_from_uri(
uri,
table_id,
location="US",
job_config=job_config
)
load_job.result()
This is what my JSON data pretty_json
looks like:
{
"records": [
{
"active": null,
"approved": null,
"buyer": [
1
],
"cancel_reason": null,
"cancelled": null,
"chef": [
1
],
"completed": null,
"created_at": "2021-07-15T17:44:31.064Z",
...
Please advise.