github.com/apache/beam/sdks/v2@v2.48.2/go/test/integration/io/xlang/bigquery/table.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package bigquery
    17  
    18  import (
    19  	"fmt"
    20  	"os/exec"
    21  	"time"
    22  )
    23  
    24  // newTempTable creates a new BigQuery table using BigQuery's Data Definition Language (DDL) and the
    25  // "bq query" console command. Reference: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
    26  // The tables created are set to expire after a day.
    27  //
    28  // newTable takes the name of a BigQuery dataset, the prefix for naming the table, and a DDL schema
    29  // for the data, and generates that table with a unique suffix and an expiration time of a day
    30  // later.
    31  func newTempTable(dataset, prefix, schema string) (string, error) {
    32  	name := fmt.Sprintf("%s.%s_temp_%v", dataset, prefix, time.Now().UnixNano())
    33  	query := fmt.Sprintf("CREATE TABLE `%s`(%s) OPTIONS(expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 1 DAY))", name, schema)
    34  	cmd := exec.Command("bq", "query", "--use_legacy_sql=false", query)
    35  	out, err := cmd.CombinedOutput()
    36  	if err != nil {
    37  		return "", fmt.Errorf("creating table through command \"%s\" failed with output:\n%s", cmd.String(), out)
    38  	}
    39  	return name, nil
    40  }
    41  
    42  // deleteTable deletes a BigQuery table using BigQuery's Data Definition Language (DDL) and the
    43  // "bq query" console command. Reference: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
    44  func deleteTempTable(table string) error {
    45  	query := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
    46  	cmd := exec.Command("bq", "query", "--use_legacy_sql=false", query)
    47  	out, err := cmd.CombinedOutput()
    48  	if err != nil {
    49  		return fmt.Errorf("deleting table through command \"%s\" failed with output:\n%s", cmd.String(), out)
    50  	}
    51  	return nil
    52  }