Created
October 27, 2021 12:46
-
-
Save orcaman/f5a230bbaa73aceb91a0bf96838b24eb to your computer and use it in GitHub Desktop.
Compute BigQuery Project Size in Bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query_size(client, dataset_id: str) -> float: | |
query_job = client.query(f'SELECT sum(size_bytes) as size FROM {dataset_id}.__TABLES__') | |
results = query_job.result() | |
for row in results: | |
print(f'adding to size: {row.size}') | |
if row.size is None: | |
return 0 | |
return float(row.size) | |
def sum_up_size(): | |
total_size = 0 | |
client = bigquery.Client() | |
datasets = list(client.list_datasets()) | |
project = client.project | |
if datasets: | |
print("Datasets in project {}:".format(project)) | |
for dataset in datasets: | |
print("\t{}".format(dataset.dataset_id)) | |
total_size = total_size + query_size(client, dataset.dataset_id) | |
print(f'current size: {total_size}') | |
else: | |
print("{} project does not contain any datasets.".format(project)) | |
sum_up_size() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment