kura 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df031ec7381a9dfbb95f0f57a937358131add7cb
4
+ data.tar.gz: 873e797a516da04f5c3f7672c6ce3a02336c809a
5
+ SHA512:
6
+ metadata.gz: b8e971c7582ee24f13c98b31f8129439b38831074966217e6174ea94e1a932cf1346a222b30f2f9e72a27a91f9c059c173eddbce3dceddc6428afbae609e1c61
7
+ data.tar.gz: fdcbabf8bb94ba1016236fdce57cdfda61fadab796e345665e37ed3cb13e87ea08f2f1854d2ed29009b95077a7b4fde8e1e65845e11a0e360d24e6f8d99a402a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle/
11
+ /test/data/service_account.json
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kura.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chikanaga Tomoyuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Kura
2
+
3
+ Kura is an interface to the [BigQuery API v2](https://cloud.google.com/bigquery/docs/reference/v2/).
4
+ This is a wrapper of google-api-client.gem.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'kura'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install kura
21
+
22
+ ## Usage
23
+
24
+ ```
25
+ client = Kura.client(project_id, email, private_key)
26
+ client.load("dataset", "table", "gs://mybucket/data.csv", wait: 120)
27
+ client.query("dataset", "result", "SELECT * FROM [dataset.table];", wait: 120)
28
+ client.extract("dataset", "result", "gs://mybucket/extracted.csv", wait: 120)
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nagachika/kura.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kura"
5
+
6
+ service_account_json = File.expand_path("../../test/data/service_account.json", __FILE__)
7
+ if File.readable?(service_account_json)
8
+ obj = JSON.parse(File.binread(service_account_json))
9
+ @client = Kura.client(obj["project_id"], obj["client_email"], obj["private_key"])
10
+ end
11
+
12
+ require "pry"
13
+ Pry.start(@client)
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kura/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kura"
8
+ spec.version = Kura::VERSION
9
+ spec.authors = ["Chikanaga Tomoyuki"]
10
+ spec.email = ["nagachika@ruby-lang.org"]
11
+
12
+ spec.summary = %q{Interface to BigQuery API v2.}
13
+ spec.description = %q{Kura is an interfece to BigQUery API v2. It is a wrapper of google-api-client.}
14
+ spec.homepage = "https://github.com/nagachika/kura/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.has_rdoc = false
22
+
23
+ spec.required_ruby_version = '>= 2.1'
24
+
25
+ spec.add_runtime_dependency "google-api-client", "~> 0.8.5"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.10"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "test-unit"
30
+ spec.add_development_dependency "simplecov"
31
+ spec.add_development_dependency "test-unit-power_assert"
32
+ spec.add_development_dependency "pry"
33
+ end
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+
3
+ require "kura/version"
4
+ require "kura/client"
5
+
6
+ module Kura
7
+ class ApiError < RuntimeError
8
+ def initialize(reason, message)
9
+ @reason = reason
10
+ super(message)
11
+ end
12
+ attr_reader :reason
13
+ end
14
+
15
+ class TimeoutError < RuntimeError
16
+ end
17
+
18
+ def self.get_private_key(private_key)
19
+ private_key_file = nil
20
+ private_key_content = nil
21
+ if private_key.respond_to?(:to_path)
22
+ private_key_file = private_key.to_path
23
+ elsif private_key.is_a?(String) and File.readable?(private_key)
24
+ private_key_file = private_key
25
+ end
26
+ if private_key_file
27
+ private_key_content = File.binread(private_key_file)
28
+ else
29
+ private_key_content = private_key
30
+ end
31
+ if private_key_content
32
+ private_key = OpenSSL::PKey.read(private_key_content)
33
+ end
34
+ private_key
35
+ end
36
+
37
+ def self.client(project_id, email_address, private_key)
38
+ private_key = get_private_key(private_key)
39
+ self::Client.new(project_id, email_address, private_key)
40
+ end
41
+ end
@@ -0,0 +1,219 @@
1
+ # coding: utf-8
2
+
3
+ require "google/api_client"
4
+ require "kura/version"
5
+
6
+ module Kura
7
+ class Client
8
+ def initialize(project_id, email_address, private_key)
9
+ @project_id = project_id
10
+ @scope = "https://www.googleapis.com/auth/bigquery"
11
+ @email_address = email_address
12
+ @private_key = private_key
13
+ auth = Signet::OAuth2::Client.new(
14
+ token_credential_uri: "https://accounts.google.com/o/oauth2/token",
15
+ audience: "https://accounts.google.com/o/oauth2/token",
16
+ scope: @scope,
17
+ issuer: @email_address,
18
+ signing_key: @private_key)
19
+ @api = Google::APIClient.new(application_name: "Kura", application_version: Kura::VERSION, authorization: auth)
20
+ @api.authorization.fetch_access_token!
21
+ @bigquery_api = @api.discovered_api("bigquery", "v2")
22
+ end
23
+
24
+ def datasets(project_id: @project_id, all: false, limit: 1000)
25
+ r = @api.execute(api_method: @bigquery_api.datasets.list, parameters: { projectId: project_id, all: all, maxResult: limit })
26
+ unless r.success?
27
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
28
+ end
29
+ r.data.datasets
30
+ end
31
+
32
+ def dataset(dataset_id, project_id: @project_id)
33
+ r = @api.execute(api_method: @bigquery_api.datasets.get, parameters: { projectId: project_id, datasetId: dataset_id })
34
+ unless r.success?
35
+ if r.data.error["code"] == 404
36
+ return nil
37
+ else
38
+ raise Kura::ApiError.new(r.data.error["reason"], r.data.error["message"])
39
+ end
40
+ end
41
+ r.data
42
+ end
43
+
44
+ def insert_dataset(dataset_id)
45
+ r = @api.execute(api_method: @bigquery_api.datasets.insert, parameters: { projectId: @project_id }, body_object: { datasetReference: { datasetId: dataset_id } })
46
+ unless r.success?
47
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
48
+ end
49
+ r.data
50
+ end
51
+
52
+ def delete_dataset(dataset_id, delete_contents: false)
53
+ r = @api.execute(api_method: @bigquery_api.datasets.delete, parameters: { projectId: @project_id, datasetId: dataset_id, deleteContents: delete_contents })
54
+ unless r.success?
55
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
56
+ end
57
+ r.data
58
+ end
59
+
60
+ def patch_dataset(dataset_id, project_id: @project_id, access: nil, description: nil, default_table_expiration_ms: nil, friendly_name: nil )
61
+ body = {}
62
+ body["access"] = access if access
63
+ body["defaultTableExpirationMs"] = default_table_expiration_ms if default_table_expiration_ms
64
+ body["description"] = description if description
65
+ body["friendlyName"] = friendly_name if friendly_name
66
+ r = @api.execute(api_method: @bigquery_api.datasets.patch, parameters: { projectId: project_id, datasetId: dataset_id }, body_object: body)
67
+ unless r.success?
68
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
69
+ end
70
+ r.data
71
+ end
72
+
73
+ def table(dataset_id, table_id, project_id: @project_id)
74
+ params = { projectId: project_id, datasetId: dataset_id, tableId: table_id }
75
+ r = @api.execute(api_method: @bigquery_api.tables.get, parameters: params)
76
+ unless r.success?
77
+ if r.data["error"]["code"] == 404
78
+ return nil
79
+ else
80
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
81
+ end
82
+ end
83
+ r.data
84
+ end
85
+
86
+ def delete_table(dataset_id, table_id)
87
+ params = { projectId: @project_id, datasetId: dataset_id, tableId: table_id }
88
+ r = @api.execute(api_method: @bigquery_api.tables.delete, parameters: params)
89
+ unless r.success?
90
+ if r.data["error"]["code"] == 404
91
+ return nil
92
+ else
93
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
94
+ end
95
+ end
96
+ r.data
97
+ end
98
+
99
+ def mode_to_write_disposition(mode)
100
+ unless %i{ append truncate empty }.include?(mode)
101
+ raise "mode option should be one of :append, :truncate, :empty but #{mode}"
102
+ end
103
+ "WRITE_#{mode.to_s.upcase}"
104
+ end
105
+ private :mode_to_write_disposition
106
+
107
+ def insert_job(configuration, wait: nil)
108
+ params = { projectId: @project_id }
109
+ body = { configuration: configuration }
110
+ r = @api.execute(api_method: @bigquery_api.jobs.insert, parameters: params, body_object: body)
111
+ unless r.success?
112
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
113
+ end
114
+ if wait
115
+ wait_job(r.data.jobReference.jobId, wait)
116
+ else
117
+ r.data.jobReference.jobId
118
+ end
119
+ end
120
+
121
+ def query(dataset_id, table_id, sql, mode: :truncate, allow_large_result: true, wait: nil)
122
+ write_disposition = mode_to_write_disposition(mode)
123
+ configuration = {
124
+ query: {
125
+ query: sql,
126
+ destinationTable: { projectId: @project_id, datasetId: dataset_id, tableId: table_id },
127
+ writeDisposition: write_disposition,
128
+ allowLargeResults: allow_large_result,
129
+ }
130
+ }
131
+ insert_job(configuration, wait: wait)
132
+ end
133
+
134
+ def load(dataset_id, table_id, source_uris, schema, delimiter: ",", mode: :append, wait: nil)
135
+ write_disposition = mode_to_write_disposition(mode)
136
+ source_uris = [source_uris] if source_uris.is_a?(String)
137
+ configuration = {
138
+ load: {
139
+ sourceUris: source_uris,
140
+ destinationTable: {
141
+ projectId: @project_id,
142
+ datasetId: dataset_id,
143
+ tableId: table_id,
144
+ },
145
+ fieldDelimiter: delimiter,
146
+ writeDisposition: write_disposition,
147
+ schema: { fields: schema },
148
+ }
149
+ }
150
+ insert_job(configuration, wait: wait)
151
+ end
152
+
153
+ def extract(dataset_id, table_id, dest_uris, wait: nil)
154
+ dest_uris = [ dest_uris ] if dest_uris.is_a?(String)
155
+ configuration = {
156
+ extract: {
157
+ sourceTable: {
158
+ projectId: @project_id,
159
+ datasetId: dataset_id,
160
+ tableId: table_id,
161
+ },
162
+ destinationUris: dest_uris,
163
+ }
164
+ }
165
+ insert_job(configuration, wait: wait)
166
+ end
167
+
168
+ def copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id, mode: :truncate, wait: nil)
169
+ write_disposition = mode_to_write_disposition(mode)
170
+ configuration = {
171
+ copy: {
172
+ destinationTable: {
173
+ projectId: @project_id,
174
+ datasetId: dest_dataset_id,
175
+ tableId: dest_table_id,
176
+ },
177
+ sourceTable: {
178
+ projectId: @project_id,
179
+ datasetId: src_dataset_id,
180
+ tableId: src_table_id,
181
+ },
182
+ writeDisposition: write_disposition,
183
+ }
184
+ }
185
+ insert_job(configuration, wait: wait)
186
+ end
187
+
188
+ def job(job_id)
189
+ params = { projectId: @project_id, jobId: job_id }
190
+ r = @api.execute(api_method: @bigquery_api.jobs.get, parameters: params)
191
+ unless r.success?
192
+ raise Kura::ApiError.new(r.data["error"]["reason"], r.data["error"]["message"])
193
+ end
194
+ r.data
195
+ end
196
+
197
+ def job_finished?(job_id)
198
+ r = job(job_id)
199
+ if r.status.state == "DONE"
200
+ if r.status["errorResult"]
201
+ raise Kura::ApiError.new(r.status.errorResult.reason, r.status.errorResult.message)
202
+ end
203
+ return true
204
+ end
205
+ return false
206
+ end
207
+
208
+ def wait_job(job_id, timeout=60*10)
209
+ expire = Time.now + timeout
210
+ while expire > Time.now
211
+ if job_finished?(job_id)
212
+ return true
213
+ end
214
+ sleep 1
215
+ end
216
+ raise Kura::TimeoutError, "wait job timeout"
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,3 @@
1
+ module Kura
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kura
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chikanaga Tomoyuki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit-power_assert
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Kura is an interfece to BigQUery API v2. It is a wrapper of google-api-client.
112
+ email:
113
+ - nagachika@ruby-lang.org
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - kura.gemspec
126
+ - lib/kura.rb
127
+ - lib/kura/client.rb
128
+ - lib/kura/version.rb
129
+ homepage: https://github.com/nagachika/kura/
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '2.1'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.4.5
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Interface to BigQuery API v2.
153
+ test_files: []