【问题标题】:How to insert/append unstructured data to bigquery table如何将非结构化数据插入/追加到 bigquery 表
【发布时间】:2016-12-22 18:51:45
【问题描述】:

背景

我想通过 python 客户端 API 将换行格式的 JSON 插入/附加到 bigquery 表中。

例如:

{"name":"xyz",mobile:xxx,location:"abc"}
{"name":"xyz",mobile:xxx,age:22}

问题是,一行中的所有字段都是可选的,并且没有固定定义的数据架构。

查询

我了解到我们可以使用支持自动模式检测的联合表。

但是,我正在寻找一种功能,它可以自动从数据中检测架构,相应地创建表,甚至在数据中出现任何额外的列/键时调整表架构,而不是创建新表。

这是否可以使用 python 客户端 API。

【问题讨论】:

    标签: python python-2.7 api google-app-engine google-bigquery


    【解决方案1】:

    您可以将自动检测与 BigQuery 加载 API 结合使用,即您使用 bq cli 工具的示例如下所示:

    ~$ cat /tmp/x.json
    {"name":"xyz","mobile":"xxx","location":"abc"}
    {"name":"xyz","mobile":"xxx","age":"22"}
    
    ~$ bq load --autodetect --source_format=NEWLINE_DELIMITED_JSON tmp.x /tmp/x.json
    Upload complete.
    
    ~$ bq show tmp.x
    Table tmp.x
    
       Last modified          Schema          Total Rows   Total Bytes   Expiration  
     ----------------- --------------------- ------------ ------------- ------------ 
      16 Aug 08:23:35   |- age: integer       2            33                        
                        |- location: string                                          
                        |- mobile: string                                            
                        |- name: string                                              
    
    
    ~$ bq query "select * from tmp.x"
    
    +------+----------+--------+------+
    | age  | location | mobile | name |
    +------+----------+--------+------+
    | NULL | abc      | xxx    | xyz  |
    |   22 | NULL     | xxx    | xyz  |
    +------+----------+--------+------+
    

    更新:如果以后需要添加其他字段,可以使用 schema_update_option 来允许新字段。唉,它还不能与自动检测一起使用,因此您需要明确地向加载 API 提供新架构:

    ~$ cat /tmp/x1.json 
    {"name":"abc","mobile":"yyy","age":"25","gender":"male"}
    
    ~$ bq load --schema=name:STRING,age:INTEGER,location:STRING,mobile:STRING,gender:STRING --schema_update_option=ALLOW_FIELD_ADDITION --source_format=NEWLINE_DELIMITED_JSON tmp.x /tmp/x1.json
    Upload complete.
    
    ~$ bq show tmp.x
    Table tmp.x
    
       Last modified          Schema          Total Rows   Total Bytes   Expiration  
     ----------------- --------------------- ------------ ------------- -----------
      19 Aug 10:43:09   |- name: string       3            57                        
                        |- age: integer                                              
                        |- location: string                                          
                        |- mobile: string                                            
                        |- gender: string                                            
    
    
    ~$ bq query "select * from tmp.x"
    status: DONE   
    +------+------+----------+--------+--------+
    | name | age  | location | mobile | gender |
    +------+------+----------+--------+--------+
    | abc  |   25 | NULL     | yyy    | male   |
    | xyz  | NULL | abc      | xxx    | NULL   |
    | xyz  |   22 | NULL     | xxx    | NULL   |
    +------+------+----------+--------+--------+
    

    【讨论】:

    • 这很好用,但是当我加载一个不同的文件时,它有一些额外的列(例如:“爱好”:“定义”),它会给出错误,说表格中没有“爱好”字段.那么,有什么想法如何动态更新表模式以避免错误并防止丢失任何数据?
    • 请稍等,下周我会给你一个很好的答复。
    • ok :),至少,我认为它目前在 bq 中不可用。
    • 可以通过 API 实现吗?我在文档中没有看到任何与 schema_update_option=ALLOW_FIELD_ADDITION 相关的内容。我可能看错了方向。你能指点我吗
    • 不是现在,它想看到全新的架构。理想情况下,这将与自动检测一起很好地工作,因此无需为更新的字段提供架构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2021-06-16
    • 2020-06-28
    • 2011-03-10
    相关资源
    最近更新 更多