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.
 
 
 

108 lines
3.2 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. bq "google.golang.org/api/bigquery/v2"
  18. )
  19. // CopyConfig holds the configuration for a copy job.
  20. type CopyConfig struct {
  21. // Srcs are the tables from which data will be copied.
  22. Srcs []*Table
  23. // Dst is the table into which the data will be copied.
  24. Dst *Table
  25. // CreateDisposition specifies the circumstances under which the destination table will be created.
  26. // The default is CreateIfNeeded.
  27. CreateDisposition TableCreateDisposition
  28. // WriteDisposition specifies how existing data in the destination table is treated.
  29. // The default is WriteEmpty.
  30. WriteDisposition TableWriteDisposition
  31. // The labels associated with this job.
  32. Labels map[string]string
  33. // Custom encryption configuration (e.g., Cloud KMS keys).
  34. DestinationEncryptionConfig *EncryptionConfig
  35. }
  36. func (c *CopyConfig) toBQ() *bq.JobConfiguration {
  37. var ts []*bq.TableReference
  38. for _, t := range c.Srcs {
  39. ts = append(ts, t.toBQ())
  40. }
  41. return &bq.JobConfiguration{
  42. Labels: c.Labels,
  43. Copy: &bq.JobConfigurationTableCopy{
  44. CreateDisposition: string(c.CreateDisposition),
  45. WriteDisposition: string(c.WriteDisposition),
  46. DestinationTable: c.Dst.toBQ(),
  47. DestinationEncryptionConfiguration: c.DestinationEncryptionConfig.toBQ(),
  48. SourceTables: ts,
  49. },
  50. }
  51. }
  52. func bqToCopyConfig(q *bq.JobConfiguration, c *Client) *CopyConfig {
  53. cc := &CopyConfig{
  54. Labels: q.Labels,
  55. CreateDisposition: TableCreateDisposition(q.Copy.CreateDisposition),
  56. WriteDisposition: TableWriteDisposition(q.Copy.WriteDisposition),
  57. Dst: bqToTable(q.Copy.DestinationTable, c),
  58. DestinationEncryptionConfig: bqToEncryptionConfig(q.Copy.DestinationEncryptionConfiguration),
  59. }
  60. for _, t := range q.Copy.SourceTables {
  61. cc.Srcs = append(cc.Srcs, bqToTable(t, c))
  62. }
  63. return cc
  64. }
  65. // A Copier copies data into a BigQuery table from one or more BigQuery tables.
  66. type Copier struct {
  67. JobIDConfig
  68. CopyConfig
  69. c *Client
  70. }
  71. // CopierFrom returns a Copier which can be used to copy data into a
  72. // BigQuery table from one or more BigQuery tables.
  73. // The returned Copier may optionally be further configured before its Run method is called.
  74. func (t *Table) CopierFrom(srcs ...*Table) *Copier {
  75. return &Copier{
  76. c: t.c,
  77. CopyConfig: CopyConfig{
  78. Srcs: srcs,
  79. Dst: t,
  80. },
  81. }
  82. }
  83. // Run initiates a copy job.
  84. func (c *Copier) Run(ctx context.Context) (*Job, error) {
  85. return c.c.insertJob(ctx, c.newJob(), nil)
  86. }
  87. func (c *Copier) newJob() *bq.Job {
  88. return &bq.Job{
  89. JobReference: c.JobIDConfig.createJobRef(c.c),
  90. Configuration: c.CopyConfig.toBQ(),
  91. }
  92. }