【问题标题】:Get list of tables in BigQuery dataset using python and BigQuery API使用 python 和 BigQuery API 获取 BigQuery 数据集中的表列表
【发布时间】:2019-06-18 20:52:20
【问题描述】:

如何查询 BigQuery 数据集并获取数据集中所有表的列表?据我所知,我只能使用 BigQuery API,但我无法进行身份验证,尽管传递了 API 密钥。

    url = f"https://bigquery.googleapis.com/bigquery/v2/projects/{params['project_id']}/datasets/{params['dataset_id']}/tables?key={params['api_key']}"
    response = requests.get(url)
    data = response.json()
    pprint.pprint(data)

【问题讨论】:

标签: python google-cloud-platform google-bigquery


【解决方案1】:

正如米哈伊尔所说,它在文档中进行了解释。答案如下:

from google.cloud import bigquery

# TODO(developer): Construct a BigQuery client object.
# client = bigquery.Client()

# TODO(developer): Set dataset_id to the ID of the dataset that contains
#                  the tables you are listing.
# dataset_id = 'your-project.your_dataset'

tables = client.list_tables(dataset_id)

print("Tables contained in '{}':".format(dataset_id))
for table in tables:
    print("{}.{}.{}".format(table.project, table.dataset_id, table.table_id))

【讨论】:

    【解决方案2】:

    我可以扩展约翰的答案并添加:

    from google.cloud import bigquery
    client = bigquery.Client()
    datasets = list(client.list_datasets())  # Make an API request.
    project = client.project
    
    if datasets:
        print("Datasets in project {}:".format(project))
        for dataset in datasets:
            print("\t{}".format(dataset.dataset_id))
            tables = client.list_tables(dataset.dataset_id)
    
            print("Tables contained in '{}':".format(dataset.dataset_id))
            for table in tables:
                print("{}.{}.{}".format(table.project, table.dataset_id, table.table_id))
    
    else:
        print("{} project does not contain any datasets.".format(project))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多