0
votes

I'm trying to create a scheduling DELETE query in Go, but I can't find any example in the bigquery documentation.

In this documentation, the only example they provided is for java and python. Is there any example on how to create a schedule query in Go? https://cloud.google.com/bigquery/docs/scheduling-queries

1

1 Answers

1
votes

You can find examples inside go library. Basically here are the examples of usage https://github.com/googleapis/google-cloud-go/blob/master/bigquery/datatransfer/apiv1/data_transfer_client_example_test.go#L84

You should create a TransferConfig https://cloud.google.com/bigquery-transfer/docs/reference/datatransfer/rpc/google.cloud.bigquery.datatransfer.v1#google.cloud.bigquery.datatransfer.v1.TransferConfig which is a part of gcloud API and you can use it from Go as well, even though it's not documented explicitly

Here is how you can achieve this:

import (
    datatransfer "cloud.google.com/go/bigquery/datatransfer/apiv1"
    "context"
    "fmt"
    structpb "github.com/golang/protobuf/ptypes/struct"
    datatransferpb "google.golang.org/genproto/googleapis/cloud/bigquery/datatransfer/v1"
)


func main() {
    projectID := "projectID"
    datasetID := "DatasetID"

    serviceAccountName := "[email protected]"
    ctx := context.Background()
    c, err := datatransfer.NewClient(ctx)
    if err != nil {
        panic(err)
    }

    transConf := &datatransferpb.TransferConfig{
        Name: "test",
        DisplayName: "TestQuery",
        DataSourceId: "scheduled_query",
        Destination: &datatransferpb.TransferConfig_DestinationDatasetId{
            DestinationDatasetId: datasetID,
        },
        Params: &structpb.Struct{
            Fields: map[string]*structpb.Value {
                "query": &structpb.Value {
                    Kind:  &structpb.Value_StringValue{ "DELETE FROM `datasetID.table` where field='Value' "},
            },
        },
    },
        Schedule: "every 15 minutes",
    }

    _, err = c.CreateTransferConfig(ctx, &datatransferpb.CreateTransferConfigRequest{
        Parent: fmt.Sprintf("projects/%s", projectID),
        TransferConfig: transConf,
        ServiceAccountName: serviceAccountName,
    })

    if err != nil {
        panic(err)
    }
}