2013-07-25 39 views
0

我想使用POST請求插入數據到BigQuery表。 我的應用程序創建的身體要求在指定的格式:谷歌BigQuery插入使用POST

--xxx 
Content-Type: application/json; charset=UTF-8 

{ 
    "configuration": { 
     "load": { 
      "sourceFormat": "NEWLINE_DELIMITED_JSON" 
     }, 
     "destinationTable": { 
      "projectId": "some-id", 
      "datasetId": "dataset-id", 
      "tableId": "cards" 
     } 
    } 
} 
--xxx 
Content-Type: application/octet-stream 

{"board_id":1,"version":2,"card_id":1,"title":"Tytul kartki 1"} 
--xxx-- 

但是當我發送此數據使用:

credentials = SignedJwtAssertionCredentials(
     SERVICE_ACCOUNT_EMAIL, 
     key, 
     scope='https://www.googleapis.com/auth/bigquery') 
self.http = credentials.authorize(httplib2.Http()) 
headers = {'Content-Type': 'multipart/related; boundary=xxx'} 
resp, content = self.http.request(url, method="POST", 
             body=output, 
             headers=headers) 

從服務器的響應是:

Status: {'date': 'Thu, 25 Jul 2013 12:49:06 GMT', 'status': '400', 'content-length': '205', 'content-type': 'application/json', 'server': 'HTTP Upload Server Built on Jul 12 2013 17:12:36 (1373674356)'} 
Content: { 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "required", 
    "message": "Required parameter is missing" 
    } 
    ], 
    "code": 400, 
    "message": "Required parameter is missing" 
} 
} 

我不知道缺少什麼參數。只有在文檔中需要的參數是sourceUris,但我想從請求體中加載數據而不是來自GS。

+0

我想你是缺少設置架構? –

回答

1

我覺得現在的問題是你缺少的架構配置:

'configuration': { 
    'load': { 
    'sourceFormat': <required for JSON files>', 
    'schema': { 
     'fields': [ 
     {'name':'f1', 'type':'STRING'}, 
     {'name':'f2', type:'INTEGER'} 
     ] 
    }, 
    'destinationTable': { 
     'projectId': 'projectId', 
     'datasetId': 'datasetId', 
     'tableId': 'tableId' 
    } 
    } 
} 

此鏈接可能幫助您:https://developers.google.com/bigquery/loading-data-into-bigquery

+0

我沒有寫架構,因爲它是可選參數,當我們使用JSON格式的數據[規範](https://developers.google.com/bigquery/docs/reference/v2/jobs#configuration.load.schema) –

0

檢查出自己loading data page的例子。注意項目ID位於兩個地方,jobData和destinationTable。這可能是缺少參數錯誤的原因。

0

的問題是,「destinationTable會」必須是裏面的「負荷」對象:

{ 
    "configuration": { 
     "load": { 
      "sourceFormat": "NEWLINE_DELIMITED_JSON" 
      "destinationTable": { 
       "projectId": "some-id", 
       "datasetId": "dataset-id", 
       "tableId": "cards" 
      } 
     } 
    } 
} 

它可以Loading Data Into BigQuery頁面可以看到。