如何查询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)
答案 0 :(得分:1)
正如米哈伊尔(Mikhail)所说,其在文档中有所解释。答案是这样:
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))
答案 1 :(得分:0)
我可以扩展John的答案并添加:
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))