|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Copyright 2016 Google, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +require "gcloud" |
| 18 | + |
| 19 | +# A short sample demonstrating browsing a BigQuery table |
| 20 | +# This uses Application Default Credentials to authenticate. |
| 21 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 22 | +def browse_table project_id, dataset_id, table_id, max=10 |
| 23 | + # [START browse_table] |
| 24 | + gcloud = Gcloud.new project_id |
| 25 | + bigquery = gcloud.bigquery |
| 26 | + |
| 27 | + dataset = bigquery.dataset dataset_id |
| 28 | + table = dataset.table table_id |
| 29 | + row_num = 0 |
| 30 | + page_token = nil |
| 31 | + loop do |
| 32 | + data = table.data :token => page_token, :max => max |
| 33 | + data.each do |row| |
| 34 | + puts "--- Row #{row_num+=1} ---" |
| 35 | + for column, value in row |
| 36 | + puts "#{column}: #{value}" |
| 37 | + end |
| 38 | + end |
| 39 | + break if not data.token |
| 40 | + puts "[Press enter for next page, any key to exit]" |
| 41 | + break if $stdin.gets.chomp != '' |
| 42 | + page_token = data.token |
| 43 | + end |
| 44 | + # [END browse_table] |
| 45 | +end |
| 46 | + |
| 47 | +# A short sample demonstrating deleting a BigQuery table |
| 48 | +# This uses Application Default Credentials to authenticate. |
| 49 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 50 | +def delete_table project_id, dataset_id, table_id |
| 51 | + # [START delete_table] |
| 52 | + gcloud = Gcloud.new project_id |
| 53 | + bigquery = gcloud.bigquery |
| 54 | + |
| 55 | + dataset = bigquery.dataset dataset_id |
| 56 | + table = dataset.table table_id |
| 57 | + table.delete |
| 58 | + # [END delete_table] |
| 59 | + puts "Deleted table #{table_id}" |
| 60 | +end |
| 61 | + |
| 62 | +# A short sample demonstrating importing data into BigQuery |
| 63 | +# This uses Application Default Credentials to authenticate. |
| 64 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 65 | +def import project_id, dataset_id, table_id, source |
| 66 | + gcloud = Gcloud.new project_id |
| 67 | + bigquery = gcloud.bigquery |
| 68 | + |
| 69 | + accepted_formats = [".csv", ".json", ".backup_info"] |
| 70 | + source_format = File.extname(source) |
| 71 | + if not accepted_formats.include? source_format |
| 72 | + raise "source format not accepted, must be csv or json" |
| 73 | + end |
| 74 | + |
| 75 | + case source_format |
| 76 | + when ".csv" |
| 77 | + format = "CSV" |
| 78 | + when ".json" |
| 79 | + format = "NEWLINE_DELIMITED_JSON" |
| 80 | + when ".backup_info" |
| 81 | + format = "DATASTORE_BACKUP" |
| 82 | + end |
| 83 | + |
| 84 | + # [START import] |
| 85 | + dataset = bigquery.dataset dataset_id |
| 86 | + table = dataset.table table_id |
| 87 | + |
| 88 | + job = table.load source, format: format |
| 89 | + job.wait_until_done! |
| 90 | + |
| 91 | + if job.failed? |
| 92 | + puts job.error |
| 93 | + else |
| 94 | + puts "Data imported successfully" |
| 95 | + end |
| 96 | + # [END import] |
| 97 | +end |
| 98 | + |
| 99 | +# A short sample demonstrating importing data into BigQuery |
| 100 | +# This uses Application Default Credentials to authenticate. |
| 101 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 102 | +def import_stream project_id, dataset_id, table_id |
| 103 | + gcloud = Gcloud.new project_id |
| 104 | + bigquery = gcloud.bigquery |
| 105 | + |
| 106 | + # [START import_stream] |
| 107 | + dataset = bigquery.dataset dataset_id |
| 108 | + table = dataset.table table_id |
| 109 | + |
| 110 | + row = Hash[table.schema["fields"].map { |f| |
| 111 | + puts "Provide a value for #{f["name"]}" |
| 112 | + [f["name"], $stdin.gets.chomp] |
| 113 | + }] |
| 114 | + |
| 115 | + job = table.insert [row] |
| 116 | + puts "Row streamed into table successfully" |
| 117 | + # [END import_stream] |
| 118 | +end |
| 119 | + |
| 120 | +# A short sample demonstrating listing BigQuery datasets |
| 121 | +# This uses Application Default Credentials to authenticate. |
| 122 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 123 | +def list_datasets project_id |
| 124 | + # [START list_datasets] |
| 125 | + gcloud = Gcloud.new project_id |
| 126 | + bigquery = gcloud.bigquery |
| 127 | + |
| 128 | + bigquery.datasets.each do |dataset| |
| 129 | + puts "#{dataset.dataset_id}" |
| 130 | + end |
| 131 | + # [END list_datasets] |
| 132 | +end |
| 133 | + |
| 134 | +# A short sample demonstrating listing BigQuery tables |
| 135 | +# This uses Application Default Credentials to authenticate. |
| 136 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 137 | +def list_tables project_id, dataset_id |
| 138 | + # [START list_tables] |
| 139 | + gcloud = Gcloud.new project_id |
| 140 | + bigquery = gcloud.bigquery |
| 141 | + dataset = bigquery.dataset dataset_id |
| 142 | + |
| 143 | + dataset.tables.each do |table| |
| 144 | + puts "#{table.table_id}" |
| 145 | + end |
| 146 | + # [END list_tables] |
| 147 | +end |
| 148 | + |
| 149 | +# A short sample demonstrating making a BigQuery request |
| 150 | +# This uses Application Default Credentials to authenticate. |
| 151 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 152 | +def query project_id, sql |
| 153 | + # [START build_service] |
| 154 | + gcloud = Gcloud.new project_id |
| 155 | + bigquery = gcloud.bigquery |
| 156 | + # [END build_service] |
| 157 | + |
| 158 | + # [START run_query] |
| 159 | + results = bigquery.query sql |
| 160 | + # [END run_query] |
| 161 | + |
| 162 | + # [START print_results] |
| 163 | + results.each do |row| |
| 164 | + puts "---" |
| 165 | + row.each do |column, value| |
| 166 | + puts "#{column}: #{value}" |
| 167 | + end |
| 168 | + end |
| 169 | + # [END print_results] |
| 170 | +end |
| 171 | + |
| 172 | +# A short sample demonstrating making a BigQuery request as a job |
| 173 | +# This uses Application Default Credentials to authenticate. |
| 174 | +# @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 175 | +def query_as_job project_id, sql |
| 176 | + gcloud = Gcloud.new project_id |
| 177 | + bigquery = gcloud.bigquery |
| 178 | + |
| 179 | + # [START run_query] |
| 180 | + job = bigquery.query_job sql |
| 181 | + |
| 182 | + if job.failed? |
| 183 | + puts job.error |
| 184 | + else |
| 185 | + job.query_results.each do |row| |
| 186 | + puts "---" |
| 187 | + row.each do |column, value| |
| 188 | + puts "#{column}: #{value}" |
| 189 | + end |
| 190 | + end |
| 191 | + end |
| 192 | +# [END run_query] |
| 193 | +end |
0 commit comments