Skip to content

Commit b42fd74

Browse files
author
remi Taylor
committed
Split Dataset and Table code into separate files
1 parent c4a7e8e commit b42fd74

4 files changed

Lines changed: 172 additions & 61 deletions

File tree

bigquery/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,33 @@
66
analytics data warehouse.
77

88
[bigquery_docs]: https://cloud.google.com/bigquery/docs/
9+
10+
## Samples
11+
12+
### Datasets
13+
14+
```
15+
Usage: ruby datasets.rb <command> [arguments]
16+
17+
Commands:
18+
create <dataset_id> Create a new dataset with the specified ID
19+
list List datasets in the specified project
20+
delete <dataset_id> Delete the dataset with the specified ID
21+
```
22+
23+
### Tables
24+
25+
```
26+
Usage: ruby tables.rb <command> [arguments]
27+
28+
Commands:
29+
create <dataset_id> <table_id> Create a new table with the specified ID
30+
list <dataset_id> List all tables in the specified dataset
31+
delete <dataset_id> <table_id> Delete table with the specified ID
32+
list_data <dataset_id> <table_id> List data in table with the specified ID
33+
import_file <dataset_id> <table_id> <file_path>
34+
import_gcs <dataset_id> <table_id> <cloud_storage_path>
35+
export <dataset_id> <table_id> <cloud_storage_path>
36+
query <query>
37+
query_job <query>
38+
```

bigquery/datasets.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
def create_dataset project_id:, dataset_id:
16+
# [START create_dataset]
17+
# project_id = "Your Google Cloud project ID"
18+
# dataset_id = "ID of the dataset to create"
19+
20+
require "google/cloud"
21+
22+
gcloud = Google::Cloud.new project_id
23+
bigquery = gcloud.bigquery
24+
25+
bigquery.create_dataset dataset_id
26+
27+
puts "Created dataset: #{dataset_id}"
28+
# [END create_dataset]
29+
end
30+
31+
def list_datasets project_id:
32+
# [START list_datasets]
33+
# project_id = "Your Google Cloud project ID"
34+
35+
require "google/cloud"
36+
37+
gcloud = Google::Cloud.new project_id
38+
bigquery = gcloud.bigquery
39+
40+
bigquery.datasets.each do |dataset|
41+
puts dataset.dataset_id
42+
end
43+
# [END list_datasets]
44+
end
45+
46+
def delete_dataset project_id:, dataset_id:
47+
# [START delete_dataset]
48+
# project_id = "Your Google Cloud project ID"
49+
# dataset_id = "ID of the dataset to delete"
50+
51+
require "google/cloud"
52+
53+
gcloud = Google::Cloud.new project_id
54+
bigquery = gcloud.bigquery
55+
dataset = bigquery.dataset dataset_id
56+
57+
dataset.delete
58+
59+
puts "Deleted dataset: #{dataset_id}"
60+
# [END delete_dataset]
61+
end
62+
63+
if __FILE__ == $PROGRAM_NAME
64+
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
65+
command = ARGV.shift
66+
67+
case command
68+
when "create"
69+
create_dataset project_id: project_id, dataset_id: ARGV.shift
70+
when "list"
71+
list_datasets project_id: project_id
72+
when "delete"
73+
delete_dataset project_id: project_id, dataset_id: ARGV.shift
74+
else
75+
puts <<-usage
76+
Usage: ruby datasets.rb <command> [arguments]
77+
78+
Commands:
79+
create <dataset_id> Create a new dataset with the specified ID
80+
list List datasets in the specified project
81+
delete <dataset_id> Delete the dataset with the specified ID
82+
usage
83+
end
84+
end

