要使用BigQuery Python API中的copy_table
方法复制表结构和数据,您可以按照以下步骤进行操作:
from google.cloud import bigquery
client = bigquery.Client()
source_project_id = "your-source-project-id"
source_dataset_id = "your-source-dataset-id"
source_table_id = "your-source-table-id"
destination_project_id = "your-destination-project-id"
destination_dataset_id = "your-destination-dataset-id"
destination_table_id = "your-destination-table-id"
get_table
方法获取源表的元数据:source_table = client.get_table(
f"{source_project_id}.{source_dataset_id}.{source_table_id}"
)
create_table
方法创建目标表,并使用源表的模式(schema):destination_table = bigquery.Table(f"{destination_project_id}.{destination_dataset_id}.{destination_table_id}")
destination_table.schema = source_table.schema
destination_table = client.create_table(destination_table)
copy_table
方法复制数据:job_config = bigquery.CopyJobConfig()
job = client.copy_table(source_table, destination_table, job_config=job_config)
job.result() # 等待复制任务完成
完整代码示例:
from google.cloud import bigquery
client = bigquery.Client()
source_project_id = "your-source-project-id"
source_dataset_id = "your-source-dataset-id"
source_table_id = "your-source-table-id"
destination_project_id = "your-destination-project-id"
destination_dataset_id = "your-destination-dataset-id"
destination_table_id = "your-destination-table-id"
source_table = client.get_table(
f"{source_project_id}.{source_dataset_id}.{source_table_id}"
)
destination_table = bigquery.Table(f"{destination_project_id}.{destination_dataset_id}.{destination_table_id}")
destination_table.schema = source_table.schema
destination_table = client.create_table(destination_table)
job_config = bigquery.CopyJobConfig()
job = client.copy_table(source_table, destination_table, job_config=job_config)
job.result()
请确保将示例中的your-source-project-id
、your-source-dataset-id
、your-source-table-id
、your-destination-project-id
、your-destination-dataset-id
和your-destination-table-id
替换为实际的项目、数据集和表的ID。