main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // read is an example client of the bigquery client library.
  15. // It reads from a table, returning the data via an Iterator.
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "log"
  21. "os"
  22. "regexp"
  23. "strings"
  24. "text/tabwriter"
  25. "golang.org/x/net/context"
  26. "golang.org/x/oauth2/google"
  27. "google.golang.org/cloud/bigquery"
  28. )
  29. var (
  30. project = flag.String("project", "", "The ID of a Google Cloud Platform project")
  31. dataset = flag.String("dataset", "", "The ID of a BigQuery dataset")
  32. table = flag.String("table", ".*", "A regular expression to match the IDs of tables to read.")
  33. jobID = flag.String("jobid", "", "The ID of a query job that has already been submitted."+
  34. " If set, --dataset, --table will be ignored, and results will be read from the specified job.")
  35. )
  36. func printValues(it *bigquery.Iterator) {
  37. // one-space padding.
  38. tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
  39. for it.Next(context.Background()) {
  40. var vals bigquery.ValueList
  41. if err := it.Get(&vals); err != nil {
  42. fmt.Printf("err calling get: %v\n", err)
  43. } else {
  44. sep := ""
  45. for _, v := range vals {
  46. fmt.Fprintf(tw, "%s%v", sep, v)
  47. sep = "\t"
  48. }
  49. fmt.Fprintf(tw, "\n")
  50. }
  51. }
  52. tw.Flush()
  53. fmt.Printf("\n")
  54. if err := it.Err(); err != nil {
  55. fmt.Printf("err reading: %v\n", err)
  56. }
  57. }
  58. func printTable(client *bigquery.Client, t *bigquery.Table) {
  59. it, err := client.Read(context.Background(), t)
  60. if err != nil {
  61. log.Fatalf("Reading: %v", err)
  62. }
  63. id := t.FullyQualifiedName()
  64. fmt.Printf("%s\n%s\n", id, strings.Repeat("-", len(id)))
  65. printValues(it)
  66. }
  67. func printQueryResults(client *bigquery.Client, queryJobID string) {
  68. job, err := client.JobFromID(context.Background(), queryJobID)
  69. if err != nil {
  70. log.Fatalf("Loading job: %v", err)
  71. }
  72. it, err := client.Read(context.Background(), job)
  73. if err != nil {
  74. log.Fatalf("Reading: %v", err)
  75. }
  76. // TODO: print schema.
  77. printValues(it)
  78. }
  79. func main() {
  80. flag.Parse()
  81. flagsOk := true
  82. if flag.Lookup("project").Value.String() == "" {
  83. fmt.Fprintf(os.Stderr, "Flag --project is required\n")
  84. flagsOk = false
  85. }
  86. var sourceFlagCount int
  87. if flag.Lookup("dataset").Value.String() != "" {
  88. sourceFlagCount++
  89. }
  90. if flag.Lookup("jobid").Value.String() != "" {
  91. sourceFlagCount++
  92. }
  93. if sourceFlagCount != 1 {
  94. fmt.Fprintf(os.Stderr, "Exactly one of --dataset or --jobid must be set\n")
  95. flagsOk = false
  96. }
  97. if !flagsOk {
  98. os.Exit(1)
  99. }
  100. tableRE, err := regexp.Compile(*table)
  101. if err != nil {
  102. fmt.Fprintf(os.Stderr, "--table is not a valid regular expression: %q\n", *table)
  103. os.Exit(1)
  104. }
  105. httpClient, err := google.DefaultClient(context.Background(), bigquery.Scope)
  106. if err != nil {
  107. log.Fatalf("Creating http client: %v", err)
  108. }
  109. client, err := bigquery.NewClient(httpClient, *project)
  110. if err != nil {
  111. log.Fatalf("Creating bigquery client: %v", err)
  112. }
  113. if *jobID != "" {
  114. printQueryResults(client, *jobID)
  115. return
  116. }
  117. ds := client.Dataset(*dataset)
  118. var tables []*bigquery.Table
  119. tables, err = ds.ListTables(context.Background())
  120. if err != nil {
  121. log.Fatalf("Listing tables: %v", err)
  122. }
  123. for _, t := range tables {
  124. if tableRE.MatchString(t.TableID) {
  125. printTable(client, t)
  126. }
  127. }
  128. }