2012-08-04 94 views
1

我使用的是官方的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}"} 
+0

謝謝! 2年後,這幫了我。找不到我需要在參數中包含「uploadType」。 – mmay 2014-09-16 01:13:51

回答

0

你需要插入每個MIME部分的標題和每個MIME頭部分的主體之間的額外的換行符。請求的正文應如下所示:

--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-- 

請注意每個部分中的Content-Type標頭後面的額外換行符。

另外,不要忘記,最後的邊界分隔符有一個尾隨 - 附加到它。

0

你的HTTP請求的格式不正確 - BigQuery的完全不承認這是一個負荷工作。我正在出去吃晚飯,所以我不能做更深入的調查,但希望這給你一個指示繼續。

我仔細看了一下,我沒有看到你的請求有什麼問題。一個建議是嘗試在bigquery UI中執行相同的工作,並使用chrome工具 - >開發人員工具/網絡選項卡查看發送的RPC。

如果我這樣做,有一個虛擬的csv文件,我得到:

--yql9f05215ct 
Content-Type: application/json; charset=utf-8 

{"jobReference":{"projectId":"helixdata2"},"configuration":{"load":{"destinationTable":{"projectId":"helixdata2","datasetId":"lotsOdata","tableId":"import"}}}} 
--yql9f05215ct 
Content-Type: application/octet-stream 
Content-Transfer-Encoding: base64 

YSxiLGMKYyxkLGUKZixnLGgK 
--yql9f05215ct-- 
+0

3分歧我看到: - jobReference鍵/值哈希 - 內容傳輸編碼:的base64 - 專案編號僅僅是名稱,而不是完整的路徑 我試過porjectId兩種方式,並在jobReference和內容 - 添加轉讓。兩個失敗。見下面的評論。 – user1575390 2012-08-04 03:44:27

+0

--xxx Content-Type:application/json; charset = UTF-8 {「jobReference」:{「projectId」:「wizards」},「configuration」:{「load」:{「destinationTable」:{「projectId」:「wizards」,「datasetId」:「all_events內容傳輸編碼:內容類型:應用/八位字節流 Content-Transfer-Encoding: base64 test,second,1234,6789,83838 --xxx – user1575390 2012-08-04 03:47:58

+0

--xxx Content-Type:application/json; charset = UTF-8 {「jobReference」:{「projectId」:「orangedoorarmory.com:wizards"},"configuration":{"load":{"destinationTable":{"projectId":"orangedoorarmory.com:wizards 「,」datasetId「:」all_events「,」tableId「:」install「},」createDisposition「:」CREATE_NEVER「,」writeDisposition「:」WRITE_APPEND「}}} --xxx Content-Type:application /流 Content-Transfer-Encoding:base64 test,second,1234,6789,83838 --xxx – user1575390 2012-08-04 03:48:49