我使用的是官方的Google Ruby gem,儘管迄今爲止我嘗試過的其他一切都正常工作(包括列出項目,數據集和表格以及創建表格),剔除負載工作與JSON錯誤響應裏面以下故障:通過API上傳本地CSV失敗
"Job configuration must contain exactly one job-specific configuration object (e.g., query, load, extract, spreadsheetExtract), but there were 0: "
我創建看起來像體字符串:
--xxx
Content-Type: application/json; charset=UTF-8
{"configuration":{"load":{"destinationTable":{"projectId":"mycompany.com:projectId","datasetId":"all_events","tableId":"install"},"createDisposition":"CREATE_NEVER","writeDisposition":"WRITE_APPEND"}}}
--xxx
Content-Type: application/octet-stream
test,second,1234,6789,83838
--xxx
我以前創建的install
表格,表格中的數據的適當模式,所以這不應該是問題米
最後,只是爲了完整,這裏是代碼的實際塊我使用斷火的要求(這是一個大的類中的兩個方法):
def create_insert_job
config = {
'configuration' => {
'load' => {
'destinationTable' => {
'projectId' => 'mycompany.com:projectId',
'datasetId' => 'all_events',
'tableId' => 'install'
},
'createDisposition' => 'CREATE_NEVER',
'writeDisposition' => 'WRITE_APPEND'
}
}
}
body = "#{multipart_boundary}\n"
body += "Content-Type: application/json; charset=UTF-8\n"
body += "#{config.to_json}\n"
body += "#{multipart_boundary}\n"
body +="Content-Type: application/octet-stream\n"
body += "test,second,1234,6789,83838\n"
body += "#{multipart_boundary}\n"
prepare_big_query # This simply gets tokens and instantiates google_client and big_query_api
param_hash = { api_method: big_query_api.jobs.insert }
param_hash[:parameters] = {'projectId' =>'mycompany.com:projectId'}
param_hash[:body] = body
param_hash[:headers] = {'Content-Type' => "multipart/related; boundary=#{multipart_boundary}"}
result = google_client.execute(param_hash)
JSON.parse(result.response.body)
end
def multipart_boundary
'--xxx'
end
任何想法?
ADDITIONS下面的答案使此代碼工作
注意上面#multipart_boundary方法與返回「 - 」前綴已經。這是一個問題,因爲當我們想要'xxx'時,設置邊界頭部(在param散列中)將導致' - xxx'。
此外,這個寶石的文檔是相當粗糙的,因爲修復我的新行問題(per @ jcondit的答案)後,我得到一個關於上傳到錯誤的URL的新錯誤。這是因爲您需要添加:
'uploadType' => 'multipart'
給參數,以便將請求發送到正確的URL。
這樣的工作(同樣,固定的換行和邊界isses後)最終param_hash樣子:
param_hash = { api_method: big_query_api.jobs.insert }
param_hash[:parameters] = {'projectId' => project_id, 'uploadType' => 'multipart'}
param_hash[:body] = body
param_hash[:headers] = {'Content-Type' => "multipart/related; boundary=#{multipart_boundary}"}
謝謝! 2年後,這幫了我。找不到我需要在參數中包含「uploadType」。 – mmay 2014-09-16 01:13:51