Python API创建Bigquery表

时间:2017-12-15 05:07:08

标签: python google-bigquery

我正在尝试使用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'

有没有人有任何想法?

2 个答案:

答案 0 :(得分:7)

您在第2行(TableReference)上找回了Table个对象,而不是table = dataset.table("mytable")。你需要这样做:

[..]
table_ref = dataset.table('my_table')
table = bigquery.Table(table_ref, schema=SCHEMA)
table = client.create_table(table)
[..]

请参阅here

答案 1 :(得分:1)

相似的答案,以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)
)