Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google APIs Client Library for Ruby を使って BigQuery にデータをロードする
require 'bundler/setup'
require 'google/api_client'
require 'yaml'
require 'json'
def multipart_boundary
'xxx'
end
# load credential yaml
oauth_yaml = YAML.load_file('.google-api.yaml')
# Initialize the client.
client = Google::APIClient.new(
:application_name => 'Example Ruby Bigquery',
:application_version => '1.0.0')
client.authorization.client_id = oauth_yaml["client_id"]
client.authorization.client_secret = oauth_yaml["client_secret"]
client.authorization.scope = oauth_yaml["scope"]
client.authorization.refresh_token = oauth_yaml["refresh_token"]
client.authorization.access_token = oauth_yaml["access_token"]
# Initialize Bigquery client.
bq_client = client.discovered_api('bigquery', 'v2')
job_config = {
'configuration' => {
'load' => {
'sourceUris' => ['gs://a-know-test/sample.csv'],
'schema' => {
'fields' => [
{
'name' => 'id',
'type' => 'INTEGER'
},
{
'name' => 'name',
'type' => 'STRING'
},
{
'name' => 'price',
'type' => 'INTEGER'
},
]
},
'destinationTable' => {
'projectId' => 'test-001',
'datasetId' => 'test',
'tableId' => 'sample'
},
'createDisposition' => 'CREATE_NEVER',
'writeDisposition' => 'WRITE_APPEND'
}
}
}
body = "--#{multipart_boundary}\n"
body += "Content-Type: application/json; charset=UTF-8\n"
body += "\n"
body += "#{job_config.to_json}\n"
body += "--#{multipart_boundary}--\n"
# Make an API call.
result = client.execute(
:api_method => bq_client.jobs.insert,
:parameters => {
'projectId' => '234230709110',
'uploadType' => 'multipart'
},
:body => body,
:headers => { 'Content-Type' => "multipart/related; boundary=#{multipart_boundary}" }
)
puts result.data
puts result.response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.