您可以使用 BigQuery 的 API 或者客户端库来获取 BigQuery 长期存储表的列表。以下是使用 Python 客户端库的示例代码:
from google.cloud import bigquery
def list_longterm_storage_tables(project_id, dataset_id):
client = bigquery.Client(project=project_id)
dataset_ref = client.dataset(dataset_id)
tables = client.list_tables(dataset_ref)
longterm_storage_tables = []
for table in tables:
table_ref = dataset_ref.table(table.table_id)
table = client.get_table(table_ref)
if table.time_partitioning and table.time_partitioning.type == 'DAY':
longterm_storage_tables.append(table.table_id)
return longterm_storage_tables
# 使用示例
project_id = 'your-project-id'
dataset_id = 'your-dataset-id'
tables = list_longterm_storage_tables(project_id, dataset_id)
for table in tables:
print(table)
这个示例代码中,首先使用 bigquery.Client
创建一个 BigQuery 客户端实例,然后使用 client.list_tables
方法获取指定数据集中的所有表。接下来,通过检查每个表的 time_partitioning
属性,筛选出长期存储表,并将其表名添加到 longterm_storage_tables
列表中。最后,返回 longterm_storage_tables
列表。
您需要将 your-project-id
和 your-dataset-id
替换为您自己的项目 ID 和数据集 ID。