Created
November 18, 2021 12:59
-
-
Save 4sushi/79799e5e5a889c6363b0df1a4daae006 to your computer and use it in GitHub Desktop.
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
from google.cloud import bigquery | |
from datetime import date | |
# You have to create a dataset in your BQ project before running this script, here the name is "archive" | |
ARCHIVE_DATASET = 'archive' | |
def archive_table(bq, dataset, table): | |
d = date.today() | |
table_src_id = '{}.{}.{}'.format(bq.project, dataset, table) | |
table_dest_id = '{}.{}.{}-{}-{:%Y-%m-%d}'.format(bq.project, ARCHIVE_DATASET, dataset, table, d) | |
bq.copy_table(table_src_id, table_dest_id) | |
bq.delete_table(table_src_id) | |
print('archive table "{}" to "{}"'.format(table_src_id, table_dest_id)) | |
def archive_dataset(bq, dataset): | |
for table in bq.list_tables(dataset): | |
archive_table(bq, dataset, table.table_id) | |
bq.delete_dataset(dataset) | |
bq = bigquery.Client() | |
archive_dataset(bq, 'dataset_name') | |
archive_table(bq, 'dataset_name2', 'table_name') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment