/
tables.rb
426 lines (338 loc) · 14.4 KB
/
tables.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
206
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
245
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Copyright 2016 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def create_table_with_schema project_id:, dataset_id:, table_id:
# [START bigquery_create_table]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset to create table in"
# table_id = "ID of the table to create"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.create_table table_id do |updater|
updater.string "full_name", mode: :required
updater.integer "age", mode: :required
end
puts "Created table: #{table_id}"
# [END bigquery_create_table]
end
def create_table_without_schema project_id:, dataset_id:, table_id:
# [START bigquery_create_table_without_schema]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset to create table in"
# table_id = "ID of the table to create"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
dataset.create_table table_id
puts "Created table: #{table_id}"
# [END bigquery_create_table_without_schema]
end
def list_tables project_id:, dataset_id:
# [START bigquery_list_tables]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset to create table in"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
dataset.tables.each do |table|
puts table.table_id
end
# [END bigquery_list_tables]
end
def delete_table project_id:, dataset_id:, table_id:
# [START bigquery_delete_table]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset delete table from"
# table_id = "ID of the table to delete"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
table.delete
puts "Deleted table: #{table_id}"
# [END bigquery_delete_table]
end
def list_table_data project_id:, dataset_id:, table_id:
# [START bigquery_browse_table]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to display data for"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
table.data.each do |row|
row.each do |column_name, value|
puts "#{column_name} = #{value}"
end
end
# [END bigquery_browse_table]
end
def import_table_data project_id:, dataset_id:, table_id:, row_data:
# [START bigquery_table_insert_rows]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to import data into"
# row_data = [{ column1: value, column2: value }, ...]
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
response = table.insert row_data
if response.success?
puts "Inserted rows successfully"
else
puts "Failed to insert #{response.error_rows.count} rows"
end
# [END bigquery_table_insert_rows]
end
def import_table_data_from_file project_id:, dataset_id:, table_id:,
local_file_path:
# [START bigquery_load_from_file]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to import file data into"
# local_file_path = "Path to local file to import into BigQuery table"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
puts "Importing data from file: #{local_file_path}"
load_job = table.load_job local_file_path
puts "Waiting for load job to complete: #{load_job.job_id}"
load_job.wait_until_done!
puts "Data imported"
# [END bigquery_load_from_file]
end
def import_table_data_from_cloud_storage project_id:, dataset_id:, table_id:,
storage_path:
# [START bigquery_load_table_gcs_csv]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to import data into"
# storage_path = "Storage path to file to import, eg. gs://bucket/file.csv"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
puts "Importing data from Cloud Storage file: #{storage_path}"
load_job = table.load_job storage_path
puts "Waiting for load job to complete: #{load_job.job_id}"
load_job.wait_until_done!
puts "Data imported"
# [END bigquery_load_table_gcs_csv]
end
def export_table_data_to_cloud_storage project_id:, dataset_id:, table_id:,
storage_path:
# [START bigquery_extract_table]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to export file data from"
# storage_path = "Storage path to export to, eg. gs://bucket/file.csv"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
puts "Exporting data to Cloud Storage file: #{storage_path}"
extract_job = table.extract_job storage_path
puts "Waiting for extract job to complete: #{extract_job.job_id}"
extract_job.wait_until_done!
puts "Data exported"
# [END bigquery_extract_table]
end
def import_table_from_gcs_json project_id:, dataset_id:
# [START bigquery_load_table_gcs_json]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset to create table in"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table_id = "us_states"
storage_path = "gs://cloud-samples-data/bigquery/us-states/us-states.json"
puts "Importing data from Cloud Storage file: #{storage_path}"
load_job = dataset.load_job table_id, storage_path, format: "json" do |schema|
schema.string "name"
schema.string "post_abbr"
end
puts "Waiting for load job to complete: #{load_job.job_id}"
load_job.wait_until_done!
puts "Data imported"
# [END bigquery_load_table_gcs_json]
end
def import_table_from_gcs_json_autodetect project_id:, dataset_id:
# [START bigquery_load_table_gcs_json_autodetect]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset to create table in"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table_id = "us_states"
storage_path = "gs://cloud-samples-data/bigquery/us-states/us-states.json"
puts "Importing data from Cloud Storage file: #{storage_path}"
load_job = dataset.load_job table_id,
storage_path,
format: "json",
autodetect: true
puts "Waiting for load job to complete: #{load_job.job_id}"
load_job.wait_until_done!
puts "Data imported"
# [END bigquery_load_table_gcs_json_autodetect]
end
def append_json_data_from_gcs project_id:, dataset_id:, table_id:
# [START bigquery_load_table_gcs_json_append]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to append data into"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
storage_path = "gs://cloud-samples-data/bigquery/us-states/us-states.json"
puts "Importing data from Cloud Storage file: #{storage_path}"
load_job = table.load_job storage_path,
format: "json",
write: "WRITE_APPEND"
puts "Waiting for load job to complete: #{load_job.job_id}"
load_job.wait_until_done!
puts "Data imported"
# [END bigquery_load_table_gcs_json_append]
end
def write_truncate_json_data_from_gcs project_id:, dataset_id:, table_id:
# [START bigquery_load_table_gcs_json_truncate]
# project_id = "Your Google Cloud project ID"
# dataset_id = "ID of the dataset containing table"
# table_id = "ID of the table to append data into"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
dataset = bigquery.dataset dataset_id
table = dataset.table table_id
storage_path = "gs://cloud-samples-data/bigquery/us-states/us-states.json"
puts "Importing data from Cloud Storage file: #{storage_path}"
load_job = table.load_job storage_path,
format: "json",
write: "WRITE_TRUNCATE"
puts "Waiting for load job to complete: #{load_job.job_id}"
load_job.wait_until_done!
puts "Data imported"
# [END bigquery_load_table_gcs_json_truncate]
end
def run_query project_id:, query_string:
# project_id = "your google cloud project id"
# query_string = "query string to execute (using bigquery query syntax)"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
query_results = bigquery.query query_string
query_results.each do |row|
puts row.inspect
end
end
def run_query_as_job project_id:, query_string:
# [START bigquery_query]
# project_id = "your google cloud project id"
# query_string = "query string to execute (using bigquery query syntax)"
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new project: project_id
puts "Running query"
query_job = bigquery.query_job query_string
puts "Waiting for query to complete"
query_job.wait_until_done!
puts "Query results:"
query_job.query_results.each do |row|
puts row.inspect
end
# [END bigquery_query]
end
if __FILE__ == $PROGRAM_NAME
require "json"
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
command = ARGV.shift
case command
when "create"
create_table_without_schema project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift
when "create_with_schema"
create_table_with_schema project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift
when "list"
list_tables project_id: project_id, dataset_id: ARGV.shift
when "delete"
delete_table project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift
when "list_data"
list_table_data project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift
when "import_file"
import_table_data_from_file project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift,
local_file_path: ARGV.shift
when "import_gcs"
import_table_data_from_cloud_storage project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift,
storage_path: ARGV.shift
when "import_gcs_json"
import_table_from_gcs_json project_id: project_id,
dataset_id: ARGV.shift
when "import_gcs_json_autodetect"
import_table_from_gcs_json_autodetect project_id: project_id,
dataset_id: ARGV.shift
when "import_data"
import_table_data project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift,
row_data: JSON.parse(ARGV.shift)
when "append_rows"
append_json_data_from_gcs project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift
when "overwrite_rows"
write_truncate_json_data_from_gcs project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift
when "export"
export_table_data_to_cloud_storage project_id: project_id,
dataset_id: ARGV.shift,
table_id: ARGV.shift,
storage_path: ARGV.shift
when "query"
run_query project_id: project_id, query_string: ARGV.shift
when "query_job"
run_query_as_job project_id: project_id, query_string: ARGV.shift
else
puts <<-usage
Usage: ruby tables.rb <command> [arguments]
Commands:
create <dataset_id> <table_id> Create a new table with the specified ID
create_with_schema <dataset_id> <table_id> Create a new table with a schema
list <dataset_id> List all tables in the specified dataset
delete <dataset_id> <table_id> Delete table with the specified ID
list_data <dataset_id> <table_id> List data in table with the specified ID
import_file <dataset_id> <table_id> <file_path>
import_gcs <dataset_id> <table_id> <cloud_storage_path>
import_gcs_json <dataset_id>
import_gcs_json_autodetect <dataset_id>
import_data <dataset_id> <table_id> "[{ <json row data> }]"
append_rows <dataset_id> <table_id>
overwrite_rows <dataset_id> <table_id>
export <dataset_id> <table_id> <cloud_storage_path>
query <query>
query_job <query>
usage
end
end