Class: Tumugi::Plugin::Bigquery::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tumugi/plugin/bigquery/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id: nil, client_email: nil, private_key: nil, private_key_file: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tumugi/plugin/bigquery/client.rb', line 13

def initialize(project_id: nil, client_email: nil, private_key: nil, private_key_file: nil)
  @project_id = project_id

  if client_email.nil? && private_key.nil? && !private_key_file.nil?
    @client = Kura.client(private_key_file)
    if @project_id.nil?
      key = JSON.parse(File.read(private_key_file))
      @project_id = key['project_id']
    end
  else
    # This method call style is needed for jruby.
    # JRuby cannot handle correctly if method using keyword hash and last hash argument.
    # see https://bugs.ruby-lang.org/issues/7529
    @client = Kura.client(project_id = { "project_id" => @project_id, "client_email" => client_email, "private_key" => private_key },
                          client_email = nil, private_key = nil, {http_options: {timeout: 60}})
  end
rescue Kura::ApiError => e
  process_error(e)
end

Instance Attribute Details

#project_idObject (readonly)

Returns the value of attribute project_id.



11
12
13
# File 'lib/tumugi/plugin/bigquery/client.rb', line 11

def project_id
  @project_id
end

Instance Method Details

#cancel_job(job_id, project_id: nil, &blk) ⇒ Object



300
301
302
303
304
# File 'lib/tumugi/plugin/bigquery/client.rb', line 300

def cancel_job(job_id, project_id: nil, &blk)
  @client.cancel_job(job_id, project_id: project_id || @project_id, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id, mode: :truncate, src_project_id: nil, dest_project_id: nil, job_project_id: dest_project_id, job_id: nil, wait: nil, dry_run: false, &blk) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/tumugi/plugin/bigquery/client.rb', line 272

def copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id,
         mode: :truncate,
         src_project_id: nil,
         dest_project_id: nil,
         job_project_id: dest_project_id,
         job_id: nil,
         wait: nil,
         dry_run: false,
         &blk)
  @client.copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id,
               mode: mode,
               src_project_id: src_project_id || @project_id,
               dest_project_id: dest_project_id || @project_id,
               job_project_id: job_project_id || @project_id,
               job_id: job_id,
               wait: wait,
               dry_run: dry_run,
               &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#dataset(dataset_id, project_id: nil, &blk) ⇒ Object



45
46
47
48
49
# File 'lib/tumugi/plugin/bigquery/client.rb', line 45

