安装客户端库

pip install google-cloud-bigquery

示例代码

from google.cloud import bigquery

# 初始化客户端
client = bigquery.Client()

# 表引用
table_ref = client.dataset('dataset_id').table('table_id')

# 获取当前表结构
table = client.get_table(table_ref)
original_schema = table.schema

# 添加新列
new_schema = original_schema[:]  # 复制当前表结构
new_schema.append(bigquery.SchemaField("new_column_name", "STRING"))

# 更新表结构
table.schema = new_schema
table = client.update_table(table, ["schema"])

print(f"表结构已更新: {table.schema}")

  

 posted on 2025-02-08 10:38  boye169  阅读(32)  评论(0)    收藏  举报