Permalink
89 lines (75 sloc) 2 KB
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Command simpleapp queries the Stack Overflow public dataset in Google BigQuery.
package main
// [START bigquery_simple_app_all]
// [START bigquery_simple_app_deps]
import (
"context"
"fmt"
"io"
"log"
"os"
"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
)
// [END bigquery_simple_app_deps]
func main() {
proj := os.Getenv("GOOGLE_CLOUD_PROJECT")
if proj == "" {
fmt.Println("GOOGLE_CLOUD_PROJECT environment variable must be set.")
os.Exit(1)
}
rows, err := query(proj)
if err != nil {
log.Fatal(err)
}
if err := printResults(os.Stdout, rows); err != nil {
log.Fatal(err)
}
}
// query returns a slice of the results of a query.
func query(proj string) (*bigquery.RowIterator, error) {
// [START bigquery_simple_app_client]
ctx := context.Background()
client, err := bigquery.NewClient(ctx, proj)
if err != nil {
return nil, err
}
// [END bigquery_simple_app_client]
// [START bigquery_simple_app_query]
query := client.Query(
`SELECT
CONCAT(
'https://stackoverflow.com/questions/',
CAST(id as STRING)) as url,
view_count
FROM ` + "`bigquery-public-data.stackoverflow.posts_questions`" + `
WHERE tags like '%google-bigquery%'
ORDER BY view_count DESC
LIMIT 10;`)
return query.Read(ctx)
// [END bigquery_simple_app_query]
}
// [START bigquery_simple_app_print]
type StackOverflowRow struct {
URL string `bigquery:"url"`
ViewCount int64 `bigquery:"view_count"`
}
// printResults prints results from a query to the Stack Overflow public dataset.
func printResults(w io.Writer, iter *bigquery.RowIterator) error {
for {
var row StackOverflowRow
err := iter.Next(&row)
if err == iterator.Done {
return nil
}
if err != nil {
return err
}
fmt.Fprintf(w, "url: %s views: %d\n", row.URL, row.ViewCount)
}
}
// [END bigquery_simple_app_print]
// [END bigquery_simple_app_all]