You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

111 lines
3.0 KiB

  1. // Copyright 2016 Google LLC
  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. package bigquery
  15. import (
  16. "context"
  17. "cloud.google.com/go/internal/trace"
  18. bq "google.golang.org/api/bigquery/v2"
  19. )
  20. // ExtractConfig holds the configuration for an extract job.
  21. type ExtractConfig struct {
  22. // Src is the table from which data will be extracted.
  23. Src *Table
  24. // Dst is the destination into which the data will be extracted.
  25. Dst *GCSReference
  26. // DisableHeader disables the printing of a header row in exported data.
  27. DisableHeader bool
  28. // The labels associated with this job.
  29. Labels map[string]string
  30. }
  31. func (e *ExtractConfig) toBQ() *bq.JobConfiguration {
  32. var printHeader *bool
  33. if e.DisableHeader {
  34. f := false
  35. printHeader = &f
  36. }
  37. return &bq.JobConfiguration{
  38. Labels: e.Labels,
  39. Extract: &bq.JobConfigurationExtract{
  40. DestinationUris: append([]string{}, e.Dst.URIs...),
  41. Compression: string(e.Dst.Compression),
  42. DestinationFormat: string(e.Dst.DestinationFormat),
  43. FieldDelimiter: e.Dst.FieldDelimiter,
  44. SourceTable: e.Src.toBQ(),
  45. PrintHeader: printHeader,
  46. },
  47. }
  48. }
  49. func bqToExtractConfig(q *bq.JobConfiguration, c *Client) *ExtractConfig {
  50. qe := q.Extract
  51. return &ExtractConfig{
  52. Labels: q.Labels,
  53. Dst: &GCSReference{
  54. URIs: qe.DestinationUris,
  55. Compression: Compression(qe.Compression),
  56. DestinationFormat: DataFormat(qe.DestinationFormat),
  57. FileConfig: FileConfig{
  58. CSVOptions: CSVOptions{
  59. FieldDelimiter: qe.FieldDelimiter,
  60. },
  61. },
  62. },
  63. DisableHeader: qe.PrintHeader != nil && !*qe.PrintHeader,
  64. Src: bqToTable(qe.SourceTable, c),
  65. }
  66. }
  67. // An Extractor extracts data from a BigQuery table into Google Cloud Storage.
  68. type Extractor struct {
  69. JobIDConfig
  70. ExtractConfig
  71. c *Client
  72. }
  73. // ExtractorTo returns an Extractor which can be used to extract data from a
  74. // BigQuery table into Google Cloud Storage.
  75. // The returned Extractor may optionally be further configured before its Run method is called.
  76. func (t *Table) ExtractorTo(dst *GCSReference) *Extractor {
  77. return &Extractor{
  78. c: t.c,
  79. ExtractConfig: ExtractConfig{
  80. Src: t,
  81. Dst: dst,
  82. },
  83. }
  84. }
  85. // Run initiates an extract job.
  86. func (e *Extractor) Run(ctx context.Context) (j *Job, err error) {
  87. ctx = trace.StartSpan(ctx, "cloud.google.com/go/bigquery.Extractor.Run")
  88. defer func() { trace.EndSpan(ctx, err) }()
  89. return e.c.insertJob(ctx, e.newJob(), nil)
  90. }
  91. func (e *Extractor) newJob() *bq.Job {
  92. return &bq.Job{
  93. JobReference: e.JobIDConfig.createJobRef(e.c),
  94. Configuration: e.ExtractConfig.toBQ(),
  95. }
  96. }