下面是使用BigQuery Python客户端创建查询表的示例代码:
from google.cloud import bigquery
# 初始化BigQuery客户端
client = bigquery.Client()
# 定义查询表的架构
schema = [
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("age", "INTEGER"),
bigquery.SchemaField("city", "STRING"),
]
# 创建表描述对象
table_ref = client.dataset("your_dataset_id").table("your_table_id")
table = bigquery.Table(table_ref, schema=schema)
# 设置表描述
table.description = "This table contains information about people's name, age, and city."
# 创建查询表
client.create_table(table)
在上述示例中,你需要将your_dataset_id
和your_table_id
替换为实际的数据集ID和表ID。然后,通过定义一个包含字段名称和字段类型的架构来创建bigquery.SchemaField
对象,并将其传递给bigquery.Table
对象的schema
参数。接下来,使用table.description
属性来设置表的描述,最后通过client.create_table
方法创建查询表。
请确保已经在你的Python环境中安装了google-cloud-bigquery
库,你可以使用以下命令安装:
pip install google-cloud-bigquery
注意:在运行代码之前,你需要先进行身份验证,以便使用你的Google Cloud账号访问BigQuery服务。