|
| 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_bigquery_client project_id: |
| 16 | + # [START create_bigquery_client] |
| 17 | + require "google/cloud" |
| 18 | + |
| 19 | + gcloud = Google::Cloud.new project_id |
| 20 | + bigquery = gcloud.bigquery |
| 21 | + # [END create_bigquery_client] |
| 22 | +end |
| 23 | + |
| 24 | +def create_dataset project_id:, dataset_id: |
| 25 | + # [START create_dataset] |
| 26 | + # project_id = "Your Google Cloud project ID" |
| 27 | + # dataset_id = "ID of the dataset to create" |
| 28 | + |
| 29 | + require "google/cloud" |
| 30 | + |
| 31 | + gcloud = Google::Cloud.new project_id |
| 32 | + bigquery = gcloud.bigquery |
| 33 | + |
| 34 | + bigquery.create_dataset dataset_id |
| 35 | + |
| 36 | + puts "Created dataset: #{dataset_id}" |
| 37 | + # [END create_dataset] |
| 38 | +end |
| 39 | + |
| 40 | +def list_datasets project_id: |
| 41 | + # [START list_datasets] |
| 42 | + # project_id = "Your Google Cloud project ID" |
| 43 | + |
| 44 | + require "google/cloud" |
| 45 | + |
| 46 | + gcloud = Google::Cloud.new project_id |
| 47 | + bigquery = gcloud.bigquery |
| 48 | + |
| 49 | + bigquery.datasets.each do |dataset| |
| 50 | + puts dataset.dataset_id |
| 51 | + end |
| 52 | + # [END list_datasets] |
| 53 | +end |
| 54 | + |
| 55 | +def delete_dataset project_id:, dataset_id: |
| 56 | + # [START delete_dataset] |
| 57 | + # project_id = "Your Google Cloud project ID" |
| 58 | + # dataset_id = "ID of the dataset to delete" |
| 59 | + |
| 60 | + require "google/cloud" |
| 61 | + |
| 62 | + gcloud = Google::Cloud.new project_id |
| 63 | + bigquery = gcloud.bigquery |
| 64 | + dataset = bigquery.dataset dataset_id |
| 65 | + |
| 66 | + dataset.delete |
| 67 | + |
| 68 | + puts "Deleted dataset: #{dataset_id}" |
| 69 | + # [END delete_dataset] |
| 70 | +end |
| 71 | + |
| 72 | +if __FILE__ == $PROGRAM_NAME |
| 73 | + project_id = ENV["GOOGLE_CLOUD_PROJECT"] |
| 74 | + command = ARGV.shift |
| 75 | + |
| 76 | + case command |
| 77 | + when "create" |
| 78 | + create_dataset project_id: project_id, dataset_id: ARGV.shift |
| 79 | + when "list" |
| 80 | + list_datasets project_id: project_id |
| 81 | + when "delete" |
| 82 | + delete_dataset project_id: project_id, dataset_id: ARGV.shift |
| 83 | + else |
| 84 | + puts <<-usage |
| 85 | +Usage: ruby datasets.rb <command> [arguments] |
| 86 | +
|
| 87 | +Commands: |
| 88 | + create <dataset_id> Create a new dataset with the specified ID |
| 89 | + list List datasets in the specified project |
| 90 | + delete <dataset_id> Delete the dataset with the specified ID |
| 91 | + usage |
| 92 | + end |
| 93 | +end |
0 commit comments