【问题标题】:Not able to parse date information in Google BigQuery无法解析 Google BigQuery 中的日期信息
【发布时间】:2021-03-25 10:46:02
【问题描述】:

我已将一些数据从 MongoDB 移动到 BigQuery。不幸的是,现在无法访问 MongoDB 源。我能够在stackoverflow communityAlessandro 的帮助下解析大部分信息,但我在解析日期信息方面遇到了问题。以下是我创建的示例数据

with cte as (
    select 12829 as user_id,
    '[{"_id":{"$oid":"5d650676af82eb0a30737e74"},"_type":"UserName","capture_date":{"$date":"2019-08-27T00:00:00.000+0000"},"source":"google","name1":"John","name3":"Doe","name4":"D/O Jane Doe","gender":"1","dob":{"$date":"1986-07-10T00:00:00.000+0000"}}]' as json_line
)
select
    user_id,
    json_value(json_line, '$[0].name1') as name1,
    json_value(json_line, '$[0].capture_date') as capture_date,
    PARSE_DATE('{"$date":"%Y-%m-%dT00:00:00.000+0000"}', json_value(json_line, '$[0].capture_date')) as capture_date1
from cte

我已经尝试过使用 parse_date,它没有用。您能否在提供的方法中帮助我解决我的错,或者是否有任何更好的方法来解决同样的问题?

我正在尝试解析 cature_date 信息并且得到空值。

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    试试json_extract_scalar:

    with cte as (
        select 12829 as user_id,
        '[{"_id":{"$oid":"5d650676af82eb0a30737e74"},"_type":"UserName","capture_date":{"$date":"2019-08-27T00:00:00.000+0000"},"source":"google","name1":"John","name3":"Doe","name4":"D/O Jane Doe","gender":"1","dob":{"$date":"1986-07-10T00:00:00.000+0000"}}]' as json_line
    )
    select
        user_id,
        json_extract(json_line, '$[0].name1') as name1,
        json_extract_scalar(json_line, "$[0].capture_date['$date']") as capture_date,
        PARSE_DATE("%Y-%m-%dT00:00:00.000+0000", json_extract_scalar(json_line, "$[0].capture_date['$date']")) as capture_date1,
    from cte
    

    json_value:

    with cte as (
        select 12829 as user_id,
        '[{"_id":{"$oid":"5d650676af82eb0a30737e74"},"_type":"UserName","capture_date":{"$date":"2019-08-27T00:00:00.000+0000"},"source":"google","name1":"John","name3":"Doe","name4":"D/O Jane Doe","gender":"1","dob":{"$date":"1986-07-10T00:00:00.000+0000"}}]' as json_line
    )
    select
        user_id,
        json_extract(json_line, '$[0].name1') as name1,
        json_value(json_line, '$[0].capture_date."$date"') as capture_date,
        PARSE_DATE("%Y-%m-%dT00:00:00.000+0000", json_value(json_line, '$[0].capture_date."$date"')) as capture_date1,
    from cte
    

    【讨论】:

    • 太棒了 :) 感谢您的快速帮助
    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多