|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Copyright 2015 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 | +module Samples |
| 18 | + # BigQuery Samples module |
| 19 | + module BigQuery |
| 20 | + # [START all] |
| 21 | + # A short sample demonstrating making a BigQuery request |
| 22 | + # This uses Application Default Credentials to authenticate. |
| 23 | + # @see https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 24 | + class Shakespeare |
| 25 | + def unique_words project_id |
| 26 | + # [START build_service] |
| 27 | + require "gcloud" |
| 28 | + |
| 29 | + gcloud = Gcloud.new project_id |
| 30 | + bigquery = gcloud.bigquery |
| 31 | + # [END build_service] |
| 32 | + |
| 33 | + # [START run_query] |
| 34 | + sql = "SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words " + |
| 35 | + "FROM [publicdata:samples.shakespeare]" |
| 36 | + results = bigquery.query sql |
| 37 | + # [END run_query] |
| 38 | + |
| 39 | + # [START print_results] |
| 40 | + results.each do |row| |
| 41 | + puts "#{row['title']}: #{row['unique_words']}" |
| 42 | + end |
| 43 | + # [END print_results] |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + if __FILE__ == $PROGRAM_NAME |
| 48 | + project_id = ARGV.shift |
| 49 | + Shakespeare.new.unique_words project_id |
| 50 | + end |
| 51 | + # [END all] |
| 52 | + end |
| 53 | +end |
0 commit comments