我正在尝试使用示例架构和一些示例数据从Perl执行上传到BigQuery。我在他们提供的文档后遇到了死胡同,所以现在我试图模仿bq
命令行客户端成功执行的操作。
我通过向bq
中的print (method, uri, headers, body)
方法添加调试request
来跟踪httplib2
所做的事情。我通过在响应上执行Dumper
来跟踪我的Perl库正在做什么,该响应还包括我发送的_request
。 bq
中的模式是他们POST
上传网址,然后返回location
到PUT
数据。通过一系列GET
请求监控相应的作业,最后他们回复。
在Perl中,POST
成功,GET
以Invalid Upload Request
失败(但没有提示为何无效)。我想弄清楚两者之间有什么区别可以解释我的失败。但我找不到它。
以下是(使用access_token,IP地址和project_id省略)我得到的痕迹。
对于POST
来自Python的信息是:
(
u'POST',
u'https://www.googleapis.com/upload/bigquery/v2/projects/<project ID>/jobs?uploadType=resumable&alt=json',
{
'content-length': '442',
'accept-encoding': 'gzip, deflate',
'accept': 'application/json',
'user-agent': u'bq/2.0 google-api-python-client/1.0',
'X-Upload-Content-Length': '84',
'X-Upload-Content-Type': 'application/octet-stream',
'content-type': 'application/json',
'Authorization': u'Bearer <access token>'
},
'{"configuration": {"load": {"sourceFormat": "NEWLINE_DELIMITED_JSON", "destinationTable": {"projectId": "<project id>", "tableId": "demo_api", "datasetId": "tmp_bt"}, "maxBadRecords": 0, "schema": {"fields": [{"type": "STRING", "mode": "required", "name": "demo_string"}, {"type": "INTEGER", "mode": "required", "name": "demo_integer"}]}}}, "jobReference": {"projectId": "<project id>", "jobId": "bqjob_r139e633b7e522cf7_0000014031d9fb49_1"}}'
)
相应的Perl获得了一个看似成功的响应对象(在其中可以看到_request
):
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_rc' => '200',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'location' => 'https://www.googleapis.com/upload/bigquery/v2/projects/<project id>/jobs?uploadType=resumable&upload_id=AEnB2Ur0mdwmZpMot6ftkgj1IkqK0f7oPbZrXWQekUDHK_E2o2HKznJO6DK2xPYCB-nhUGrMrEJJ7z1Tz9Crnka9e5EYGP1lWQ',
'date' => 'Tue, 06 Aug 2013 20:46:05 GMT',
'client-ssl-cert-issuer' => '/C=US/O=Google Inc/CN=Google Internet Authority',
'client-ssl-cipher' => 'RC4-SHA',
'client-peer' => '<some ip>:443',
'content-length' => '0',
'client-date' => 'Tue, 06 Aug 2013 20:46:05 GMT',
'content-type' => 'text/html; charset=UTF-8',
'client-ssl-cert-subject' => '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com',
'server' => 'HTTP Upload Server Built on Jul 24 2013 17:20:01 (1374711601)',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'OK',
'_request' => bless( {
'_content' => '{"configuration":{"load":{"maxBadRecords":0,"destinationTable":{"datasetId":"tmp_bt","tableId":"perl","projectId":<project id>},"sourceFormat":"NEWLINE_DELIMITED_JSON","schema":{"fields":[{"mode":"required","name":"demo_string","type":"STRING"},{"mode":"required","name":"demo_integer","type":"INTEGER"}]}}},"jobReference":{"projectId":<project id>,"jobId":"perlapi_1375821964"}}',
'_uri' => bless( do{\(my $o = 'https://www.googleapis.com/upload/bigquery/v2/projects/<project id>/jobs?uploadType=resumable')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.05',
'content-type' => 'application/json',
'accept' => 'application/json',
':X-Upload-Content-Type' => 'application/octet-stream',
'content-length' => 379,
':X-Upload-Content-Length' => '84',
'authorization' => 'Bearer <access token>'
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
然后我们有PUT
。在Python方面,我们发送了:
(
'PUT',
'https://www.googleapis.com/upload/bigquery/v2/projects/<project id>/jobs?uploadType=resumable&alt=json&upload_id=AEnB2UpWMRCAOffqyR0d7zvGVtD-KWhrC9jGB-q_igecJgoyz_mIHgEFfs9cYoPxUwUxuflQScMzGxDsKKJ_CJPQq4Os-AkdZA',
{
'Content-Range': 'bytes 0-83/84',
'Content-Length': '84',
'Authorization': u'Bearer <access token>',
'user-agent': u'bq/2.0'
},
<apiclient.http._StreamSlice object at 0x10ce11150>
)
(我已经验证了流切片对象与Perl具有相同的84个字节。)这是Perl失败:
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Invalid Upload Request"
}
],
"code": 400,
"message": "Invalid Upload Request"
}
}
',
'_rc' => '400',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Tue, 06 Aug 2013 20:46:07 GMT',
'client-ssl-cert-issuer' => '/C=US/O=Google Inc/CN=Google Internet Authority',
'client-ssl-cipher' => 'RC4-SHA',
'client-peer' => '<some IP address>:443',
'content-length' => '193',
'client-date' => 'Tue, 06 Aug 2013 20:46:07 GMT',
'content-type' => 'application/json',
'client-ssl-cert-subject' => '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com',
'server' => 'HTTP Upload Server Built on Jul 24 2013 17:20:01 (1374711601)',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'Bad Request',
'_request' => bless( {
'_content' => '{"demo_string":"foo", "demo_integer":"2"}
{"demo_string":"bar", "demo_integer":"3"}
',
'_uri' => bless( do{\(my $o = 'https://www.googleapis.com/upload/bigquery/v2/projects/<project id>/jobs?uploadType=resumable&upload_id=AEnB2Ur0mdwmZpMot6ftkgj1IkqK0f7oPbZrXWQekUDHK_E2o2HKznJO6DK2xPYCB-nhUGrMrEJJ7z1Tz9Crnka9e5EYGP1lWQ')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.05',
':Content-Length' => '84',
':Content-Range' => '0-83/84',
'content-length' => 84,
'authorization' => 'Bearer <access token>'
}, 'HTTP::Headers' ),
'_method' => 'PUT',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
我应该尝试更改Perl方面以使BigQuery像bq
那样回复我?
答案 0 :(得分:1)
你的一些PUT标题前面有冒号,但Python没有:
':Content-Length' => '84',
':Content-Range' => '0-83/84',
答案 1 :(得分:0)
我怀疑多部分上传请求中存在格式错误。错误“无效上载请求”是响应于尝试将数据有效负载从多部分mime消息中分离出来。您的日志记录不包含请求正文的详细信息,因此我们无法将它们进行并排比较,以发现意外差异。
要确保问题是分段上传,您可以尝试从Google存储加载数据的加载请求,而不是将数据包含在请求有效负载本身中。这将验证perl api请求路径是否适合您。
仅供参考:有一个alpha Perl Google Apis客户端可能会帮助您。我没有尝试过,也不知道它是否在积极开发中,但你可能会在那里找到一些有用的提示。查看https://code.google.com/p/google-api-perl-client/