2016-11-24 61 views
0

json_data = '[ “XXXXXX”,65465464.0,2,-1,10.10]'大量查询插入失败为timestamp数据类型

架构大量查询表的:

ID STRING NULLABLE
时间戳STRING NULLABLE
XID INTEGER NULLABLE
YID INTEGER NULLABLE
幅度FLOAT NULLABLE

script.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' 

有没有人有这个错误的任何想法?

赞赏!

+1

没有看到您的资料,很难确切地知道,但它看起来像BigQuery期待某件事是'datetime.datetime'对象,并且你传递一个float。 – fdsa

+0

@fdsa基本上,我在UNIX纪元中有一个时间戳数据,你可以在json记录中看到浮点数类型。当我触发批处理作业加载此数据的json文件时,它工作正常,无需将此浮点类型转换为Datetime。但对于相同的json记录,当我试图插入一次使用大查询API(如上所示)时,它会抛出上述错误。任何指针都会有帮助! – Revan

+0

https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp – fdsa

回答

0

@fdsa是正确的。当您上传批处理文件时,它将接受浮动和字符串,因为您无法在文件中存储日期时间。但使用insert_data命令你BQ预计时间戳是任何一个datetime.datetime对象,以便在UNIX时间戳加载到日期时间插入前:

datetime.datetime.fromtimestamp(your_unix_timestamp)