示例代码: 以下示例代码用于导出Bigquery 表中的数据到Cloud storage,备份数据后删除Bigquery 表中的数据。
from google.cloud import bigquery
# 导出表数据到Cloud Storage
destination_uri = 'gs://my_bucket/folder/file.csv'
dataset_ref = bigquery.DatasetReference(
'project_id', 'dataset_id')
table_ref = dataset_ref.table('table_name')
job_config = bigquery.ExtractJobConfig()
job_config.destination_format = 'CSV'
job = client.extract_table(
table_ref,
destination_uri,
job_config=job_config) # API request
job.result() # Waits for table extraction to complete.
# 删除表中的数据
table_ref = client.dataset('dataset_id').table('table_name')
client.delete_table(table_ref) # API request
在这个示例中,我们通过client.extract_table
方法将Bigquery中的表数据导出到Cloud storage。同时,我们使用client.delete_table
方法删除数据库中的表的全部数据。这两个步骤将快速备份数据并删除Bigquery中的数据。