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"}]
添加回答
舉報