main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // concat_table is an example client of the bigquery client library.
  15. // It concatenates two BigQuery tables and writes the result to another 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. src1 = flag.String("src1", "", "The ID of the first BigQuery table to concatenate")
  31. src2 = flag.String("src2", "", "The ID of the second BigQuery table to concatenate")
  32. dest = flag.String("dest", "", "The ID of the BigQuery table to write the result to")
  33. pollint = flag.Duration("pollint", 10*time.Second, "Polling interval for checking job status")
  34. )
  35. func main() {
  36. flag.Parse()
  37. flagsOk := true
  38. for _, f := range []string{"project", "dataset", "src1", "src2", "dest"} {
  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. if *src1 == *src2 || *src1 == *dest || *src2 == *dest {
  48. log.Fatalf("Different values must be supplied for each of --src1, --src2 and --dest")
  49. }
  50. httpClient, err := google.DefaultClient(context.Background(), bigquery.Scope)
  51. if err != nil {
  52. log.Fatalf("Creating http client: %v", err)
  53. }
  54. client, err := bigquery.NewClient(httpClient, *project)
  55. if err != nil {
  56. log.Fatalf("Creating bigquery client: %v", err)
  57. }
  58. s1 := &bigquery.Table{
  59. ProjectID: *project,
  60. DatasetID: *dataset,
  61. TableID: *src1,
  62. }
  63. s2 := &bigquery.Table{
  64. ProjectID: *project,
  65. DatasetID: *dataset,
  66. TableID: *src2,
  67. }
  68. d := &bigquery.Table{
  69. ProjectID: *project,
  70. DatasetID: *dataset,
  71. TableID: *dest,
  72. }
  73. // Concatenate data.
  74. job, err := client.Copy(context.Background(), d, bigquery.Tables{s1, s2}, bigquery.WriteTruncate)
  75. if err != nil {
  76. log.Fatalf("Concatenating: %v", err)
  77. }
  78. fmt.Printf("Job for concatenation operation: %+v\n", job)
  79. fmt.Printf("Waiting for job to complete.\n")
  80. for range time.Tick(*pollint) {
  81. status, err := job.Status(context.Background())
  82. if err != nil {
  83. fmt.Printf("Failure determining status: %v", err)
  84. break
  85. }
  86. if !status.Done() {
  87. continue
  88. }
  89. if err := status.Err(); err == nil {
  90. fmt.Printf("Success\n")
  91. } else {
  92. fmt.Printf("Failure: %+v\n", err)
  93. }
  94. break
  95. }
  96. }