使用 python 和 BigQuery API 获取 BigQuery 数据集中的表列表

Joh*_*ohn 3 python google-bigquery google-cloud-platform

如何查询 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)

Run Code Online (Sandbox Code Playgroud)

Joh*_*ohn 11

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

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))
Run Code Online (Sandbox Code Playgroud)