从变量将数据插入BQ表时动态处理Bigquery表架构

时间:2019-07-03 08:48:40

标签: google-cloud-platform google-bigquery google-cloud-storage python-bigquery

我正在尝试使用需要动态架构处理的python代码将数据附加到BQ表。 谁能为我提供处理上述情况的链接。

1 个答案:

答案 0 :(得分:0)

使用python客户端库的loading a .csv file into BigQuery的示例代码:

# from google.cloud import bigquery
# client = bigquery.Client()
# filename = '/path/to/file.csv'
# dataset_id = 'my_dataset'
# table_id = 'my_table'

dataset_ref = client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
job_config = bigquery.LoadJobConfig()
job_config.source_format = bigquery.SourceFormat.CSV
job_config.skip_leading_rows = 1
job_config.autodetect = True

with open(filename, "rb") as source_file:
    job = client.load_table_from_file(source_file, table_ref, job_config=job_config)

job.result()  # Waits for table load to complete.

print("Loaded {} rows into {}:{}.".format(job.output_rows, dataset_id, table_id))

还可以查看文档的此部分,以使用相同或不同的架构从源文件中进一步了解appending data into tables