Currently I am retrieving data in json format from a GraphQL API and inserting it into a BigQuery table. Every time this code executes, then it deletes the table to guarantee the insertion of data in the table – if the schema of the API changes then a new table will be created with the new schema. Below is the code:
import json
import requests
import pandas as pd
def main():
url = apigraphql/v1/content
headers = {'key': 'xxxx', 'Content-Type': 'application/json'}
response = requests.request("POST", url, headers=headers)
data = json.loads(response.text)
table = 'myproject.mydataset.mytable'
table_exists = True
try:
client.get_table(table)
except:
table_exists = False
if table_exists:
client.delete_table(table)
flattened = flatten(data)
df = pd.DataFrame(flattened)
client = Client()
job_config = bigquery.LoadJobConfig()
job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
job = client.load_table_from_dataframe(df, table, job_config=job_config)
job.result()
The code works since every time I change the schema of the response then the new table will have the new structure, but I am wondering if there’s a better way to replace the try-except clause. Feel free to improve this question or ask for further clarification 🙂