简体   繁体   English

从 python3.7 在 bigquery 中插入时间戳值时出错

[英]error on inserting timestamp value in bigquery from python3.7

i am calling a rest api from python3.7 and I am getting api response in json format.我正在从 python3.7 调用一个 rest api,我得到 json 格式的 api 响应。 One of the fields is a timestamp string, but in the json response i am getting that in this format.其中一个字段是时间戳字符串,但在 json 响应中,我以这种格式获取。

create_time {
  seconds: 1604562539
  nanos: 418758000
}

I need to insert this into a bigquery table in UTC format which is a timestamp field我需要将其插入到 UTC 格式的 bigquery 表中,这是一个时间戳字段

so I convert this to Json string like this所以我把它转换成这样的 Json 字符串

createdtime=json.dumps(create_time, default=datetime_handler) createdtime=json.dumps(create_time, default=datetime_handler)

def datetime_handler(dt):
    if isinstance(dt, datetime):
        return dt.isoformat()
    raise TypeError("Unknown type")

this gives me a json string in this format 2020-11-05T07:48:59.418758+00:00这给了我一个这种格式的 json 字符串2020-11-05T07:48:59.418758+00:00

next I need to try to insert this create_time into big-query table in a timestamp field by passing this value to the bigquery request and i get the insert error接下来,我需要通过将此值传递给 bigquery 请求来尝试将此 create_time 插入到时间戳字段中的 big-query 表中,并且出现插入错误

 'bigquery#tableDataInsertAllResponse', 'insertErrors': [{'index': 0, 'errors': [{'reason': 'invalid', 'location': 'createdtime', 'debugInfo': '', 'message': 'Could not parse \'"2020-11-05T07:48:59.418758+00:00"\' as a timestamp. Required format is YYYY-MM-DD HH:MM[:SS[.SSSSSS]]'}]}

so I remove the offset(00:00) and now the date becomes 2020-11-05T07:48:59.418758 and i pass this to bigquery again i get the same error.所以我删除了偏移量(00:00),现在日期变成了2020-11-05T07:48:59.418758我再次将它传递给 bigquery 我得到了同样的错误。

'bigquery#tableDataInsertAllResponse', 'insertErrors': [{'index': 0, 'errors': [{'reason': 'invalid', 'location': 'createdtime', 'debugInfo': '', 'message': 'Could not parse \'"2020-11-05T07:48:59.418758"\' as a timestamp. Required format is YYYY-MM-DD HH:MM[:SS[.SSSSSS]]'}]},

so finally i replace the offset with Z and the createdtime becomes 2020-11-05T07:48:59.418758Z and i pass it to bigquery and get the same result所以最后我用 Z 替换了偏移量,createdtime 变为2020-11-05T07:48:59.418758Z并将它传递给 bigquery 并得到相同的结果

**Error: {'kind': 'bigquery#tableDataInsertAllResponse', 'insertErrors': [{'index': 0, 'errors': [{'reason': 'invalid', 'location': 'createdtime', 'debugInfo': '', 'message': 'Could not parse \'"2020-11-05T07:48:59.418758Z"\' as a timestamp. Required format is YYYY-MM-DD HH:MM[:SS[.SSSSSS]]'}]}**

And finally based on one of the suggestions i received below i have replaced T also so now the date becomes 2020-11-05 07:48:59.418758 and I still get the same error:最后根据我在下面收到的建议之一,我也替换了T,所以现在日期变为2020-11-05 07:48:59.418758并且我仍然遇到相同的错误:

Error: {'kind': 'bigquery#tableDataInsertAllResponse', 'insertErrors': [{'index': 0, 'errors': [{'reason': 'invalid', 'location': 'createdtime', 'debugInfo': '', 'message': 'Could not parse \'"2020-11-05 07:48:59.418758"\' as a timestamp. Required format is YYYY-MM-DD HH:MM[:SS[.SSSSSS]]'}]},

I am very new to python3.7 and also not much familiar with timestamp values in python and bigquery, Can anyone please suggest a solution?我对python3.7很陌生,对python和bigquery中的时间戳值也不太熟悉,有人可以提出解决方案吗? Thanks谢谢

This is the code I have put all the irrelevant parts also for understanding:这是我把所有不相关的部分也放在理解的代码:

    row={}
rows=[]
     def datetime_handler(dt):
            if isinstance(dt, datetime):
                return dt.isoformat()
            raise TypeError("Unknown type")
     createdtime=json.dumps(snapshotresponse.create_time,default=datetime_hander)
    createdtime=createdtime.replace("+00:00", "").replace('T'," ")
    row["createdTime"]= createdtime
    json_msg = {
                           "json": row
                        }
    rows.append(json_msg)
    body = {
            "kind": "bigquery#tableDataInsertAllRequest",
            "skipInvalidRows": "false",
            "rows": rows
                }
 

Required format is stated as: YYYY-MM-DD HH:MM[:SS[.SSSSSS]]所需格式表示为: YYYY-MM-DD HH:MM[:SS[.SSSSSS]]

You have a 'T' you need to get rid of:你有一个“T”你需要摆脱:

date_time_str = '2020-11-05T07:48:59.418758'.replace('T', ' ')


EDIT:编辑:

I was avoiding creating a datetime object just to make it a string and then send it to be parsed into a dt object again since you already had a json value.我避免创建一个 datetime 对象只是为了使它成为一个字符串,然后将它发送到再次解析为 dt 对象,因为你已经有了一个 json 值。 But, if you do a simple print(date_time_str) I'd imagine you could easily match it up to the required format and see what is wrong.但是,如果你做一个简单的 print(date_time_str) 我想你可以很容易地将它与所需的格式相匹配,看看有什么问题。

If you don't see the formatting issue, I'd say go with the suggestion of hatef Alipoor and just stringify an actual datetime.如果您没有看到格式问题,我会说按照hatef Alipoor的建议,将实际日期时间字符串化。

Required format: YYYY-MM-DD HH:MM[:SS[.SSSSSS]]所需格式:YYYY-MM-DD HH:MM[:SS[.SSSSSS]]

from datetime import datetime

sec = 1604562539
nano = 418758000
total_sec = sec + nano / 1000000000
dt = datetime.utcfromtimestamp(total_sec)
dt.strftime('%Y-%m-%d %H:%M:%S.%f')
# '2020-11-05 07:48:59.418758'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM