main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // load is an example client of the bigquery client library.
  15. // It loads a file from Google Cloud Storage into a BigQuery 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. table = flag.String("table", "", "The ID of a BigQuery table to load data into")
  31. bucket = flag.String("bucket", "", "The name of a Google Cloud Storage bucket to load data from")
  32. object = flag.String("object", "", "The name of a Google Cloud Storage object to load data from. Must exist within the bucket specified by --bucket")
  33. skiprows = flag.Int64("skiprows", 0, "The number of rows of the source data to skip when loading")
  34. pollint = flag.Duration("pollint", 10*time.Second, "Polling interval for checking job status")
  35. )
  36. func main() {
  37. flag.Parse()
  38. flagsOk := true
  39. for _, f := range []string{"project", "dataset", "table", "bucket", "object"} {
  40. if flag.Lookup(f).Value.String() == "" {
  41. fmt.Fprintf(os.Stderr, "Flag --%s is required\n", f)
  42. flagsOk = false
  43. }
  44. }
  45. if !flagsOk {
  46. os.Exit(1)
  47. }
  48. httpClient, err := google.DefaultClient(context.Background(), bigquery.Scope)
  49. if err != nil {
  50. log.Fatalf("Creating http client: %v", err)
  51. }
  52. client, err := bigquery.NewClient(httpClient, *project)
  53. if err != nil {
  54. log.Fatalf("Creating bigquery client: %v", err)
  55. }
  56. table := &bigquery.Table{
  57. ProjectID: *project,
  58. DatasetID: *dataset,
  59. TableID: *table,
  60. }
  61. gcs := client.NewGCSReference(fmt.Sprintf("gs://%s/%s", *bucket, *object))
  62. gcs.SkipLeadingRows = *skiprows
  63. // Load data from Google Cloud Storage into a BigQuery table.
  64. job, err := client.Copy(
  65. context.Background(), table, gcs,
  66. bigquery.MaxBadRecords(1),
  67. bigquery.AllowQuotedNewlines(),
  68. bigquery.WriteTruncate)
  69. if err != nil {
  70. log.Fatalf("Loading data: %v", err)
  71. }
  72. fmt.Printf("Job for data load operation: %+v\n", job)
  73. fmt.Printf("Waiting for job to complete.\n")
  74. for range time.Tick(*pollint) {
  75. status, err := job.Status(context.Background())
  76. if err != nil {
  77. fmt.Printf("Failure determining status: %v", err)
  78. break
  79. }
  80. if !status.Done() {
  81. continue
  82. }
  83. if err := status.Err(); err == nil {
  84. fmt.Printf("Success\n")
  85. } else {
  86. fmt.Printf("Failure: %+v\n", err)
  87. }
  88. break
  89. }
  90. }