我正在尝试使用Ruby API对Google Big Query运行一个Query。
这是我使用Ruby的第一个项目,我仍在学习该语言。
这也是我使用谷歌API的第一个项目。
环境:
- Windows 7
- Ruby 1.9
- 法拉第0.90
- Google API-服务帐户验证
我的代码运行时没有给出任何警告或错误消息通过:
@client.authorization.fetch_access_token!
doc = File.read('bigQueryAPI.json')
@bigQuery = @client.register_discovery_document('bigquery', 'v2', doc)
注意:@bigQuery
是从文件加载的,因为当我尝试用
加载@bigquery
时@bigquery = @client.discovered_api('bigquery', 'v2')
我得到Google::APIClient::ClientError: Not Found
,只检查打印#<Google::APIClient::API:0x17c94cc ID:bigquery:v2>
但是,如果我将Big Query API保存为来自https://www.googleapis.com/discovery/v1/apis/bigquery/v2/rest
然后用将其加载为文本文件
doc = File.read('bigQueryAPI.json')
@bigQuery = @client.register_discovery_document('bigquery', 'v2', doc)
那么CCD_ 6实际上返回一些有用的东西。@bigQuery.inspect输出。
然而,当我尝试实际运行查询时,如下所示:
result = @client.execute!(
:api_method => @bigQuery.batch_path.query,
:body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " +
"count(payload_commit) as commits_total, " +
"count(DISTINCT repository_name) / count(payload_commit) as average, " +
"FROM [githubarchive:github.timeline]" }, #,
:parameters => { "projectId" => @project_id })
我得到以下错误:
NoMethodError: undefined method `query_values' for nil:NilClass
以下是错误的完整堆栈跟踪:
1) Error:
test_averages(Test_GitHub_Archive):
NoMethodError: undefined method `query_values' for nil:NilClass
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client/request.rb:145:in `uri='
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client/request.rb:101:in `initialize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:518:in `new'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:518:in `generate_request'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:583:in `execute!'
C:/Users/tfburton/Documents/private/ProjectSuggestor/RubyStats/GitHub_Archive.rb:39:in `get_averages'
C:/Users/tfburton/Documents/private/ProjectSuggestor/RubyStats/TestSpec/test_GitHub_Archive.rb:26:in `test_averages'
以下是@client.inspect的结果
注意:我本可以粘贴到这里,但我的帖子超过了长度限制。
在做了一些挖掘之后。看起来我没有传递正确的@bigQuery
prameter来获取查询函数
查看@bigQuery.inspect的转储,我需要在751行传递该方法
然而,我似乎不知道如何传递那个方法。
如果去掉检查输出的其余部分,则"路径"如下所示:{ "resources => { "jobs" => { "methods" => { "query"
我尝试过@bigQuery.Jobs.query
,结果出现一个错误,指出@bigQuery.Jobs
不存在。
- 那么我是否正确创建了
@bigQuery
- 为什么
@bigQuery.Jobs.query
不起作用
下面是我如何使用bigquery.jobs.query方法的,这可能正是您所需要的。
我必须设置OpenSSL::SSL::VERIFY_PEER=OpenSSL::SSL::VERIFY_NONE,因为否则授权过程将失败,但这可能特定于最低Win7/MgitSys环境。在任何情况下,这条特定的生产线在产品中都是不安全的
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
require 'google/api_client/auth/file_storage'
require 'openssl'
require 'json'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
# Initialize the client.
client = Google::APIClient.new(
:application_name => 'Example Ruby application',
:application_version => '1.0.0'
)
CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json"
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
# Initialize Google+ API. Note this will make a request to the
# discovery service every time, so be sure to use serialization
# in your production code. Check the samples for more details.
@bigQuery = client.discovered_api('bigquery', 'v2')
# Load client secrets from your client_secrets.json.
client_secrets = Google::APIClient::ClientSecrets.load
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
if file_storage.authorization.nil?
client_secrets = Google::APIClient::ClientSecrets.load
# The InstalledAppFlow is a helper class to handle the OAuth 2.0 installed
# application flow, which ties in with FileStorage to store credentials
# between runs.
flow = Google::APIClient::InstalledAppFlow.new(
:client_id => client_secrets.client_id,
:client_secret => client_secrets.client_secret,
:scope => ['https://www.googleapis.com/auth/cloud-platform','https://www.googleapis.com/auth/bigquery']
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
puts "authorized, requesting"
# Make an API call.
result = client.execute!(
:api_method => @bigQuery.jobs.query,
:body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " +
"count(payload_commit) as commits_total, " +
"count(DISTINCT repository_name) / count(payload_commit) as average, " +
"FROM [githubarchive:github.timeline]" }, #,
:parameters => { "projectId" => "845227657643" })
puts JSON.dump result.data