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-
6315def 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]
211163end
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 ]
229181end
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 ]
252204end
253205
254- # TODO: separate sample into separate executable files
255- #
256206if __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
265261end
0 commit comments