首页 > 解决方案 > BigQuery DataTransfer:StartManualTransferRunsRequest 的构造函数输入无效

问题描述

我正在尝试通过 GCP 中的云函数触发计划查询。我尝试遵循几个教程和示例并浏览了文档,但是使用新版本的 APIgoogle-cloud-bigquery-datatransfer==3.1.1我找不到任何显示正确程序的内容。

您将在我当前的代码下方找到:

from google.cloud import bigquery_datatransfer_v1

def triggerQuery (parent, requested_run_time):
    client = bigquery_datatransfer_v1.DataTransferServiceClient()
    project_id = '###' # Enter your projectID here
    location_id = 'europe' # Enter your locationID here
    transfer_id = '###'  # Enter your transferId here
    transfer_config = {...}
    path = f"projects/{project_id}/locations/{location_id}/transferConfigs/{transfer_id}"
    parent = client.transfer_config_path(project_id, transfer_id)
    response = client.start_manual_transfer_runs(path)
    print(response)

我尝试传递给client.start_manual_transfer_runspathparent我得到:

TypeError: Invalid constructor input for StartManualTransferRunsRequest: 'projects/###/locations/europe/transferConfigs/###'

我还需要指定 Cloud Function 与有权在 BigQuery 中运行查询的正确服务帐户相关联。

帮助将不胜感激!

标签: pythongoogle-bigquerygoogle-cloud-functionsgoogle-cloud-data-transfer

解决方案


您需要以字典格式输入手动传输运行的参数。下面的代码对我有用:

import time
from google.cloud import bigquery_datatransfer_v1
from google.protobuf.timestamp_pb2 import Timestamp


client = bigquery_datatransfer_v1.DataTransferServiceClient()

project_id = 'your-project-id'
transfer_config_id = 'config-id-can-be-found-in-ui' 
location = 'location-of-your-transfer-config'

start_time = Timestamp(seconds=int(time.time()+20))
parent = f'projects/{project_id}/locations/{location}/transferConfigs/{transfer_config_id}'

response = client.start_manual_transfer_runs({"parent": parent, "requested_run_time": start_time})
print(response)

推荐阅读