Created
July 4, 2023 09:48
-
-
Save Sang2306/088b91c4f21a561c5e752201263bcbfc to your computer and use it in GitHub Desktop.
Python example for managing tables on BigQuery
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
| # pip install google-cloud-bigquery~=2.16.1 | |
| from google.cloud import bigquery | |
| client = bigquery.Client.from_service_account_json("<full/path/to/service_account.json>") | |
| # If your account has create/delete permissions, you can simply | |
| # client = bigquery.Client(project="<project-id>") | |
| def copy_table(source_table_ref, destination_table_ref): | |
| # Fetch the schema from the source table | |
| source_table = client.get_table(source_table_ref) | |
| schema = source_table.schema | |
| # Start the copy job | |
| copy_job = client.copy_table(source_table_ref, destination_table_ref) | |
| # Wait for the job to complete | |
| copy_job.result() | |
| print(f"Table created successfully.") | |
| def delete_table(table_id): | |
| client.delete_table(table_id) | |
| print("Deleted the table successfully") | |
| def create_partitioned_table(dataset_id, table_id): | |
| # Construct the destination table reference | |
| destination_table_ref = client.dataset(dataset_id).table(table_id) | |
| # Define the table schema | |
| schema = [] | |
| # Create the table configuration with partitioning and schema | |
| table_config = bigquery.Table(table_ref=destination_table_ref, schema=schema) | |
| table_config.time_partitioning = bigquery.TimePartitioning( | |
| type_=bigquery.TimePartitioningType.DAY | |
| ) | |
| # Create the table | |
| client.create_table(table_config) | |
| print(f"Partitioned table {dataset_id}.{table_id} created successfully.") | |
| if __name__ == "__main__": | |
| # Construct the source and destination table references | |
| _source_table_ref = '<project-id>.<dataset-id>.<source_table>' | |
| _destination_table_ref = '<project-id>.<dataset-id>.<source_table>_copy' | |
| # Call the function to create the copy | |
| # copy_table(_source_table_ref, _destination_table_ref) | |
| # Delete table | |
| # delete_table(_source_table_ref) | |
| # Create table with _partitiontime | |
| # dataset_id = '<dataset-id>' | |
| # table_id = '<table-id>' | |
| # create_partitioned_table(dataset_id, table_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment