要加载JSON数据到BigQuery中,需要进行以下步骤:
将JSON数据转换为可用的表格格式,例如CSV或Avro。
将转换后的数据上传到Google Cloud Storage。
创建一个表格定义,包括数据集和表格ID、架构和文件位置(Cloud Storage URI)。
在BigQuery中运行一个"Load"或"Insert"命令,来将数据从Cloud Storage加载到表格中。
以下是将JSON文件加载到BigQuery中的Python代码示例:
from google.cloud import bigquery
# set up BigQuery client
client = bigquery.Client()
# set up table reference
table_ref = client.dataset('my_dataset').table('my_table')
# set up job configuration
job_config = bigquery.LoadJobConfig()
job_config.autodetect = True # let BigQuery detect schema
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
# start job to load data
with open('/path/to/my/json/file.json', 'rb') as source_file:
job = client.load_table_from_file(source_file, table_ref, job_config=job_config)
# wait for job to complete
job.result()