Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active November 15, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holysugar/4985188e2e8ad98488b8a84eea1c78f1 to your computer and use it in GitHub Desktop.
Save holysugar/4985188e2e8ad98488b8a84eea1c78f1 to your computer and use it in GitHub Desktop.
bigquery v2 call sample (クエリのテーブルへの出力)
# https://github.com/google/google-api-ruby-client/blob/master/samples/cli/lib/samples/bigquery.rb
require 'google/apis/bigquery_v2'
require 'googleauth'
require 'securerandom'
# CHANGEME
project_id = 'myproject'
dataset_id = 'mydataset'
table_id = 'mytable'
job_id = "apitest-#{SecureRandom.uuid}"
query = <<EOD
SELECT repository.url as url, count(repository.url) as count FROM [bigquery-public-data:samples.github_nested]
GROUP BY url
ORDER BY count DESC
EOD
Bigquery = Google::Apis::BigqueryV2
scopes = [
Bigquery::AUTH_BIGQUERY,
]
bigquery = Bigquery::BigqueryService.new
# 認証は GCE の権限でいい場合↓で済む。そうじゃない場合は要修正
bigquery.authorization = Google::Auth.get_application_default(scopes)
job = Bigquery::Job.new(
configuration: {
query: {
allow_large_results: true,
destination_table: {
project_id: project_id,
dataset_id: dataset_id,
table_id: table_id,
},
write_disposition: 'WRITE_TRUNCATE',
query: query,
},
},
job_reference: {
job_id: job_id,
project_id: project_id,
},
)
bigquery.insert_job(project_id, job) {|result, err|
raise err if err # FIXME
puts result.class
puts result.to_json
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment