我正在试图找出如何列出Google Big Query中所有项目中所有表格的所有大小。也许它将是多个表的SQL联盟。虽然,我在这里看很多表,所以我想要一些自动化解决方案。我可以使用R代码来执行此任务。或者我甚至冷使用Python来做到这一点。如果此处的任何人都有解决方案来列出一些指标,主要是每个对象(表格)的大小,以及其他相关指标,请在此处分享。非常感谢!
Python中的这个示例列出了所有项目中的所有表及其大小(以字节为单位)。您可以将其作为示例来构建适合您的用例的脚本:
from google.cloud import bigquery
from google.cloud.bigquery import Dataset
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
# credentials to list project
credentials = GoogleCredentials.get_application_default()
service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)
# list project
request = service.projects().list()
response = request.execute()
# Main loop for project
for project in response.get('projects', []):
client = bigquery.Client(project['projectId']) # Start the client in the right project
# list dataset
datasets = list(client.list_datasets())
if datasets: # If there is some BQ dataset
print('Datasets in project {}:'.format(project['name']))
# Second loop to list the tables in the dataset
for dataset in datasets:
print(' - {}'.format(dataset.dataset_id))
get_size = client.query("select table_id, size_bytes as size from "+dataset.dataset_id+".__TABLES__") # This query retrieve all the tables in the dataset and the size in bytes. It can be modified to get more fields.
tables = get_size.result() # Get the result
# Third loop to list the tables and print the result
for table in tables:
print('\t{} size: {}'.format(table.table_id,table.size))
参考:
列出项目: https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#embedded-explorer
要列出数据集: https://cloud.google.com/bigquery/docs/datasets#bigquery-list-datasets-python
选项1
截至今天的当前选项是使用Google API获取项目/数据集/表信息并将其存储在本地表中。既然你提到你有很多数据集和表,我建议你使用无服务器方法来实现流程的可扩展性和速度
选项2
BigQuery现在提供他们的Beta program access to information schema,检查它可能会节省您的时间和精力
select * from `DATASET.INFORMATION_SCHEMA.TABLES`
要么
select * from `DATASET.INFORMATION_SCHEMA.COLUMNS`
选项3。
您可以查询__TABLES__
以获取表信息
select * from `project.__TABLES__`