def dataset(dataset_id, project_id: nil, &blk)
  @client.dataset(dataset_id, project_id: project_id || @project_id, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#dataset_exist?(dataset_id, project_id: nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/tumugi/plugin/bigquery/client.rb', line 51

def dataset_exist?(dataset_id, project_id: nil)
  !@client.dataset(dataset_id, project_id: project_id || @project_id).nil?
rescue Kura::ApiError => e
  process_error(e)
end

#datasets(project_id: nil, all: false, limit: 1000, &blk) ⇒ Object



39
40
41
42
43
# File 'lib/tumugi/plugin/bigquery/client.rb', line 39

def datasets(project_id: nil, all: false, limit: 1000, &blk)
  @client.datasets(project_id: project_id || @project_id, all: all, limit: limit, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#delete_dataset(dataset_id, project_id: nil, delete_contents: true, &blk) ⇒ Object



63
64
65
66
67
# File 'lib/tumugi/plugin/bigquery/client.rb', line 63

def delete_dataset(dataset_id, project_id: nil, delete_contents: true, &blk)
  @client.delete_dataset(dataset_id, project_id: project_id || @project_id, delete_contents: delete_contents, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#delete_table(dataset_id, table_id, project_id: nil, &blk) ⇒ Object



125
126
127
128
129
# File 'lib/tumugi/plugin/bigquery/client.rb', line 125

def delete_table(dataset_id, table_id, project_id: nil, &blk)
  @client.delete_table(dataset_id, table_id, project_id: project_id || @project_id, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#extract(dataset_id, table_id, dest_uris, compression: "NONE", destination_format: "CSV", field_delimiter: ",", print_header: true, project_id: nil, job_project_id: nil, job_id: nil, wait: nil, dry_run: false, &blk) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/tumugi/plugin/bigquery/client.rb', line 246

def extract(dataset_id, table_id, dest_uris,
            compression: "NONE",
            destination_format: "CSV",
            field_delimiter: ",",
            print_header: true,
            project_id: nil,
            job_project_id: nil,
            job_id: nil,
            wait: nil,
            dry_run: false,
            &blk)
  @client.extract(dataset_id, table_id, dest_uris,
                  compression: compression,
                  destination_format: destination_format,
                  field_delimiter: field_delimiter,
                  print_header: print_header,
                  project_id: project_id || @project_id,
                  job_project_id: job_project_id || @project_id,
                  job_id: job_id,
                  wait: wait,
                  dry_run: dry_run,
                  &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#insert_dataset(dataset_id, project_id: nil, &blk) ⇒ Object



57
58
59
60
61
# File 'lib/tumugi/plugin/bigquery/client.rb', line 57

def insert_dataset(dataset_id, project_id: nil, &blk)
  @client.insert_dataset(dataset_id, project_id: project_id || @project_id, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#insert_job(configuration, job_id: nil, project_id: nil, media: nil, wait: nil, &blk) ⇒ Object



169
170
171
172
173
# File 'lib/tumugi/plugin/bigquery/client.rb', line 169

def insert_job(configuration, job_id: nil, project_id: nil, media: nil, wait: nil, &blk)
  @client.insert_job(configuration, job_id: job_id, project_id: project_id || @project_id, media: media, wait: wait, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#insert_table(dataset_id, table_id, project_id: nil, expiration_time: nil, friendly_name: nil, schema: nil, description: nil, query: nil, external_data_configuration: nil, &blk) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/tumugi/plugin/bigquery/client.rb', line 103

def insert_table(dataset_id, table_id,
                  project_id: nil,
                  expiration_time: nil,
                  friendly_name: nil,
                  schema: nil,
                  description: nil,
                  query: nil,
                  external_data_configuration: nil,
                  &blk)
  @client.insert_table(dataset_id, table_id,
                        project_id: project_id || @project_id,
                        expiration_time: expiration_time,
                        friendly_name: friendly_name,
                        schema: schema,
                        description: description,
                        query: query,
                        external_data_configuration: external_data_configuration,
                        &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#insert_tabledata(dataset_id, table_id, rows, project_id: nil, ignore_unknown_values: false, skip_invalid_rows: false, template_suffix: nil) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/tumugi/plugin/bigquery/client.rb', line 159

def insert_tabledata(dataset_id, table_id, rows, project_id: nil, ignore_unknown_values: false, skip_invalid_rows: false, template_suffix: nil)
  @client.insert_tabledata(dataset_id, table_id, rows,
                          project_id: project_id || @project_id,
                          ignore_unknown_values: ignore_unknown_values,
                          skip_invalid_rows: skip_invalid_rows,
                          template_suffix: template_suffix)
rescue Kura::ApiError => e
  process_error(e)
end

#job(job_id, project_id: nil, &blk) ⇒ Object



294
295
296
297
298
# File 'lib/tumugi/plugin/bigquery/client.rb', line 294

def job(job_id, project_id: nil, &blk)
  @client.job(job_id, project_id: project_id || @project_id, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#list_tabledata(dataset_id, table_id, project_id: nil, start_index: 0, max_result: 100, page_token: nil, schema: nil, &blk) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tumugi/plugin/bigquery/client.rb', line 147

def list_tabledata(dataset_id, table_id, project_id: nil, start_index: 0, max_result: 100, page_token: nil, schema: nil, &blk)
  @client.list_tabledata(dataset_id, table_id,
                          project_id: project_id || @project_id,
                          start_index: start_index,
                          max_result: max_result,
                          page_token: page_token,
                          schema: schema,
                          &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#load(dataset_id, table_id, source_uris = nil, schema: nil, field_delimiter: ",", mode: :append, allow_jagged_rows: false, max_bad_records: 0, ignore_unknown_values: false, allow_quoted_newlines: false, quote: '"', skip_leading_rows: 0, source_format: "CSV", project_id: nil, job_project_id: nil, job_id: nil, file: nil, wait: nil, dry_run: false, &blk) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/tumugi/plugin/bigquery/client.rb', line 207

def load(dataset_id, table_id, source_uris=nil,
         schema: nil,
         field_delimiter: ",",
         mode: :append,
         allow_jagged_rows: false,
         max_bad_records: 0,
         ignore_unknown_values: false,
         allow_quoted_newlines: false,
         quote: '"',
         skip_leading_rows: 0,
         source_format: "CSV",
         project_id: nil,
         job_project_id: nil,
         job_id: nil,
         file: nil, wait: nil,
         dry_run: false,
         &blk)
  @client.load(dataset_id, table_id, source_uris=source_uris,
               schema: schema,
               field_delimiter: field_delimiter,
               mode: mode,
               allow_jagged_rows: allow_jagged_rows,
               max_bad_records: max_bad_records,
               ignore_unknown_values: ignore_unknown_values,
               allow_quoted_newlines: allow_quoted_newlines,
               quote: quote,
               skip_leading_rows: skip_leading_rows,
               source_format: source_format,
               project_id: project_id || @project_id,
               job_project_id: job_project_id || @project_id,
               job_id: job_id,
               file: file,
               wait: wait,
               dry_run: dry_run,
               &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#patch_dataset(dataset_id, project_id: nil, access: nil, description: :na, default_table_expiration_ms: :na, friendly_name: :na, &blk) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tumugi/plugin/bigquery/client.rb', line 69

def patch_dataset(dataset_id, project_id: nil,
                  access: nil,
                  description: :na,
                  default_table_expiration_ms: :na,
                  friendly_name: :na,
                  &blk)
  @client.patch_dataset(dataset_id, project_id: project_id || @project_id,
                        access: access,
                        description: description,
                        default_table_expiration_ms: default_table_expiration_ms,
                        friendly_name: friendly_name,
                        &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#patch_table(dataset_id, table_id, project_id: nil, expiration_time: :na, friendly_name: :na, description: :na, &blk) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tumugi/plugin/bigquery/client.rb', line 131

def patch_table(dataset_id, table_id,
                project_id: nil,
                expiration_time: :na,
                friendly_name: :na,
                description: :na,
                &blk)
  @client.patch_table(dataset_id, table_id,
                      project_id: project_id || @project_id,
                      expiration_time: expiration_time,
                      friendly_name: friendly_name,
                      description: description,
                      &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#projects(limit: 1000, &blk) ⇒ Object



33
34
35
36
37
# File 'lib/tumugi/plugin/bigquery/client.rb', line 33

def projects(limit: 1000, &blk)
  @client.projects(limit: limit, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#query(sql, mode: :truncate, dataset_id: nil, table_id: nil, allow_large_results: true, flatten_results: true, priority: "INTERACTIVE", use_query_cache: true, use_legacy_sql: true, user_defined_function_resources: nil, project_id: nil, job_project_id: nil, job_id: nil, wait: nil, dry_run: false, &blk) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/tumugi/plugin/bigquery/client.rb', line 175

def query(sql, mode: :truncate,
          dataset_id: nil, table_id: nil,
          allow_large_results: true,
          flatten_results: true,
          priority: "INTERACTIVE",
          use_query_cache: true,
          use_legacy_sql: true,
          user_defined_function_resources: nil,
          project_id: nil,
          job_project_id: nil,
          job_id: nil,
          wait: nil,
          dry_run: false,
          &blk)
  @client.query(sql, mode: mode,
                dataset_id: dataset_id, table_id: table_id,
                allow_large_results: allow_large_results,
                flatten_results: flatten_results,
                priority: priority,
                use_query_cache: use_query_cache,
                use_legacy_sql: use_legacy_sql,
                user_defined_function_resources: user_defined_function_resources,
                project_id: project_id || @project_id,
                job_project_id: job_project_id || @project_id,
                job_id: job_id,
                wait: wait,
                dry_run: dry_run,
                &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#table(dataset_id, table_id, project_id: nil) ⇒ Object



91
92
93
94
95
# File 'lib/tumugi/plugin/bigquery/client.rb', line 91

def table(dataset_id, table_id, project_id: nil)
  @client.table(dataset_id, table_id, project_id: project_id || @project_id)
rescue Kura::ApiError => e
  process_error(e)
end

#table_exist?(dataset_id, table_id, project_id: nil) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/tumugi/plugin/bigquery/client.rb', line 97

def table_exist?(dataset_id, table_id, project_id: nil)
  !@client.table(dataset_id, table_id, project_id: project_id || @project_id).nil?
rescue Kura::ApiError => e
  process_error(e)
end

#tables(dataset_id, project_id: nil, limit: 1000, &blk) ⇒ Object



85
86
87
88
89
# File 'lib/tumugi/plugin/bigquery/client.rb', line 85

def tables(dataset_id, project_id: nil, limit: 1000, &blk)
  @client.tables(dataset_id, project_id: project_id || @project_id, limit: limit, &blk)
rescue Kura::ApiError => e
  process_error(e)
end

#wait_job(job, timeout = 60*10, project_id: nil, &blk) ⇒ Object



306
307
308
309
310
# File 'lib/tumugi/plugin/bigquery/client.rb', line 306

def wait_job(job, timeout=60*10, project_id: nil, &blk)
  @client.wait_job(job, timeout, project_id: project_id || @project_id, &blk)
rescue Kura::ApiError => e
  process_error(e)
end