要解决这个问题,您可以使用BigQuery Transfer Service API来创建传输,并在代码中指定目标数据集的位置。以下是使用Python编写的代码示例:
from google.cloud import bigquery_datatransfer_v1
def create_transfer(project_id, dataset_id, location):
client = bigquery_datatransfer_v1.DataTransferServiceClient()
parent = client.location_path(project_id, "us-central1")
transfer_config = {
"destination_dataset_id": dataset_id,
"display_name": "My Transfer",
"data_source_id": "bigquery",
"params": {
"destination_table_name_template": "table_{run_time|"%Y%m%d"}",
"write_disposition": "WRITE_TRUNCATE"
},
"schedule": "every 24 hours"
}
if location == "REGION_ASIA_SOUTHEAST_1":
transfer_config["params"]["location"] = "asia-southeast1"
response = client.create_transfer_config(parent, transfer_config)
print("Transfer created: {}".format(response.name))
# 调用函数创建传输
create_transfer("my-project-id", "my-dataset-id", "REGION_ASIA_SOUTHEAST_1")
在上面的代码中,我们使用bigquery_datatransfer_v1
库创建了一个BigQuery Transfer Service的客户端。然后,我们指定了传输的参数,包括目标数据集ID、显示名称、数据源ID(在这种情况下是BigQuery)、目标表名模板和写入模式。如果目标数据集位于REGION_ASIA_SOUTHEAST_1
,我们还指定了数据集的位置为asia-southeast1
。
最后,我们调用create_transfer_config
方法来创建传输配置,并打印出响应中的传输名称。
请确保您已经安装了google-cloud-bigquery-datatransfer
库,并使用正确的项目ID、数据集ID和位置参数调用create_transfer
函数。