bigbroda 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +25 -0
- data/.pryrc +3 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +408 -0
- data/Rakefile +12 -0
- data/google_bigquery.gemspec +30 -0
- data/lib/.DS_Store +0 -0
- data/lib/active_record/.DS_Store +0 -0
- data/lib/active_record/connection_adapters/bigquery_adapter.rb +949 -0
- data/lib/active_record/tasks/bigquery_database_tasks.rb +42 -0
- data/lib/generators/.DS_Store +0 -0
- data/lib/generators/google_bigquery/.DS_Store +0 -0
- data/lib/generators/google_bigquery/install/install_generator.rb +21 -0
- data/lib/generators/templates/README +11 -0
- data/lib/generators/templates/bigquery.rb.erb +7 -0
- data/lib/google_bigquery/auth.rb +27 -0
- data/lib/google_bigquery/client.rb +52 -0
- data/lib/google_bigquery/config.rb +17 -0
- data/lib/google_bigquery/dataset.rb +77 -0
- data/lib/google_bigquery/engine.rb +21 -0
- data/lib/google_bigquery/jobs.rb +173 -0
- data/lib/google_bigquery/project.rb +16 -0
- data/lib/google_bigquery/railtie.rb +39 -0
- data/lib/google_bigquery/table.rb +63 -0
- data/lib/google_bigquery/table_data.rb +23 -0
- data/lib/google_bigquery/version.rb +3 -0
- data/lib/google_bigquery.rb +27 -0
- data/spec/.DS_Store +0 -0
- data/spec/dummy/.DS_Store +0 -0
- data/spec/dummy/.gitignore +20 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/stylesheets/application.css.scss +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/helpers/application_helper.rb +3 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/log_data.rb +3 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/models/user.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.haml +32 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +11 -0
- data/spec/dummy/config/database.yml +32 -0
- data/spec/dummy/config/environment.rb +6 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +8 -0
- data/spec/dummy/config/initializers/bigquery.rb +19 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +6 -0
- data/spec/dummy/config/initializers/secret_token.rb +8 -0
- data/spec/dummy/config/initializers/session_store.rb +9 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +15 -0
- data/spec/dummy/config/locales/devise.en.yml +58 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/locales/simple_form.en.yml +26 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20140224051640_create_users.rb +11 -0
- data/spec/dummy/db/migrate/20140224063709_add_last_name_to_user.rb +5 -0
- data/spec/dummy/db/migrate/20140225014314_create_log_data.rb +12 -0
- data/spec/dummy/db/migrate/20140227015551_create_posts.rb +9 -0
- data/spec/dummy/db/schema.rb +39 -0
- data/spec/dummy/db/schema_migrations.json +1 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/lib/templates/erb/scaffold/_form.html.erb +13 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/test/fixtures/log_data.yml +9 -0
- data/spec/dummy/test/fixtures/posts.yml +11 -0
- data/spec/dummy/test/fixtures/users.yml +11 -0
- data/spec/dummy/test/models/log_data_test.rb +7 -0
- data/spec/dummy/test/models/post_test.rb +7 -0
- data/spec/dummy/test/models/user_test.rb +7 -0
- data/spec/fixtures/.DS_Store +0 -0
- data/spec/fixtures/configs/account_config.yml-example +6 -0
- data/spec/fixtures/keys/.DS_Store +0 -0
- data/spec/fixtures/keys/example-privatekey-p12 +0 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/.DS_Store +0 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/adapter/simple_quering.yml +324 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/after_each.yml +154 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/authorize_config.yml +367 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/create_each.yml +195 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/migrations/_down/adds_the_email_at_utc_hour_column.yml +575 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/migrations/_up/adds_the_created_at_updated_at_column.yml +644 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/migrations/add_column/adds_published_column.yml +779 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/migrations/associations/users_posts.yml +1464 -0
- data/spec/fixtures/vcr_cassettes/ActiveRecord_Adapter/migrations/remove_column/should_raise_error.yml +713 -0
- data/spec/fixtures/vcr_cassettes/Dataset/_list.yml +64 -0
- data/spec/fixtures/vcr_cassettes/Dataset/authorize_config.yml +367 -0
- data/spec/fixtures/vcr_cassettes/Dataset/operations/_get_delete.yml +237 -0
- data/spec/fixtures/vcr_cassettes/Dataset/operations/_patch_delete.yml +240 -0
- data/spec/fixtures/vcr_cassettes/Dataset/operations/_update_delete.yml +297 -0
- data/spec/fixtures/vcr_cassettes/Dataset/operations/create_delete.yml +173 -0
- data/spec/fixtures/vcr_cassettes/Project/_list.yml +64 -0
- data/spec/fixtures/vcr_cassettes/Project/authorize_config.yml +2166 -0
- data/spec/fixtures/vcr_cassettes/Table/authorize_config.yml +367 -0
- data/spec/fixtures/vcr_cassettes/Table/operations/creation_edition/_create_delete.yml +404 -0
- data/spec/fixtures/vcr_cassettes/Table/operations/creation_edition/_create_update_delete.yml +471 -0
- data/spec/fixtures/vcr_cassettes/Table/operations/list.yml +232 -0
- data/spec/fixtures/vcr_cassettes/TableData/authorize_config.yml +2166 -0
- data/spec/fixtures/vcr_cassettes/TableData/create_each.yml +135 -0
- data/spec/fixtures/vcr_cassettes/TableData/delete_each.yml +154 -0
- data/spec/fixtures/vcr_cassettes/TableData/insertAll2.yml +189 -0
- data/spec/fixtures/vcr_cassettes/auth.yml +2168 -0
- data/spec/fixtures/vcr_cassettes/authorize_config.yml +2166 -0
- data/spec/fixtures/vcr_cassettes/datasets.yml +119 -0
- data/spec/fixtures/vcr_cassettes/delete_each_dataset.yml +48 -0
- data/spec/functional/adapter/adapter_spec.rb +213 -0
- data/spec/functional/auth_spec.rb +24 -0
- data/spec/functional/client_spec.rb +9 -0
- data/spec/functional/config_spec.rb +24 -0
- data/spec/functional/dataset_spec.rb +77 -0
- data/spec/functional/project_spec.rb +24 -0
- data/spec/functional/table_data_spec.rb +61 -0
- data/spec/functional/table_spec.rb +105 -0
- data/spec/models/user_spec.rb +0 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/support/models.rb +11 -0
- data/spec/support/schema.rb +43 -0
- metadata +370 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Tasks # :nodoc:
|
|
3
|
+
#extend ActiveSupport::Autoload
|
|
4
|
+
|
|
5
|
+
class BigQueryDatabaseTasks
|
|
6
|
+
|
|
7
|
+
include ActiveRecord::Tasks::DatabaseTasks
|
|
8
|
+
|
|
9
|
+
delegate :connection, :establish_connection, to: ActiveRecord::Base
|
|
10
|
+
|
|
11
|
+
ActiveRecord::Tasks::DatabaseTasks.register_task(/bigquery/, ActiveRecord::Tasks::BigQueryDatabaseTasks)
|
|
12
|
+
|
|
13
|
+
def create
|
|
14
|
+
establish_connection configuration
|
|
15
|
+
connection.create_database configuration['database']
|
|
16
|
+
establish_connection configuration
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def drop
|
|
20
|
+
establish_connection configuration
|
|
21
|
+
connection.drop_database configuration['database']
|
|
22
|
+
establish_connection configuration
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(configuration)
|
|
26
|
+
@configuration = configuration
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def configuration
|
|
32
|
+
@configuration
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def configuration_without_database
|
|
36
|
+
configuration.merge('database' => nil)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
module GoogleBigquery
|
|
4
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
5
|
+
|
|
6
|
+
source_root File.expand_path("../../../templates", __FILE__)
|
|
7
|
+
|
|
8
|
+
desc "Creates a BigQuery initializer."
|
|
9
|
+
#class_option :orm
|
|
10
|
+
|
|
11
|
+
def copy_initializer
|
|
12
|
+
say_status("installing", "BigQuery", :green)
|
|
13
|
+
copy_file "bigquery.rb.erb", "config/initializers/bigquery.rb"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def show_readme
|
|
17
|
+
readme "README" if behavior == :invoke
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
Hey, Thanks for install BigBroda
|
|
3
|
+
In order to make Google BigQuery work you will need to follow these steps:
|
|
4
|
+
|
|
5
|
+
1. register your api access in: google api console at https://code.google.com/apis/console/) and create a server key.
|
|
6
|
+
|
|
7
|
+
2. Download the p12 key. and save it in your app ie:. config/keys/xxx.p12
|
|
8
|
+
|
|
9
|
+
3. Configure options in config/initializers/bigquery.rb
|
|
10
|
+
|
|
11
|
+
Enjoy :)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
GoogleBigquery::Config.setup do |config|
|
|
2
|
+
#config.pass_phrase = "notasecret"
|
|
3
|
+
#config.key_file = Rails.root + "/config/XXXXkey_file.p12"
|
|
4
|
+
#config.client_id = "XXXXX.apps.googleusercontent.com"
|
|
5
|
+
#config.scope = "https://www.googleapis.com/auth/bigquery"
|
|
6
|
+
#config.email = "XXXXXX@developer.gserviceaccount.com"
|
|
7
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module GoogleBigquery
|
|
2
|
+
class Auth
|
|
3
|
+
|
|
4
|
+
attr_accessor :api, :client
|
|
5
|
+
cattr_accessor :api, :client
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
config = GoogleBigquery::Config
|
|
9
|
+
@key = Google::APIClient::PKCS12.load_key(config.key_file, config.pass_phrase)
|
|
10
|
+
@asserter = Google::APIClient::JWTAsserter.new( config.email, config.scope, @key)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def authorize
|
|
14
|
+
@client = Google::APIClient.new()
|
|
15
|
+
@client.authorization = @asserter.authorize()
|
|
16
|
+
@api = @client.discovered_api("bigquery",'v2')
|
|
17
|
+
self.class.api = @api
|
|
18
|
+
self.class.client = @client
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.authorized?
|
|
22
|
+
client.present?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module GoogleBigquery
|
|
2
|
+
class Client
|
|
3
|
+
|
|
4
|
+
attr_accessor :options, :api, :client
|
|
5
|
+
|
|
6
|
+
def initialize(opts = {})
|
|
7
|
+
|
|
8
|
+
@api ||= GoogleBigquery::Auth.api
|
|
9
|
+
@client ||= GoogleBigquery::Auth.client
|
|
10
|
+
#@auth.authorize # check expiration and cache ?
|
|
11
|
+
|
|
12
|
+
self.tap do |client|
|
|
13
|
+
client.options ||= {}
|
|
14
|
+
client.defaults_options(opts)
|
|
15
|
+
client.options ||= opts
|
|
16
|
+
yield client if block_given?
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def defaults_options(opts)
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def parse_response(res)
|
|
25
|
+
JSON.parse(res.body)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.parse_response(res)
|
|
29
|
+
raise_detected_errors(res)
|
|
30
|
+
JSON.parse(res.body)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def merge_options(name, opts)
|
|
36
|
+
@options.merge! name => opts
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def raise_detected_errors
|
|
40
|
+
body = JSON.parse(@results.body)
|
|
41
|
+
raise body["error"]["errors"].collect{|e| "#{e["reason"]}: #{e["message"]}" }.join(", ") if body.keys.include?("error")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.raise_detected_errors(res)
|
|
45
|
+
body = JSON.parse(res.body)
|
|
46
|
+
raise body["error"]["errors"].map{|o| "[BigQuery: #{o['domain']}]: #{o['reason']} #{o['message']}" }.join(", ") if body.has_key?("error")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require "active_support/core_ext/module/attribute_accessors"
|
|
2
|
+
|
|
3
|
+
module GoogleBigquery
|
|
4
|
+
class Config
|
|
5
|
+
|
|
6
|
+
def self.setup
|
|
7
|
+
yield self
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
mattr_accessor :pass_phrase,
|
|
11
|
+
:key_file,
|
|
12
|
+
:client_id,
|
|
13
|
+
:scope,
|
|
14
|
+
:profile_id,
|
|
15
|
+
:email
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
|
|
2
|
+
module GoogleBigquery
|
|
3
|
+
class Dataset < GoogleBigquery::Client
|
|
4
|
+
|
|
5
|
+
attr_accessor :options
|
|
6
|
+
|
|
7
|
+
def initialize( opts={})
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.list(project_id)
|
|
12
|
+
parse_response GoogleBigquery::Auth.client.execute(
|
|
13
|
+
GoogleBigquery::Auth.api.datasets.list,
|
|
14
|
+
projectId: project_id
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.get(project_id, dataset_id)
|
|
19
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
20
|
+
:api_method=> GoogleBigquery::Auth.api.datasets.get,
|
|
21
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id }
|
|
22
|
+
)
|
|
23
|
+
parse_response(res)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.update(project_id, dataset_id, body={})
|
|
27
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
28
|
+
:api_method=> GoogleBigquery::Auth.api.datasets.update,
|
|
29
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
30
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id }
|
|
31
|
+
)
|
|
32
|
+
parse_response(res)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.patch(project_id, dataset_id, body={})
|
|
36
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
37
|
+
:api_method=> GoogleBigquery::Auth.api.datasets.update,
|
|
38
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
39
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id }
|
|
40
|
+
)
|
|
41
|
+
parse_response(res)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.create(project_id, body={})
|
|
45
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
46
|
+
:api_method=> GoogleBigquery::Auth.api.datasets.insert,
|
|
47
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
48
|
+
:parameters=> {"projectId"=> project_id }
|
|
49
|
+
)
|
|
50
|
+
parse_response(res)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def delete(opts)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.delete(project_id, dataset_id, body={})
|
|
57
|
+
|
|
58
|
+
tables = GoogleBigquery::Table.list(project_id, dataset_id)["tables"]
|
|
59
|
+
|
|
60
|
+
unless tables.nil? or tables.empty?
|
|
61
|
+
tables.map!{|o| o["tableReference"]["tableId"]}
|
|
62
|
+
tables.each do |table_id|
|
|
63
|
+
GoogleBigquery::Table.delete(project_id, dataset_id, table_id)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
68
|
+
:api_method=> GoogleBigquery::Auth.api.datasets.delete,
|
|
69
|
+
#:body_object=> {"deleteContents"=> true},
|
|
70
|
+
:parameters=> {"projectId"=> project_id, "datasetId" => dataset_id }
|
|
71
|
+
)
|
|
72
|
+
res.status == 204 ? true : parse_response(res)
|
|
73
|
+
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
|
2
|
+
|
|
3
|
+
require "active_record/connection_adapters/bigquery_adapter.rb"
|
|
4
|
+
|
|
5
|
+
module GoogleBigquery
|
|
6
|
+
class Engine < ::Rails::Engine
|
|
7
|
+
|
|
8
|
+
isolate_namespace GoogleBigquery
|
|
9
|
+
#config.generators do |g|
|
|
10
|
+
# g.test_framework :rspec,
|
|
11
|
+
# :fixture_replacement => :factory_girl ,
|
|
12
|
+
# :dir => "spec/factories"
|
|
13
|
+
# g.integration_tool :rspec
|
|
14
|
+
#end
|
|
15
|
+
|
|
16
|
+
#initializer "require GoogleBigquery" do
|
|
17
|
+
#end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
module GoogleBigquery
|
|
2
|
+
class Jobs < GoogleBigquery::Client
|
|
3
|
+
|
|
4
|
+
def initialize(client=nil, opts={})
|
|
5
|
+
@client = client
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
#query
|
|
9
|
+
#Runs a BigQuery SQL query synchronously and returns query results if the query completes within a specified timeout.
|
|
10
|
+
def self.query(project_id, body={})
|
|
11
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
12
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.query,
|
|
13
|
+
:body_object=> body,
|
|
14
|
+
:parameters=> {"projectId"=> project_id}
|
|
15
|
+
)
|
|
16
|
+
parse_response(res)
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
#Retrieves the specified job by ID.
|
|
21
|
+
def self.get(project_id , job_id)
|
|
22
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
23
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.get,
|
|
24
|
+
:parameters=> {"projectId"=> project_id, "jobId"=>job_id}
|
|
25
|
+
)
|
|
26
|
+
#parse_response(res)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
#Retrieves the results of a query job.
|
|
30
|
+
def self.getQueryResults(project_id , job_id, body={})
|
|
31
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
32
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.get_query_results,
|
|
33
|
+
:body_object=> body,
|
|
34
|
+
:parameters=> {"projectId"=> project_id, "jobId"=>job_id}
|
|
35
|
+
)
|
|
36
|
+
parse_response(res)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
#Starts a new asynchronous job.
|
|
40
|
+
def self.insert(project_id, body={})
|
|
41
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
42
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.insert,
|
|
43
|
+
:body_object=> body,
|
|
44
|
+
:parameters=> {"projectId"=> project_id}
|
|
45
|
+
)
|
|
46
|
+
parse_response(res)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
#Lists all the Jobs in the specified project that were started by the user.
|
|
50
|
+
def self.list(project_id, body={})
|
|
51
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
52
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.list,
|
|
53
|
+
:body_object=> body,
|
|
54
|
+
:parameters=> {"projectId"=> project_id}
|
|
55
|
+
)
|
|
56
|
+
parse_response(res)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#export data
|
|
60
|
+
def self.export(project_id, dataset_id, table_id, bucket_location)
|
|
61
|
+
body = {'projectId'=> project_id,
|
|
62
|
+
'configuration'=> {
|
|
63
|
+
'extract'=> {
|
|
64
|
+
'sourceTable'=> {
|
|
65
|
+
'projectId'=> project_id,
|
|
66
|
+
'datasetId'=> dataset_id,
|
|
67
|
+
'tableId'=> table_id
|
|
68
|
+
},
|
|
69
|
+
'destinationUri'=> "gs://#{bucket_location}",
|
|
70
|
+
'destinationFormat'=> 'NEWLINE_DELIMITED_JSON'
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
76
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.insert,
|
|
77
|
+
:body_object=> body,
|
|
78
|
+
:parameters=> {"projectId"=> project_id}
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
job_id = JSON.parse(res.body)["jobReference"]["jobId"]
|
|
82
|
+
puts 'Waiting for export to complete..'
|
|
83
|
+
|
|
84
|
+
loop do
|
|
85
|
+
status = JSON.parse(self.get(project_id, job_id).body)
|
|
86
|
+
|
|
87
|
+
if 'DONE' == status['status']['state']
|
|
88
|
+
|
|
89
|
+
puts "Done exporting!"
|
|
90
|
+
if status["status"]["errors"]
|
|
91
|
+
puts status["status"]["errors"].map{|o| "#{o['reason']} : #{o['message']}"}
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
end
|
|
97
|
+
sleep(10)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def self.load(project_id, dataset_id, table_id, sources, fields)
|
|
102
|
+
body = { 'projectId'=> project_id,
|
|
103
|
+
'configuration'=> {
|
|
104
|
+
'load'=> {
|
|
105
|
+
'sourceUri' => sources.first,
|
|
106
|
+
'sourceUris' => sources,
|
|
107
|
+
'schema' => {
|
|
108
|
+
"fields"=> fields
|
|
109
|
+
},
|
|
110
|
+
'destinationTable'=> {
|
|
111
|
+
'projectId'=> project_id,
|
|
112
|
+
'datasetId'=> dataset_id,
|
|
113
|
+
'tableId'=> table_id
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
119
|
+
:api_method=> GoogleBigquery::Auth.api.jobs.insert,
|
|
120
|
+
:body_object=> body,
|
|
121
|
+
:parameters=> {"projectId"=> project_id}
|
|
122
|
+
)
|
|
123
|
+
#binding.pry
|
|
124
|
+
job_id = JSON.parse(res.body)["jobReference"]["jobId"]
|
|
125
|
+
puts 'Waiting for import to complete..'
|
|
126
|
+
|
|
127
|
+
loop do
|
|
128
|
+
status = JSON.parse(self.get(project_id, job_id).body)
|
|
129
|
+
|
|
130
|
+
if 'DONE' == status['status']['state']
|
|
131
|
+
|
|
132
|
+
puts "Done loading!"
|
|
133
|
+
if status["status"]["errors"]
|
|
134
|
+
puts status["status"]["errors"].map{|o| "#{o['reason']} : #{o['message']}"}
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
return
|
|
138
|
+
|
|
139
|
+
end
|
|
140
|
+
sleep(10)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def self.import()
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def self.copy()
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
def self.build_body_object(options)
|
|
153
|
+
project_id = options[:project_id]
|
|
154
|
+
dataset_id = options[:dataset_id]
|
|
155
|
+
table_id = options[:table_id]
|
|
156
|
+
bucket_location = options[:bucket_location]
|
|
157
|
+
{'projectId'=> project_id,
|
|
158
|
+
'configuration'=> {
|
|
159
|
+
'extract'=> {
|
|
160
|
+
'sourceTable'=> {
|
|
161
|
+
'projectId'=> project_id,
|
|
162
|
+
'datasetId'=> dataset_id,
|
|
163
|
+
'tableId'=> table_id
|
|
164
|
+
},
|
|
165
|
+
'destinationUri'=> "gs://#{bucket_location}",
|
|
166
|
+
'destinationFormat'=> 'NEWLINE_DELIMITED_JSON'
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
module GoogleBigquery
|
|
3
|
+
class Project < GoogleBigquery::Client
|
|
4
|
+
|
|
5
|
+
attr_accessor :options
|
|
6
|
+
|
|
7
|
+
def initialize( opts={})
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.list
|
|
12
|
+
parse_response GoogleBigquery::Auth.client.execute( GoogleBigquery::Auth.api.projects.list)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
module GoogleBigquery
|
|
4
|
+
class Railtie < ::Rails::Railtie
|
|
5
|
+
config.before_configuration do
|
|
6
|
+
require "#{Rails.root}/config/initializers/bigquery"
|
|
7
|
+
GoogleBigquery::Auth.new.authorize
|
|
8
|
+
# if config.action_view.javascript_expansions
|
|
9
|
+
# config.action_view.javascript_expansions[:high_charts] |= %w(highcharts exporting)
|
|
10
|
+
# end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
config.after_initialize do
|
|
14
|
+
#Google::APIClient.logger = Rails.logger
|
|
15
|
+
Google::APIClient.logger = Logger.new("#{Rails.root}/log/bigquery-cli.log")
|
|
16
|
+
#Logger.new(STDOUT)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
rake_tasks do
|
|
20
|
+
require "active_record/base"
|
|
21
|
+
require "active_record/tasks/bigquery_database_tasks"
|
|
22
|
+
|
|
23
|
+
#ActiveRecord::Tasks::DatabaseTasks.seed_loader = Rails.application
|
|
24
|
+
#ActiveRecord::Tasks::DatabaseTasks.env = Rails.env
|
|
25
|
+
|
|
26
|
+
#namespace :db do
|
|
27
|
+
|
|
28
|
+
#end
|
|
29
|
+
|
|
30
|
+
#load "active_record/railties/databases.rake"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
module GoogleBigquery
|
|
3
|
+
class Table < GoogleBigquery::Client
|
|
4
|
+
|
|
5
|
+
attr_accessor :options
|
|
6
|
+
|
|
7
|
+
def initialize( opts={})
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.list(project_id, dataset_id)
|
|
12
|
+
parse_response GoogleBigquery::Auth.client.execute(
|
|
13
|
+
GoogleBigquery::Auth.api.tables.list,
|
|
14
|
+
projectId: project_id, datasetId: dataset_id
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.get(project_id, dataset_id, table_id)
|
|
19
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
20
|
+
:api_method=> GoogleBigquery::Auth.api.tables.get,
|
|
21
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id, "tableId"=> table_id }
|
|
22
|
+
)
|
|
23
|
+
parse_response(res)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.update(project_id, dataset_id, table_id, body={})
|
|
27
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
28
|
+
:api_method=> GoogleBigquery::Auth.api.tables.update,
|
|
29
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
30
|
+
:parameters=> {"projectId"=> project_id, "datasetId" => dataset_id, "tableId"=> table_id }
|
|
31
|
+
)
|
|
32
|
+
parse_response(res)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.patch(project_id, dataset_id, table_id, body={})
|
|
36
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
37
|
+
:api_method=> GoogleBigquery::Auth.api.tables.update,
|
|
38
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
39
|
+
:parameters=> {"projectId"=> project_id, "datasetId" => dataset_id, "tableId"=> table_id }
|
|
40
|
+
)
|
|
41
|
+
parse_response(res)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.create(project_id, dataset_id, body={})
|
|
45
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
46
|
+
:api_method=> GoogleBigquery::Auth.api.tables.insert,
|
|
47
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
48
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id }
|
|
49
|
+
)
|
|
50
|
+
parse_response(res)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.delete(project_id, dataset_id, table_id, body={})
|
|
54
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
55
|
+
:api_method=> GoogleBigquery::Auth.api.tables.delete,
|
|
56
|
+
#:body_object=> body, #{"deleteContents"=> false},
|
|
57
|
+
:parameters=> {"projectId"=> project_id, "datasetId" => dataset_id, "tableId"=> table_id }
|
|
58
|
+
)
|
|
59
|
+
res.status == 204 ? true : parse_response(res)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
|
|
2
|
+
module GoogleBigquery
|
|
3
|
+
class TableData < GoogleBigquery::Client
|
|
4
|
+
|
|
5
|
+
def self.create(project_id, dataset_id, table_id, body={})
|
|
6
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
7
|
+
:api_method=> GoogleBigquery::Auth.api.tabledata.insert_all,
|
|
8
|
+
:body_object=> body , #{"datasetReference"=> {"datasetId" =>"whoa"}},
|
|
9
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id, "tableId"=>table_id }
|
|
10
|
+
)
|
|
11
|
+
parse_response(res)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.list(project_id, dataset_id, table_id)
|
|
15
|
+
res = GoogleBigquery::Auth.client.execute(
|
|
16
|
+
:api_method=> GoogleBigquery::Auth.api.tabledata.list,
|
|
17
|
+
:parameters=> {"projectId"=> project_id, "datasetId"=> dataset_id, "tableId"=>table_id }
|
|
18
|
+
)
|
|
19
|
+
parse_response(res)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
|
2
|
+
|
|
3
|
+
require "google/api_client"
|
|
4
|
+
require "active_support"
|
|
5
|
+
|
|
6
|
+
module GoogleBigquery
|
|
7
|
+
|
|
8
|
+
autoload :VERSION, 'google_bigquery/version.rb'
|
|
9
|
+
autoload :Config, 'google_bigquery/config.rb'
|
|
10
|
+
autoload :Auth, 'google_bigquery/auth.rb'
|
|
11
|
+
autoload :Client, 'google_bigquery/client.rb'
|
|
12
|
+
autoload :Project, 'google_bigquery/project.rb'
|
|
13
|
+
autoload :Dataset, 'google_bigquery/dataset.rb'
|
|
14
|
+
autoload :Table, 'google_bigquery/table.rb'
|
|
15
|
+
autoload :TableData,'google_bigquery/table_data.rb'
|
|
16
|
+
autoload :Jobs, 'google_bigquery/jobs.rb'
|
|
17
|
+
|
|
18
|
+
if defined?(::Rails::Railtie)
|
|
19
|
+
autoload :Rails, 'google_bigquery/engine.rb' if ::Rails.version >= '3.1'
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
if defined?(::Rails::Railtie)
|
|
23
|
+
autoload :Rails, 'google_bigquery/engine.rb' if ::Rails.version >= '3.1'
|
|
24
|
+
require File.join(File.dirname(__FILE__), *%w[google_bigquery railtie]) if ::Rails.version.to_s >= '3.1'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
data/spec/.DS_Store
ADDED
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
|
2
|
+
#
|
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
|
6
|
+
|
|
7
|
+
# Ignore bundler config.
|
|
8
|
+
/.bundle
|
|
9
|
+
|
|
10
|
+
# Ignore the default SQLite database.
|
|
11
|
+
/db/*.sqlite3
|
|
12
|
+
/db/*.sqlite3-journal
|
|
13
|
+
|
|
14
|
+
/public/assets
|
|
15
|
+
# Ignore all logfiles and tempfiles.
|
|
16
|
+
/log/*.log
|
|
17
|
+
/tmp
|
|
18
|
+
|
|
19
|
+
*.rdb
|
|
20
|
+
/public/uploads
|