GithubHelp home page GithubHelp logo

bigquery's Introduction

BigQuery

BigQuery is a wrapper around the Google api ruby gem designed to make interacting with BigQuery easier.

Install

gem install bigquery

Authorization

Only service accounts are supported right now. https://developers.google.com/accounts/docs/OAuth2#serviceaccount

Available methods

  • query
  • tables
  • datasets
  • load
  • tables_formatted
  • job
  • jobs
  • insert_job
  • refresh_auth

Example

require 'big_query'

opts = {}
opts['client_id']     = '1234.apps.googleusercontent.com'
opts['service_email'] = '[email protected]'
opts['key']           = '/path/to/somekeyfile-privatekey.p12'
opts['project_id']    = '54321'
opts['dataset']       = 'yourdataset'

bq = BigQuery::Client.new(opts)

puts bq.tables

Tables

List tables in dataset

bq.tables

List table names

bq.tables_formatted

Fetch table data

bq.table_data('table_name')

Delete exiting table

bq.delete_table('test123')

Create table. First param is the table name second one is the table schema defined with the following format

{
    field_name: {
        type: 'TYPE_VALUE BETWEEN (STRING, INTEGER, FLOAT, BOOLEAN, RECORD, TIMESTAMP)',
        mode: 'MODE_VALUE BETWEEN (NULLABLE, REQUIRED, REPEATED)'
    },
    other_field_name: { ... }
}

As this example defines

table_name = 'test123'
table_schema = { id: { type: 'INTEGER' },
                 name: { type: 'STRING' } }
bq.create_table(table_name, table_schema)

Describe table schema

bq.describe_table('table_name')

Datasets

List datasets in dataset

bq.datasets

List dataset names

bq.datasets_formatted

Delete exiting dataset

bq.delete_dataset('test123')

Create dataset. First param is the dataset name

bq.create_dataset('test123')

Querying

bq.query("SELECT * FROM [#{config['dataset']}.table_name] LIMIT 1")

Inserting

Insert a single row

bq.insert('table_name', 'id' => 123, 'type' => 'Task')

Batch insert an array of rows. See bigquery docs for limitations.

data = [{'id' => 123, 'type' => 'Foo'}, {'id' => 321, 'type' => 'Bar'}]
bq.insert('table_name', data)

Patching

Patching a exiting table

bq.patch_table('test', 'id' => 123, 'type' => 'Task', 'name' => 'Task1')

Tables: patch See bigquery docs for details.

Updating

Updating a exiting table

bq.update_table('test', 'id' => 123, 'type' => 'Task', 'name' => 'Task1')

Tables: update See bigquery docs for details.

Keys

To get the keys you need to have a:

1- Goto your project google api access

https://code.google.com/apis/console/b/0/?noredirect&pli=1#project:YOUR_PROJECT_ID:access

2- Create a new client-ID for service_account 3- Download de key file

Now you have everything:

  • client_id: API access client-ID
  • service_email: API access Email address
  • key: API access key file path
  • project_id: your google API project id
  • dataset: your big query dataset name

Troubleshooting

If you're getting an "invalid_grant" error it usually means your system clock is off.

If you're getting unauthorized requested but you've been able to successfully connect before, you need to refresh your auth by running the "refresh_auth" method.

How to run test

Before run test, you must create file named .bigquery_settings.yml on root of this repository. .bigquery_settings.yml must include following infomation.

client_id:     '1234.apps.googleusercontent.com'
service_email: '[email protected]'
key:           '/path/to/somekeyfile-privatekey.p12'
project_id:    '54321'
dataset:       'yourdataset'
faraday_option:
  timeout: 999

Then run tests via rake.

$ bundle install && bundle exec rake test

Contributing

Fork and submit a pull request and make sure you add a test for any feature you add.

License

LICENSE:

(The MIT License)

Copyright © 2012 Adam Bronte

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bigquery's People

Contributors

abronte avatar andresbravog avatar blak3mill3r avatar clairemcginty avatar derencius avatar digitalronin avatar dtaniwaki avatar goose42 avatar hakobera avatar kkosuge avatar kysnm avatar nateberkopec avatar prair-ppl avatar sonots avatar tanukiti1987 avatar trinityxmontoya avatar troynt avatar yukiyan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.