[英]How to insert/append unstructured data to bigquery table
背景
我想通過python客戶端API將換行格式化的JSON插入/附加到bigquery
表中。
例如:
{"name":"xyz",mobile:xxx,location:"abc"}
{"name":"xyz",mobile:xxx,age:22}
問題是,連續的所有字段都是可選的,並且數據沒有固定的已定義模式。
詢問
我已經讀過,我們可以使用支持自動檢測的聯合表。
但是,我正在尋找一種功能,它可以自動檢測數據模式,相應地創建表,甚至調整表模式,如果數據中出現任何額外的列/鍵而不是創建新表。
使用python客戶端API是否可行。
您可以將自動檢測與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 |
+------+------+----------+--------+--------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.