【问题标题】:bigquery insert failed for timestamp datatype时间戳数据类型的 bigquery 插入失败
【发布时间】:2016-11-24 05:05:28
【问题描述】:

json_data = '["xxxxxx",65465464.0,2,-1,10.10]'

bigquery 表的架构:

id 字符串可以为空
时间戳 STRING NULLABLE
xid INTEGER NULLABLE
yid INTEGER NULLABLE
幅度 FLOAT NULLABLE

脚本.py:

data = json.loads(json_data)
table.reload()
rows = [data]
errors = table.insert_data(rows)

错误:

errors = table.insert_data(rows)
File "/usr/local/lib/python2.7/dist-    
packages/google/cloud/bigquery/table.py", line 749, in insert_data
value = _microseconds_from_datetime(value) * 1e-6
File "/usr/local/lib/python2.7/dist-packages/google/cloud/_helpers.py", line     
363, in _microseconds_from_datetime
if not value.tzinfo:
AttributeError: 'float' object has no attribute 'tzinfo'

有人知道这个错误吗?

赞赏!

【问题讨论】:

  • 在没有看到您的数据的情况下,很难确定,但看起来 BigQuery 期望某些东西是 datetime.datetime 对象,而您正在传递一个浮点数。
  • @fdsa 基本上,我在 UNIX 纪元中有一个时间戳数据,您可以在 json 记录中将其视为浮点类型。当我触发批处理作业以加载此数据的 json 文件时,它可以正常工作,无需将此浮点类型转换为 Datetime。但是对于相同的 json 记录,当我尝试一次使用大查询 API 插入时(如上所示),它会引发上述错误。任何指针都会有所帮助!

标签: python python-3.x google-bigquery


【解决方案1】:

@fdsa 是正确的。当您上传批处理文件时,它将接受浮点数和字符串,因为您无法在文件中存储日期时间。但是要使用 insert_data 命令,您 BQ 期望时间戳是 datetime.datetime 对象,因此在插入之前将 UNIX 时间戳加载到 datetime 中:

 datetime.datetime.fromtimestamp(your_unix_timestamp)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2020-01-13
    • 2017-08-16
    • 2011-05-29
    • 1970-01-01
    • 2021-12-13
    • 2017-06-12
    相关资源
    最近更新 更多