You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过BigQuery获取多个数据集的表及最后修改时间

获取BigQuery多个数据集的表信息及最后修改时间

方法1:用通配符批量查询

直接通过通配符匹配项目下的多个数据集,无需逐个指定:

query = """SELECT 
  dataset_id,
  table_id,
  TIMESTAMP_MILLIS(creation_time) AS creation_time,
  TIMESTAMP_MILLIS(last_modified_time) as last_modified_time
FROM
  `your-project-id.*.__TABLES__`;"""
  • 替换your-project-id为实际项目ID,*会匹配该项目下所有数据集
  • 若要筛选特定前缀的数据集,比如sales_开头的,可写成your-project-id.sales_*.__TABLES__

方法2:使用INFORMATION_SCHEMA(推荐)

通过标准元数据视图查询,兼容性更强,还能灵活过滤表类型:

query = """SELECT
  table_catalog AS project_id,
  table_schema AS dataset_id,
  table_name AS table_id,
  creation_time,
  last_modified_time
FROM
  `region-us`.INFORMATION_SCHEMA.TABLES
WHERE
  table_catalog = 'your-project-id'
  AND table_type = 'BASE TABLE'; -- 可选,仅查询基础表,排除视图/外部表"""
  • 替换region-us为你的BigQuery区域,your-project-id为项目ID

方法3:脚本遍历数据集(复杂场景)

如果需要按自定义规则筛选数据集(比如权限、名称关键词),可以用脚本先获取数据集列表再逐个查询:

from google.cloud import bigquery

client = bigquery.Client(project="your-project-id")
all_table_info = []

# 获取项目下所有数据集
for dataset in client.list_datasets():
    dataset_ref = client.dataset(dataset.dataset_id)
    # 查询当前数据集的表信息
    query = f"""SELECT
      dataset_id,
      table_id,
      TIMESTAMP_MILLIS(creation_time) AS creation_time,
      TIMESTAMP_MILLIS(last_modified_time) as last_modified_time
    FROM
      `{dataset_ref.project}.{dataset_ref.dataset_id}.__TABLES__`;"""
    results = client.query(query).result()
    all_table_info.extend([dict(row) for row in results])

# 输出或处理结果
for table in all_table_info:
    print(table)

内容的提问来源于stack exchange,提问作者Jerry Vfc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 14:45:39