0

Create Scheduled Query

by
Published Jul 17, 2024

Creates a scheduled query in Google Cloud. [See the documentation](https://cloud.google.com/bigquery/docs/scheduling-queries)

Script gcloud Verified

The script

Submitted by hugo697 Bun
Verified 692 days ago
1
import { v1 as bqdt, protos } from "@google-cloud/bigquery-data-transfer";
2

3
const { CreateTransferConfigRequest, TransferConfig } =
4
  protos.google.cloud.bigquery.datatransfer.v1;
5

6
type Gcloud = {
7
  type: string;
8
  project_id: string;
9
  private_key_id: string;
10
  private_key: string;
11
  client_email: string;
12
  client_id: string;
13
  auth_uri: string;
14
  token_uri: string;
15
  auth_provider_x509_cert_url: string;
16
  client_x509_cert_url: string;
17
  universe_domain: string;
18
};
19

20
export async function main(
21
  resource: Gcloud,
22
  destinationDatasetId: string,
23
  datasetRegion: string = "us",
24
  displayName: string,
25
  query: string,
26
  schedule: string,
27
  writeDisposition: string = "WRITE_TRUNCATE",
28
  destinationTableNameTemplate: string = "logs"
29
) {
30
  const bqdtClient = new bqdt.DataTransferServiceClient({
31
    credentials: resource,
32
    projectId: resource.project_id,
33
  });
34

35
  const parent = bqdtClient.projectPath(resource.project_id);
36

37
  const transferConfig = new TransferConfig({
38
    displayName,
39
    dataSourceId: "scheduled_query",
40
    destinationDatasetId,
41
    schedule,
42
    datasetRegion,
43
    params: {
44
      fields: {
45
        query: { stringValue: query },
46
        destination_table_name_template: {
47
          stringValue: destinationTableNameTemplate,
48
        },
49
        write_disposition: { stringValue: writeDisposition },
50
      },
51
    },
52
  });
53

54
  const request = new CreateTransferConfigRequest({
55
    parent,
56
    transferConfig,
57
    serviceAccountName: resource.clientEmail,
58
  });
59

60
  const [response] = await bqdtClient.createTransferConfig(request);
61

62
  return response;
63
}
64