如果在上传CSV文件时出现了“创建表失败”的错误消息,则可能是由于上传的CSV文件中存在以下问题:
您可以使用以下代码示例来解决此问题:
from google.cloud import bigquery
# 上传CSV文件的路径
csv_file = "path/to/csv/file.csv"
# BigQuery表的名称
table_name = "my-bigquery-table"
# 设置BigQuery表的schema
schema = [
bigquery.SchemaField("Column1", "STRING"),
bigquery.SchemaField("Column2", "INTEGER"),
bigquery.SchemaField("Column3", "FLOAT"),
]
# 将CSV文件上传到BigQuery
client = bigquery.Client()
table_id = "{project_id}.{dataset_id}.{table_name}".format(
project_id=client.project, dataset_id="my-dataset", table_name=table_name
)
job_config = bigquery.LoadJobConfig(schema=schema, skip_leading_rows=1)
with open(csv_file, "rb") as source_file:
job = client.load_table_from_file(source_file, table_id, job_config=job_config)
job.result() # 等待上传完成
# 输出上传成功的消息
print("File {} uploaded to table {}.".format(csv_file, table_id))