【问题标题】:Creating a Bigquery table by Python API通过 Python API 创建 Bigquery 表
【发布时间】:2018-05-29 07:04:06
【问题描述】:
我正在尝试使用 Python API 创建一个 Bigquery 表。
from google.cloud import bigquery
bigquery_client = bigquery.Client(project="myproject")
dataset = bigquery_client.dataset("mydataset")
table = dataset.table("mytable")
table.create()
我一直收到这个错误
AttributeError: 'TableReference' 对象没有属性 'create'
有人知道吗?
【问题讨论】:
标签:
python
google-bigquery
【解决方案1】:
您将返回一个 TableReference 对象,而不是您的倒数第二行 (table = dataset.table("mytable")) 上的 Table。你需要这样做:
[..]
table_ref = dataset.table('my_table')
table = bigquery.Table(table_ref, schema=SCHEMA)
table = client.create_table(table)
[..]
见here。
【解决方案2】:
类似的答案,以schema 和另一个source 为例
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"
schema = [
bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"),
bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"),
]
table = bigquery.Table(table_id, schema=schema)
table = client.create_table(table) # Make an API request.
print(
"Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id)
)