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