You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BigQuery数据传输ScheduleOptions无法保留函数设置值问题咨询

BigQuery DataTransfer ScheduleOptions赋值后字段为None的问题

在初始化bigquery_datatransfer_v1.types.ScheduleOptions消息并设置字段值时,无论是构造函数传值还是直接属性赋值,start_time等字段始终为None,文档中提到的set方法也无法被识别。当前代码及输出如下:

函数代码

def build_schedule_options(disable_auto_scheduling_flag): 
    config = bigquery_datatransfer_v1.types.ScheduleOptions()     
    print(start_dt.isoformat())
    staggered_start_time = Timestamp().FromDatetime(start_dt)
    config.disable_auto_scheduling = disable_auto_scheduling_flag
    config.start_time = staggered_start_time
    
    print(config.start_time)
    ...
    print(config.start_time, config.end_time, config.disable_auto_scheduling,)

输出结果

2023-09-08T12:43:57.755482
None
None None False

问题原因及解决方法

核心问题:Timestamp转换方式错误

你使用的Timestamp().FromDatetime(start_dt)是PHP版的调用方式,Python版的Protobuf Timestamp类并不支持这种写法:

  • Python中Timestamp类的正确转换方法是类方法from_datetime(小写开头),而非实例调用FromDatetime
  • 错误的调用方式会返回None,导致start_time赋值后仍为None

修正步骤

  1. 导入正确的Timestamp类:
    from google.protobuf.timestamp_pb2 import Timestamp
    
  2. 修正Timestamp转换代码:
    将staggered_start_time = Timestamp().FromDatetime(start_dt)替换为:
    staggered_start_time = Timestamp.from_datetime(start_dt)
    

验证字段赋值正确性

Python中Protobuf消息类的字段直接通过属性赋值是合法的,确认ScheduleOptions的字段名与Python版定义一致(可通过print(dir(config))查看所有可用字段)。

修正后的完整代码示例

from google.protobuf.timestamp_pb2 import Timestamp
import bigquery_datatransfer_v1

def build_schedule_options(disable_auto_scheduling_flag): 
    config = bigquery_datatransfer_v1.types.ScheduleOptions()     
    print(start_dt.isoformat())
    # 正确转换Datetime到Timestamp
    staggered_start_time = Timestamp.from_datetime(start_dt)
    config.disable_auto_scheduling = disable_auto_scheduling_flag
    config.start_time = staggered_start_time
    
    print(config.start_time)
    # ...其他业务逻辑
    print(config.start_time, config.end_time, config.disable_auto_scheduling,)

内容的提问来源于stack exchange,提问作者Ben Delany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:43:17