我使用的是官方的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