通过Python API创建Bigquery表

MFR*_*MFR 5 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()
Run Code Online (Sandbox Code Playgroud)

我一直收到这个错误

AttributeError:'TableReference'对象没有属性'create'

有谁有想法吗?

Gra*_*ley 7

你回来了一个TableReference对象,而不是Table你最后一行的第二行(table = dataset.table("mytable")).你需要这样做:

[..]
table_ref = dataset.table('my_table')
table = bigquery.Table(table_ref, schema=SCHEMA)
table = client.create_table(table)
[..]
Run Code Online (Sandbox Code Playgroud)

看到这里.


小智 7

类似的答案,有一个例子schema和另一个来源

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)
)
Run Code Online (Sandbox Code Playgroud)