2017-07-28 106 views
0

我有一個JSON記錄,它看起來像BIGQUERY無法從谷歌雲存儲加載數據

{"customer_id":"2349uslvn2q3","order_id":"9sufd23rdl40", 
"line_item": [{"line":"1","sku":"10","amount":10}, 
       {"line":"2","sku":"20","amount":20}]} 

我試圖加載上述成具有架構定義爲表記錄,

"fields": [ 
    { 
    "mode": "NULLABLE", 
    "name": "customer_id", 
    "type": "STRING" 
    }, 
    { 
    "mode": "NULLABLE", 
    "name": "order_id", 
    "type": "STRING" 
    }, 
    { 
    "mode": "REPEATED", 
    "name": "line_item", 
    "type": "STRING" 
    } 
] 

我收到以下錯誤「消息」:

JSON行解析錯誤,起始於位置0的文件: gs:// gcs_bucket/file0。用於非記錄字段中指定的JSON對象: LINE_ITEM

我想其可以具有多於1行作爲JSON字符串的數組中的行項目列於表line_item JSON字符串。

有什麼建議嗎?

回答

1

的第一件事情是,你的輸入JSON should't有一個「\ n」字符,所以你應該保存它想:

:你的JSON文件應該如何看起來像

{"customer_id":"2349uslvn2q3","order_id":"9sufd23rdl40", "line_item": [{"line":"1","sku":"10","amount":10}, {"line":"2","sku":"20","amount":20}]} 

一個例子

{"customer_id":"2349uslvn2q3","order_id":"9sufd23rdl40", "line_item": [{"line":"1","sku":"10","amount":10}, {"line":"2","sku":"20","amount":20}]} 
{"customer_id":"2","order_id":"2", "line_item": [{"line":"2","sku":"20","amount":20}, {"line":"2","sku":"20","amount":20}]} 
{"customer_id":"3","order_id":"3", "line_item": [{"line":"3","sku":"30","amount":30}, {"line":"3","sku":"30","amount":30}]} 

而且你的模式不正確。它應該是:

[ 
    { 
    "mode": "NULLABLE", 
    "name": "customer_id", 
    "type": "STRING" 
    }, 
    { 
    "mode": "NULLABLE", 
    "name": "order_id", 
    "type": "STRING" 
    }, 
    { 
    "mode": "REPEATED", 
    "name": "line_item", 
    "type": "RECORD", 
    "fields": [{"name": "line", "type": "STRING"}, {"name": "sku", "type": "STRING"}, {"name": "amount", "type": "INTEGER"}] 
    } 
] 

爲了更好地理解的模式是如何工作的,我已經試過在this answer寫不大不小的指導。希望它具有一定的價值。

如果你的數據內容在保存例如一提起稱爲gs://gcs_bucket/file0和你在schema.json模式則該命令應爲你工作:

bq load --source_format=NEWLINE_DELIMITED_JSON dataset.table gs://gcs_bucket/file0 schema.json 

(假設您正在使用CLI工具,因爲它似乎是在你的問題的情況下)。

+0

謝謝你的建議!您在這裏的模式定義,我已經嘗試過並且工作正常。我之前有過,我希望將整個line_item作爲表中的json值數組,而不是字符串數組。是否有可能將整行項目作爲json? – Kevin