bigquery/spec/bigquery_sample_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
require_relative "../bigquery_samples"
15+
require_relative "../datasets"
16+
require_relative "../tables"
1617
require "rspec"
1718
require "google/cloud"
1819
require "csv"
@@ -307,7 +308,7 @@ def capture &block
307308
describe "Querying" do
308309
example "run query" do
309310
capture do
310-
run_query_sync(
311+
run_query(
311312
project_id: @project_id,
312313
query_string: "SELECT TOP(word, 50) as word, COUNT(*) as count " +
313314
"FROM publicdata:samples.shakespeare"
@@ -319,7 +320,7 @@ def capture &block
319320

320321
example "run query as job" do
321322
capture do
322-
run_query_async(
323+
run_query_as_job(
323324
project_id: @project_id,
324325
query_string: "SELECT TOP(word, 50) as word, COUNT(*) as count " +
325326
"FROM publicdata:samples.shakespeare"
Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
def create_dataset project_id:, dataset_id:
16-
# [START create_dataset]
17-
# project_id = "Your Google Cloud project ID"
18-
# dataset_id = "ID of the dataset to create"
19-
20-
require "google/cloud"
21-
22-
gcloud = Google::Cloud.new project_id
23-
bigquery = gcloud.bigquery
24-
25-
bigquery.create_dataset dataset_id
26-
27-
puts "Created dataset: #{dataset_id}"
28-
# [END create_dataset]
29-
end
30-
31-
def list_datasets project_id:
32-
# [START list_datasets]
33-
# project_id = "Your Google Cloud project ID"
34-
35-
require "google/cloud"
36-
37-
gcloud = Google::Cloud.new project_id
38-
bigquery = gcloud.bigquery
39-
40-
bigquery.datasets.each do |dataset|
41-
puts dataset.dataset_id
42-
end
43-
# [END list_datasets]
44-
end
45-
46-
def delete_dataset project_id:, dataset_id:
47-
# [START delete_dataset]
48-
# project_id = "Your Google Cloud project ID"
49-
# dataset_id = "ID of the dataset to delete"
50-
51-
require "google/cloud"
52-
53-
gcloud = Google::Cloud.new project_id
54-
bigquery = gcloud.bigquery
55-
dataset = bigquery.dataset dataset_id
56-
57-
dataset.delete
58-
59-
puts "Deleted dataset: #{dataset_id}"
60-
# [END delete_dataset]
61-
end
62-
6315
def create_table project_id:, dataset_id:, table_id:
6416
# [START create_table]
6517
# project_id = "Your Google Cloud project ID"
@@ -210,8 +162,8 @@ def export_table_data_to_cloud_storage project_id:, dataset_id:, table_id:,
210162
# [END export_table_data_to_cloud_storage]
211163
end
212164

213-
def run_query_sync project_id:, query_string:
214-
# [start run_query_sync]
165+
def run_query project_id:, query_string:
166+
# [start run_query]
215167
# project_id = "your google cloud project id"
216168
# query_string = "query string to execute (using bigquery query syntax)"
217169

@@ -225,11 +177,11 @@ def run_query_sync project_id:, query_string:
225177
data.each do |row|
226178
puts row.inspect
227179
end
228-
# [end run_query_sync]
180+
# [end run_query]
229181
end
230182

231-
def run_query_async project_id:, query_string:
232-
# [start run_query_async]
183+
def run_query_as_job project_id:, query_string:
184+
# [start run_query_as_job]
233185
# project_id = "your google cloud project id"
234186
# query_string = "query string to execute (using bigquery query syntax)"
235187

@@ -248,18 +200,62 @@ def run_query_async project_id:, query_string:
248200
query_job.query_results.each do |row|
249201
puts row.inspect
250202
end
251-
# [end run_query_async]
203+
# [end run_query_as_job]
252204
end
253205

254-
# TODO: separate sample into separate executable files
255-
#
256206
if __FILE__ == $PROGRAM_NAME
257207
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
258208
command = ARGV.shift
259209

260210
case command
261-
when "< command here >"
211+
when "create"
212+
create_table project_id: project_id,
213+
dataset_id: ARGV.shift,
214+
table_id: ARGV.shift
215+
when "list"
216+
list_tables project_id: project_id, dataset_id: ARGV.shift
217+
when "delete"
218+
delete_table project_id: project_id,
219+
dataset_id: ARGV.shift,
220+
table_id: ARGV.shift
221+
when "list_data"
222+
list_table_data project_id: project_id,
223+
dataset_id: ARGV.shift,
224+
table_id: ARGV.shift
225+
226+
when "import_file"
227+
import_table_data_from_file project_id: project_id,
228+
dataset_id: ARGV.shift,
229+
table_id: ARGV.shift,
230+
local_file_path: ARGV.shift
231+
when "import_gcs"
232+
import_table_data_from_cloud_storage project_id: project_id,
233+
dataset_id: ARGV.shift,
234+
table_id: ARGV.shift,
235+
storage_path: ARGV.shift
236+
when "export"
237+
export_table_data_to_cloud_storage project_id: project_id,
238+
dataset_id: ARGV.shift,
239+
table_id: ARGV.shift,
240+
storage_path: ARGV.shift
241+
when "query"
242+
run_query project_id: project_id, query_string: ARGV.shift
243+
when "query_job"
244+
run_query_as_job project_id: project_id, query_string: ARGV.shift
262245
else
263-
puts "Usage: "
246+
puts <<-usage
247+
Usage: ruby tables.rb <command> [arguments]
248+
249+
Commands:
250+
create <dataset_id> <table_id> Create a new table with the specified ID
251+
list <dataset_id> List all tables in the specified dataset
252+
delete <dataset_id> <table_id> Delete table with the specified ID
253+
list_data <dataset_id> <table_id> List data in table with the specified ID
254+
import_file <dataset_id> <table_id> <file_path>
255+
import_gcs <dataset_id> <table_id> <cloud_storage_path>
256+
export <dataset_id> <table_id> <cloud_storage_path>
257+
query <query>
258+
query_job <query>
259+
usage
264260
end
265261
end

0 commit comments

Comments
 (0)