2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BigQueryのテーブルのサイズ一覧を出すスクリプト

2
Posted at

import re

from google.cloud import bigquery
from google.cloud.bigquery.table import Table

MATCH_ALL = r'.*'
MATCH_NONE = r'^$'

client = bigquery.Client(project="your-project")


def list_table_id(dataset_id, include_pattern=MATCH_ALL, exclude_pattern=MATCH_NONE):
    dataset_ref = client.dataset(dataset_id)
    tables = list(client.list_tables(dataset_ref))
    ret = []
    for table in tables:
        if re.search(include_pattern, table.table_id) and not re.search(exclude_pattern, table.table_id):
            ret.append(table.table_id)
    return ret


if __name__ == "__main__":
    datasets = list(client.list_datasets())
    for dataset in datasets:
        dataset_id = dataset.dataset_id
        try:
            for table_id in list_table_id(dataset_id):
                try:
                    dataset_ref = client.dataset(dataset_id)
                    table_ref = dataset_ref.table(table_id)
                    table: Table = client.get_table(table_ref)
                    print(f"{dataset_id}\t{table_id}\t{table.num_bytes}")
                except Exception as e:
                    print(f"{dataset_id}\t{table_id}\terror\t{e}")
        except Exception as e:
            print(f"{dataset_id}\terror\t{e}")
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?