Skip to content

Instantly share code, notes, and snippets.

@a-know
Created May 6, 2014 14:00

Revisions

  1. a-know created this gist May 6, 2014.
    76 changes: 76 additions & 0 deletions load_csv.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    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