1 回答

TA贡献2016条经验 获得超9个赞
我从 cloud shell 进行了测试,它可以工作。
这里是cloud shell的pip依赖:google-cloud-bigquery 1.18.0
这是我的工作代码
from google.cloud import bigquery
client = bigquery.Client()
dataset_id = 'us_dataset'
dataset_ref = client.dataset(dataset_id)
job_config = bigquery.LoadJobConfig()
# I use from file path version
schema_dict = client.schema_from_json("schemaname")
print(schema_dict)
job_config.schema = schema_dict
job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
job_config.create_disposition = bigquery.CreateDisposition.CREATE_IF_NEEDED
# The source format defaults to CSV, so the line below is optional.
job_config.source_format = bigquery.SourceFormat.CSV
uri = "gs://MY_BUCKET/name.csv"
load_job = client.load_table_from_uri(
uri, dataset_ref.table("name"), job_config=job_config
) # API request
print("Starting job {}".format(load_job.job_id))
load_job.result() # Waits for table load to complete.
print("Job finished.")
destination_table = client.get_table(dataset_ref.table("name"))
print("Loaded {} rows.".format(destination_table.num_rows))
我用这个命令生成模式文件:bq show --schema us_dataset.name > schemaname
结果在这里
[{"type":"STRING","name":"name","mode":"NULLABLE"},{"type":"STRING","name":"id","mode":"NULLABLE"}]
添加回答
举报