tumugi-plugin-bigquery 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ab49f7c9e04361951e1f7ab9eff4019d09c8829
4
+ data.tar.gz: 1b5ed8b53e77fb413a98ce6cfe4a682611b33f62
5
+ SHA512:
6
+ metadata.gz: ea2e037bc46885c0e7cec71d085a5883dd9508f597d6e98d275b11464d2a9f27596a26cfd4a9141f51090b9ab2c9a96df63eb27c59ebba12113c00e2cac4d239
7
+ data.tar.gz: 1e18b4578eda83a38d200896cf0b8e22a85726b08f956bd20e9e794d81322df18416004d58f977e78b053b3609e559064d04cfe683ab1ecc60d32e8f89f34067
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ tumugi_config.rb
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1.10
5
+ - 2.2.5
6
+ - 2.3.1
7
+ - jruby-9.0.5.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tumugi-plugin-bigquery.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ [![Build Status](https://travis-ci.org/tumugi/tumugi-plugin-bigquery.svg?branch=master)](https://travis-ci.org/tumugi/tumugi-plugin-bigquery) [![Code Climate](https://codeclimate.com/github/tumugi/tumugi-plugin-bigquery/badges/gpa.svg)](https://codeclimate.com/github/tumugi/tumugi-plugin-bigquery) [![Coverage Status](https://coveralls.io/repos/github/tumugi/tumugi-plugin-bigquery/badge.svg?branch=master)](https://coveralls.io/github/tumugi/tumugi-plugin-bigquery)
2
+
3
+ # tumugi-plugin-bigquery
4
+
5
+ tumugi-plugin-bigquery is a plugin for integrate [Google BigQuery](https://cloud.google.com/bigquery/) and [Tumugi](https://github.com/tumugi/tumugi).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tumugi-plugin-bigquery'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```sh
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```sb
24
+ $ gem install tumugi-plugin-bigquery
25
+ ```
26
+
27
+ ## Target
28
+
29
+ ### Tumugi::Plugin::BigqueryDatasetTarget
30
+
31
+ `Tumugi::Plugin::BigqueryDatasetTarget` is target for BigQuery dataset.
32
+
33
+ #### Tumugi::Plugin::BigqueryTableTarget
34
+
35
+ `Tumugi::Plugin::BigqueryDatasetTarget` is target for BigQuery table.
36
+
37
+ ## Task
38
+
39
+ ### Tumugi::Plugin::BigqueryDatasetTask
40
+
41
+ `Tumugi::Plugin::BigqueryDatasetTask` is task to create a dataset.
42
+
43
+ #### Usage
44
+
45
+ ```rb
46
+ task :task1, type: :bigquery_dataset do
47
+ param_set :dataset_id, 'test'
48
+ end
49
+ ```
50
+
51
+ ### Tumugi::Plugin::BigqueryQueryTask
52
+
53
+ `Tumugi::Plugin::BigqueryQueryTask` is task to run `query` and save the result into the table which specified by parameter.
54
+
55
+ #### Usage
56
+
57
+ ```rb
58
+ task :task1, type: :bigquery_query do
59
+ param_set :query, "SELECT COUNT(*) AS cnt FROM [bigquery-public-data:samples.wikipedia]"
60
+ param_set :dataset_id, 'test'
61
+ param_set :table_id, "dest_table#{Time.now.to_i}"
62
+ end
63
+ ```
64
+
65
+ ### Tumugi::Plugin::BigqueryCopyTask
66
+
67
+ `Tumugi::Plugin::BigqueryCopyTask` is task to copy table which specified by parameter.
68
+
69
+ #### Usage
70
+
71
+ ```rb
72
+ task :task1, type: :bigquery_copy do
73
+ param_set :src_dataset_id, 'test'
74
+ param_set :src_table_id, 'src_table'
75
+ param_set :dest_dataset_id, 'test'
76
+ param_set :dest_table_id, 'dest_table'
77
+ end
78
+ ```
79
+
80
+ ### Config Section
81
+
82
+ tumugi-plugin-bigquery provide config section named "bigquery" which can specified BigQuery autenticaion info.
83
+
84
+ #### Authenticate by client_email and private_key
85
+
86
+ ```rb
87
+ Tumugi.config do |config|
88
+ config.section("bigquery") do |section|
89
+ section.project_id = "xxx"
90
+ section.client_email = "yyy@yyy.iam.gserviceaccount.com"
91
+ section.private_key = "zzz"
92
+ end
93
+ end
94
+ ```
95
+
96
+ #### Authenticate by JSON key file
97
+
98
+ ```rb
99
+ Tumugi.config do |config|
100
+ config.section("bigquery") do |section|
101
+ section.private_key_file = "/path/to/key.json"
102
+ end
103
+ end
104
+ ```
105
+
106
+ ## Development
107
+
108
+ After checking out the repo, run `bin/setup` to install dependencies.
109
+ Then, export [Google Cloud Platform Service Accounts](https://cloud.google.com/iam/docs/service-accounts) key as following,
110
+
111
+ ```sh
112
+ export PROJECT_ID="xxx"
113
+ export CLIENT_EMAIL="yyy@yyy.iam.gserviceaccount.com"
114
+ export PRIVATE_KEY="zzz"
115
+ ```
116
+
117
+ Then run `bundle exec rake test` to run the tests.
118
+
119
+ ## Contributing
120
+
121
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tumugi/tumugi-plugin-bigquery.
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [Apache License
126
+ Version 2.0](http://www.apache.org/licenses/).
data/Rakefile ADDED
@@ -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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/examples/copy.rb ADDED
@@ -0,0 +1,21 @@
1
+ task :task1, type: :bigquery_copy do
2
+ param_set :src_project_id, ->{ input.project_id }
3
+ param_set :src_dataset_id, ->{ input.dataset_id }
4
+ param_set :src_table_id, ->{ input.table_id }
5
+ param_set :dest_dataset_id, "test"
6
+ param_set :dest_table_id, ->{ "dest_table_#{Time.now.to_i}" }
7
+
8
+ requires :task2
9
+ end
10
+
11
+ task :task2, type: :bigquery_query do
12
+ param_set :query, "SELECT COUNT(*) AS cnt FROM [bigquery-public-data:samples.wikipedia]"
13
+ param_set :dataset_id, "test" #->{ input.dataset_id }
14
+ param_set :table_id, "dest_#{Time.now.to_i}"
15
+
16
+ requires :task3
17
+ end
18
+
19
+ task :task3, type: :bigquery_dataset do
20
+ param_set :dataset_id, "test"
21
+ end
@@ -0,0 +1,10 @@
1
+ task :task1 do
2
+ requires :task2
3
+ run do
4
+ log input.dataset_id
5
+ end
6
+ end
7
+
8
+ task :task2, type: :bigquery_dataset do
9
+ param_set :dataset_id, 'test'
10
+ end
data/examples/query.rb ADDED
@@ -0,0 +1,12 @@
1
+ task :task1 do
2
+ requires :task2
3
+ run do
4
+ log input.table_name
5
+ end
6
+ end
7
+
8
+ task :task2, type: :bigquery_query do
9
+ param_set :query, "SELECT COUNT(*) AS cnt FROM [bigquery-public-data:samples.wikipedia]"
10
+ param_set :dataset_id, 'test'
11
+ param_set :table_id, "dest_#{Time.now.to_i}"
12
+ end
@@ -0,0 +1,7 @@
1
+ Tumugi.config do |c|
2
+ c.section('bigquery') do |s|
3
+ s.project_id = ENV["PROJECT_ID"]
4
+ s.client_email = ENV["CLIENT_EMAIL"]
5
+ s.private_key = ENV["PRIVATE_KEY"].gsub(/\\n/, "\n")
6
+ end
7
+ end
@@ -0,0 +1,288 @@
1
+ require 'kura'
2
+ require_relative './error'
3
+
4
+ Tumugi::Config.register_section('bigquery', :project_id, :client_email, :private_key, :private_key_file)
5
+
6
+ module Tumugi
7
+ module Plugin
8
+ module Bigquery
9
+ class Client
10
+ attr_reader :project_id
11
+
12
+ def initialize(project_id: nil, client_email: nil, private_key: nil)
13
+ config = Tumugi.config.section('bigquery')
14
+ @project_id = project_id || config.project_id
15
+ @client_email = client_email || config.client_email
16
+ @private_key = private_key || config.private_key
17
+ @client = Kura.client(@project_id, @client_email, @private_key)
18
+ rescue Kura::ApiError => e
19
+ process_error(e)
20
+ end
21
+
22
+ def projects(limit: 1000, &blk)
23
+ @client.projects(limit: limit, &blk)
24
+ rescue Kura::ApiError => e
25
+ process_error(e)
26
+ end
27
+
28
+ def datasets(project_id: nil, all: false, limit: 1000, &blk)
29
+ @client.datasets(project_id: project_id || @project_id, all: all, limit: limit, &blk)
30
+ rescue Kura::ApiError => e
31
+ process_error(e)
32
+ end
33
+
34
+ def dataset(dataset_id, project_id: nil, &blk)
35
+ @client.dataset(dataset_id, project_id: project_id || @project_id, &blk)
36
+ rescue Kura::ApiError => e
37
+ process_error(e)
38
+ end
39
+
40
+ def dataset_exist?(dataset_id, project_id: nil)
41
+ !@client.dataset(dataset_id, project_id: project_id || @project_id).nil?
42
+ rescue Kura::ApiError => e
43
+ process_error(e)
44
+ end
45
+
46
+ def insert_dataset(dataset_id, project_id: nil, &blk)
47
+ @client.insert_dataset(dataset_id, project_id: project_id || @project_id, &blk)
48
+ rescue Kura::ApiError => e
49
+ process_error(e)
50
+ end
51
+
52
+ def delete_dataset(dataset_id, project_id: nil, delete_contents: true, &blk)
53
+ @client.delete_dataset(dataset_id, project_id: project_id || @project_id, delete_contents: delete_contents, &blk)
54
+ rescue Kura::ApiError => e
55
+ process_error(e)
56
+ end
57
+
58
+ def patch_dataset(dataset_id, project_id: nil,
59
+ access: nil,
60
+ description: :na,
61
+ default_table_expiration_ms: :na,
62
+ friendly_name: :na,
63
+ &blk)
64
+ @client.patch_dataset(dataset_id, project_id: project_id || @project_id,
65
+ access: access,
66
+ description: description,
67
+ default_table_expiration_ms: default_table_expiration_ms,
68
+ friendly_name: friendly_name,
69
+ &blk)
70
+ rescue Kura::ApiError => e
71
+ process_error(e)
72
+ end
73
+
74
+ def tables(dataset_id, project_id: nil, limit: 1000, &blk)
75
+ @client.tables(dataset_id, project_id: project_id || @project_id, limit: limit, &blk)
76
+ rescue Kura::ApiError => e
77
+ process_error(e)
78
+ end
79
+
80
+ def table_exist?(dataset_id, table_id, project_id: nil)
81
+ !@client.table(dataset_id, table_id, project_id: project_id || @project_id).nil?
82
+ rescue Kura::ApiError => e
83
+ process_error(e)
84
+ end
85
+
86
+ def insert_table(dataset_id, table_id,
87
+ project_id: nil,
88
+ expiration_time: nil,
89
+ friendly_name: nil,
90
+ schema: nil,
91
+ description: nil,
92
+ query: nil,
93
+ external_data_configuration: nil,
94
+ &blk)
95
+ @client.insert_table(dataset_id, table_id,
96
+ project_id: project_id || @project_id,
97
+ expiration_time: expiration_time,
98
+ friendly_name: friendly_name,
99
+ schema: schema,
100
+ description: description,
101
+ query: query,
102
+ external_data_configuration: external_data_configuration,
103
+ &blk)
104
+ rescue Kura::ApiError => e
105
+ process_error(e)
106
+ end
107
+
108
+ def delete_table(dataset_id, table_id, project_id: nil, &blk)
109
+ @client.delete_table(dataset_id, table_id, project_id: project_id || @project_id, &blk)
110
+ rescue Kura::ApiError => e
111
+ process_error(e)
112
+ end
113
+
114
+ def patch_table(dataset_id, table_id,
115
+ project_id: nil,
116
+ expiration_time: :na,
117
+ friendly_name: :na,
118
+ description: :na,
119
+ &blk)
120
+ @client.patch_table(dataset_id, table_id,
121
+ project_id: project_id || @project_id,
122
+ expiration_time: expiration_time,
123
+ friendly_name: friendly_name,
124
+ description: description,
125
+ &blk)
126
+ rescue Kura::ApiError => e
127
+ process_error(e)
128
+ end
129
+
130
+ def list_tabledata(dataset_id, table_id, project_id: nil, start_index: 0, max_result: 100, page_token: nil, schema: nil, &blk)
131
+ @client.list_tabledata(dataset_id, table_id,
132
+ project_id: project_id || @project_id,
133
+ start_index: start_index,
134
+ max_result: max_result,
135
+ page_token: page_token,
136
+ schema: schema,
137
+ &blk)
138
+ rescue Kura::ApiError => e
139
+ process_error(e)
140
+ end
141
+
142
+ def insert_tabledata(dataset_id, table_id, rows, project_id: nil, ignore_unknown_values: false, skip_invalid_rows: false, template_suffix: nil)
143
+ @client.insert_tabledata(dataset_id, table_id, rows,
144
+ project_id: project_id || @project_id,
145
+ ignore_unknown_values: ignore_unknown_values,
146
+ skip_invalid_rows: skip_invalid_rows,
147
+ template_suffix: template_suffix)
148
+ rescue Kura::ApiError => e
149
+ process_error(e)
150
+ end
151
+
152
+ def insert_job(configuration, job_id: nil, project_id: nil, media: nil, wait: nil, &blk)
153
+ @client.insert_job(configuration, job_id: job_id, project_id: project_id || @project_id, media: media, wait: wait, &blk)
154
+ rescue Kura::ApiError => e
155
+ process_error(e)
156
+ end
157
+
158
+ def query(sql, mode: :truncate,
159
+ dataset_id: nil, table_id: nil,
160
+ allow_large_results: true,
161
+ flatten_results: true,
162
+ priority: "INTERACTIVE",
163
+ use_query_cache: true,
164
+ user_defined_function_resources: nil,
165
+ project_id: nil,
166
+ job_id: nil,
167
+ wait: nil,
168
+ dry_run: false,
169
+ &blk)
170
+ @client.query(sql, mode: mode,
171
+ dataset_id: dataset_id, table_id: table_id,
172
+ allow_large_results: allow_large_results,
173
+ flatten_results: flatten_results,
174
+ priority: priority,
175
+ use_query_cache: use_query_cache,
176
+ user_defined_function_resources: user_defined_function_resources,
177
+ project_id: project_id || @project_id,
178
+ job_project_id: project_id || @project_id,
179
+ job_id: job_id,
180
+ wait: wait,
181
+ dry_run: dry_run,
182
+ &blk)
183
+ rescue Kura::ApiError => e
184
+ process_error(e)
185
+ end
186
+
187
+ def load(dataset_id, table_id, source_uris=nil,
188
+ schema: nil, delimiter: ",", field_delimiter: delimiter, mode: :append,
189
+ allow_jagged_rows: false, max_bad_records: 0,
190
+ ignore_unknown_values: false,
191
+ allow_quoted_newlines: false,
192
+ quote: '"', skip_leading_rows: 0,
193
+ source_format: "CSV",
194
+ project_id: nil,
195
+ job_id: nil,
196
+ file: nil, wait: nil,
197
+ dry_run: false,
198
+ &blk)
199
+ @client.load(dataset_id, table_id, source_uris=source_uris,
200
+ schema: schema, delimiter: delimiter, field_delimiter: field_delimiter, mode: mode,
201
+ allow_jagged_rows: allow_jagged_rows, max_bad_records: max_bad_records,
202
+ ignore_unknown_values: ignore_unknown_values,
203
+ allow_quoted_newlines: allow_quoted_newlines,
204
+ quote: quote, skip_leading_rows: skip_leading_rows,
205
+ source_format: source_format,
206
+ project_id: project_id || @project_id,
207
+ job_project_id: project_id || @project_id,
208
+ job_id: job_id,
209
+ file: file, wait: wait,
210
+ dry_run: dry_run,
211
+ &blk)
212
+ rescue Kura::ApiError => e
213
+ process_error(e)
214
+ end
215
+
216
+ def extract(dataset_id, table_id, dest_uris,
217
+ compression: "NONE",
218
+ destination_format: "CSV",
219
+ field_delimiter: ",",
220
+ print_header: true,
221
+ project_id: nil,
222
+ job_id: nil,
223
+ wait: nil,
224
+ dry_run: false,
225
+ &blk)
226
+ @client.extract(dataset_id, table_id, dest_uris,
227
+ compression: compression,
228
+ destination_format: destination_format,
229
+ field_delimiter: field_delimiter,
230
+ print_header: print_header,
231
+ project_id: project_id || @project_id,
232
+ job_project_id: project_id || @project_id,
233
+ job_id: job_id,
234
+ wait: wait,
235
+ dry_run: dry_run,
236
+ &blk)
237
+ rescue Kura::ApiError => e
238
+ process_error(e)
239
+ end
240
+
241
+ def copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id,
242
+ mode: :truncate,
243
+ src_project_id: nil,
244
+ dest_project_id: nil,
245
+ job_id: nil,
246
+ wait: nil,
247
+ dry_run: false,
248
+ &blk)
249
+ @client.copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id,
250
+ mode: mode,
251
+ src_project_id: src_project_id || @project_id,
252
+ dest_project_id: dest_project_id || @project_id,
253
+ job_project_id: dest_project_id || @project_id,
254
+ job_id: job_id,
255
+ wait: wait,
256
+ dry_run: dry_run,
257
+ &blk)
258
+ rescue Kura::ApiError => e
259
+ process_error(e)
260
+ end
261
+
262
+ def job(job_id, project_id: nil, &blk)
263
+ @client.job(job_id, project_id: project_id || @project_id, &blk)
264
+ rescue Kura::ApiError => e
265
+ process_error(e)
266
+ end
267
+
268
+ def cancel_job(job_id, project_id: nil, &blk)
269
+ @client.cancel_job(job_id, project_id: project_id || @project_id, &blk)
270
+ rescue Kura::ApiError => e
271
+ process_error(e)
272
+ end
273
+
274
+ def wait_job(job, timeout=60*10, project_id: nil, &blk)
275
+ @client.wait_job(job, timeout, project_id: project_id || @project_id, &blk)
276
+ rescue Kura::ApiError => e
277
+ process_error(e)
278
+ end
279
+
280
+ private
281
+
282
+ def process_error(e)
283
+ raise Tumugi::Plugin::Bigquery::BigqueryError.new(e.reason, e.message)
284
+ end
285
+ end
286
+ end
287
+ end
288
+ end
@@ -0,0 +1,26 @@
1
+ module Tumugi
2
+ module Plugin
3
+ module Bigquery
4
+ class Dataset
5
+ attr_reader :project_id, :dataset_id
6
+
7
+ def initialize(project_id:, dataset_id:)
8
+ @project_id = project_id
9
+ @dataset_id = dataset_id
10
+ end
11
+
12
+ def dataset_name
13
+ "#{dataset_id}"
14
+ end
15
+
16
+ def dataset_full_name
17
+ "#{project_id}:#{dataset_id}"
18
+ end
19
+
20
+ def to_s
21
+ "bq://#{project_id}/#{dataset_id}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'tumugi/error'
2
+
3
+ module Tumugi
4
+ module Plugin
5
+ module Bigquery
6
+ class BigqueryError < Tumugi::TumugiError
7
+ def initialize(reason, message)
8
+ @reason = reason
9
+ super(message)
10
+ end
11
+ attr_reader :reason
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require_relative './dataset'
2
+
3
+ module Tumugi
4
+ module Plugin
5
+ module Bigquery
6
+ class Table
7
+ attr_reader :project_id, :dataset_id, :table_id
8
+
9
+ def initialize(project_id:, dataset_id:, table_id:)
10
+ @project_id = project_id
11
+ @dataset_id = dataset_id
12
+ @table_id = table_id
13
+ end
14
+
15
+ def dataset
16
+ Tumugi::Plugin::Bigquery::Dataset.new(project_id: @project_id, dataset_id: @dataset_id)
17
+ end
18
+
19
+ def table_name
20
+ "#{dataset_id}.#{table_id}"
21
+ end
22
+
23
+ def table_full_name
24
+ "#{project_id}:#{dataset_id}.#{table_id}"
25
+ end
26
+
27
+ def to_s
28
+ "bq://#{project_id}/#{dataset_id}/#{table_id}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module Tumugi
2
+ module Plugin
3
+ module Bigquery
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'forwardable'
2
+ require 'tumugi'
3
+ require_relative '../bigquery/client'
4
+ require_relative '../bigquery/dataset'
5
+
6
+ module Tumugi
7
+ module Plugin
8
+ class BigqueryDatasetTarget
9
+ Tumugi::Plugin.register_target('bigquery_dataset', self)
10
+
11
+ attr_reader :project_id, :dataset_id, :client
12
+
13
+ extend Forwardable
14
+ def_delegators :@dataset, :dataset_name, :dataset_full_name, :to_s
15
+
16
+ def initialize(project_id: nil, dataset_id:, client: nil)
17
+ cfg = Tumugi.config.section('bigquery')
18
+ @project_id = project_id || cfg.project_id
19
+ @dataset_id = dataset_id
20
+ @client = client || Tumugi::Plugin::Bigquery::Client.new(project_id: @project_id)
21
+ @dataset = Tumugi::Plugin::Bigquery::Dataset.new(project_id: @project_id, dataset_id: @dataset_id)
22
+ end
23
+
24
+ def exist?
25
+ @client.dataset_exist?(@dataset_id, project_id: @project_id)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ require 'forwardable'
2
+ require 'tumugi'
3
+ require_relative '../bigquery/client'
4
+ require_relative '../bigquery/table'
5
+
6
+ module Tumugi
7
+ module Plugin
8
+ class BigqueryTableTarget
9
+ Tumugi::Plugin.register_target('bigquery_table', self)
10
+
11
+ extend Forwardable
12
+ def_delegators :@table, :table_name, :table_full_name, :to_s
13
+
14
+ attr_reader :project_id, :dataset_id, :table_id, :client
15
+
16
+ def initialize(project_id: nil, dataset_id:, table_id:, client: nil)
17
+ cfg = Tumugi.config.section('bigquery')
18
+ @project_id = project_id || cfg.project_id
19
+ @dataset_id = dataset_id
20
+ @table_id = table_id
21
+ @client = client || Tumugi::Plugin::Bigquery::Client.new(project_id: @project_id)
22
+ @table = Tumugi::Plugin::Bigquery::Table.new(project_id: @project_id, dataset_id: @dataset_id, table_id: @table_id)
23
+ end
24
+
25
+ def exist?
26
+ @client.table_exist?(@dataset_id, @table_id, project_id: @project_id)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ require 'tumugi'
2
+ require_relative '../target/bigquery_table'
3
+
4
+ module Tumugi
5
+ module Plugin
6
+ class BigqueryCopyTask < Tumugi::Task
7
+ Tumugi::Plugin.register_task('bigquery_copy', self)
8
+
9
+ param :src_project_id, type: :string
10
+ param :src_dataset_id, type: :string, required: true
11
+ param :src_table_id, type: :string, required: true
12
+ param :dest_project_id, type: :string
13
+ param :dest_dataset_id, type: :string, required: true
14
+ param :dest_table_id, type: :string, required: true
15
+ param :wait, type: :int, default: 60
16
+
17
+ def output
18
+ opts = { dataset_id: dest_dataset_id, table_id: dest_table_id }
19
+ opts[:project_id] = dest_project_id if dest_project_id
20
+ Tumugi::Plugin::BigqueryTableTarget.new(opts)
21
+ end
22
+
23
+ def run
24
+ log "Source: bq://#{src_project_id}/#{src_dataset_id}/#{src_table_id}"
25
+ log "Destination: #{output}"
26
+
27
+ bq_client = output.client
28
+ opts = { wait: wait }
29
+ opts[:src_project_id] = src_project_id if src_project_id
30
+ opts[:dest_project_id] = dest_project_id if dest_project_id
31
+ bq_client.copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id, opts)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ require 'tumugi'
2
+ require_relative '../target/bigquery_dataset'
3
+
4
+ module Tumugi
5
+ module Plugin
6
+ class BigqueryDatasetTask < Tumugi::Task
7
+ Tumugi::Plugin.register_task('bigquery_dataset', self)
8
+
9
+ param :project_id, type: :string
10
+ param :dataset_id, type: :string, required: true
11
+
12
+ def output
13
+ Tumugi::Plugin::BigqueryDatasetTarget.new(project_id: project_id, dataset_id: dataset_id)
14
+ end
15
+
16
+ def run
17
+ log "Dataset: #{output}"
18
+ if output.exist?
19
+ log "skip: #{output} is already exists"
20
+ else
21
+ bq_client = output.client
22
+ bq_client.insert_dataset(dataset_id, project_id: project_id)
23
+ log "run: #{output}"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ require 'tumugi'
2
+ require_relative '../target/bigquery_table'
3
+
4
+ module Tumugi
5
+ module Plugin
6
+ class BigqueryQueryTask < Tumugi::Task
7
+ Tumugi::Plugin.register_task('bigquery_query', self)
8
+
9
+ param :query, type: :string, required: true
10
+ param :project_id, type: :string
11
+ param :dataset_id, type: :string, required: true
12
+ param :table_id, type: :string, required: true
13
+ param :wait, type: :int, default: 60
14
+
15
+ def output
16
+ Tumugi::Plugin::BigqueryTableTarget.new(project_id: project_id, dataset_id: dataset_id, table_id: table_id)
17
+ end
18
+
19
+ def run
20
+ log "Launching Query"
21
+ log "Query: #{query}"
22
+ log "Query destination: #{output}"
23
+
24
+ bq_client = output.client
25
+ bq_client.query(query, project_id: project_id, dataset_id: output.dataset_id, table_id: output.table_id, wait: wait)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tumugi/plugin/bigquery/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tumugi-plugin-bigquery"
8
+ spec.version = Tumugi::Plugin::Bigquery::VERSION
9
+ spec.authors = ["Kazuyuki Honda"]
10
+ spec.email = ["hakobera@gmail.com"]
11
+
12
+ spec.summary = "Tumugi plugin for Google BigQuery"
13
+ spec.homepage = "https://github.com/tumugi/tumugi-plugin-bigquery"
14
+ spec.license = "Apache License Version 2.0"
15
+
16
+ spec.required_ruby_version = '>= 2.1'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "tumugi", "~> 0.4.5"
24
+ spec.add_runtime_dependency "kura", "0.2.16"
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.11'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'test-unit', '~> 3.1'
29
+ spec.add_development_dependency 'test-unit-rr'
30
+ spec.add_development_dependency 'coveralls'
31
+ spec.add_development_dependency 'github_changelog_generator'
32
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tumugi-plugin-bigquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kazuyuki Honda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tumugi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.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.4.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: kura
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.16
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.16
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit-rr
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: coveralls
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
+ - !ruby/object:Gem::Dependency
112
+ name: github_changelog_generator
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - hakobera@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".travis.yml"
134
+ - Gemfile
135
+ - README.md
136
+ - Rakefile
137
+ - bin/setup
138
+ - examples/copy.rb
139
+ - examples/dataset.rb
140
+ - examples/query.rb
141
+ - examples/tumugi_config_example.rb
142
+ - lib/tumugi/plugin/bigquery/client.rb
143
+ - lib/tumugi/plugin/bigquery/dataset.rb
144
+ - lib/tumugi/plugin/bigquery/error.rb
145
+ - lib/tumugi/plugin/bigquery/table.rb
146
+ - lib/tumugi/plugin/bigquery/version.rb
147
+ - lib/tumugi/plugin/target/bigquery_dataset.rb
148
+ - lib/tumugi/plugin/target/bigquery_table.rb
149
+ - lib/tumugi/plugin/task/bigquery_copy.rb
150
+ - lib/tumugi/plugin/task/bigquery_dataset.rb
151
+ - lib/tumugi/plugin/task/bigquery_query.rb
152
+ - tumugi-plugin-bigquery.gemspec
153
+ homepage: https://github.com/tumugi/tumugi-plugin-bigquery
154
+ licenses:
155
+ - Apache License Version 2.0
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '2.1'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.5.1
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Tumugi plugin for Google BigQuery
177
+ test_files: []