main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  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. // query is an example client of the bigquery client library.
  15. // It submits a query and writes the result to a table.
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "log"
  21. "os"
  22. "time"
  23. "golang.org/x/net/context"
  24. "golang.org/x/oauth2/google"
  25. "google.golang.org/cloud/bigquery"
  26. )
  27. var (
  28. project = flag.String("project", "", "The ID of a Google Cloud Platform project")
  29. dataset = flag.String("dataset", "", "The ID of a BigQuery dataset")
  30. q = flag.String("q", "", "The query string")
  31. dest = flag.String("dest", "", "The ID of the BigQuery table to write the result to. If unset, an ephemeral table ID will be generated.")
  32. pollint = flag.Duration("pollint", 10*time.Second, "Polling interval for checking job status")
  33. wait = flag.Bool("wait", false, "Whether to wait for the query job to complete.")
  34. )
  35. func main() {
  36. flag.Parse()
  37. flagsOk := true
  38. for _, f := range []string{"project", "dataset", "q"} {
  39. if flag.Lookup(f).Value.String() == "" {
  40. fmt.Fprintf(os.Stderr, "Flag --%s is required\n", f)
  41. flagsOk = false
  42. }
  43. }
  44. if !flagsOk {
  45. os.Exit(1)
  46. }
  47. httpClient, err := google.DefaultClient(context.Background(), bigquery.Scope)
  48. if err != nil {
  49. log.Fatalf("Creating http client: %v", err)
  50. }
  51. client, err := bigquery.NewClient(httpClient, *project)
  52. if err != nil {
  53. log.Fatalf("Creating bigquery client: %v", err)
  54. }
  55. d := &bigquery.Table{}
  56. if *dest != "" {
  57. d.ProjectID = *project
  58. d.DatasetID = *dataset
  59. d.TableID = *dest
  60. }
  61. query := &bigquery.Query{
  62. Q: *q,
  63. DefaultProjectID: *project,
  64. DefaultDatasetID: *dataset,
  65. }
  66. // Query data.
  67. job, err := client.Copy(context.Background(), d, query, bigquery.WriteTruncate)
  68. if err != nil {
  69. log.Fatalf("Querying: %v", err)
  70. }
  71. fmt.Printf("Submitted query. Job ID: %s\n", job.ID())
  72. if !*wait {
  73. return
  74. }
  75. fmt.Printf("Waiting for job to complete.\n")
  76. for range time.Tick(*pollint) {
  77. status, err := job.Status(context.Background())
  78. if err != nil {
  79. fmt.Printf("Failure determining status: %v", err)
  80. break
  81. }
  82. if !status.Done() {
  83. continue
  84. }
  85. if err := status.Err(); err == nil {
  86. fmt.Printf("Success\n")
  87. } else {
  88. fmt.Printf("Failure: %+v\n", err)
  89. }
  90. break
  91. }
  92. }