|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | + |
| 17 | + |
| 18 | +def update_table_expiration(table_id, expiration): |
| 19 | + orig_table_id = table_id |
| 20 | + orig_expiration = expiration |
| 21 | + |
| 22 | + # [START bigquery_update_table_expiration] |
| 23 | + from google.cloud import bigquery |
| 24 | + |
| 25 | + client = bigquery.Client() |
| 26 | + |
| 27 | + # TODO(dev): Change table_id to the full name of the table you want to update. |
| 28 | + table_id = "your-project.your_dataset.your_table_name" |
| 29 | + |
| 30 | + # TODO(dev): Set table to expire for desired days days from now. |
| 31 | + expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( |
| 32 | + days=5 |
| 33 | + ) |
| 34 | + # [END bigquery_update_table_expiration] |
| 35 | + |
| 36 | + table_id = orig_table_id |
| 37 | + expiration = orig_expiration |
| 38 | + |
| 39 | + # [START bigquery_update_table_expiration] |
| 40 | + table = client.get_table(table_id) # Make an API request. |
| 41 | + table.expires = expiration |
| 42 | + table = client.update_table(table, ["expires"]) # API request |
| 43 | + |
| 44 | + print(f"Updated {table_id}, expires {table.expires}.") |
| 45 | + # [END bigquery_update_table_expiration] |
0 commit comments