A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/bigquery/docs/scheduling-queries below:

Scheduling queries | BigQuery | Google Cloud

Stay organized with collections Save and categorize content based on your preferences.

Scheduling queries

This page describes how to schedule recurring queries in BigQuery.

You can schedule queries to run on a recurring basis. Scheduled queries must be written in GoogleSQL, which can include data definition language (DDL) and data manipulation language (DML) statements. You can organize query results by date and time by parameterizing the query string and destination table.

When you create or update the schedule for a query, the scheduled time for the query is converted from your local time to UTC. UTC is not affected by daylight saving time.

Before you begin Required permissions

To schedule a query, you need the following IAM permissions:

To modify or delete a scheduled query, you must either have the bigquery.transfers.update and bigquery.transfers.get permissions, or the bigquery.jobs.create permission and ownership over the scheduled query.

The predefined BigQuery Admin (roles/bigquery.admin) IAM role includes the permissions that you need in order to schedule or modify a query.

For more information about IAM roles in BigQuery, see Predefined roles and permissions.

To create or update scheduled queries run by a service account, you must have access to that service account. For more information on granting users the service account role, see Service Account user role. To select a service account in the scheduled query UI of the Google Cloud console, you need the following IAM permissions:

Note: If you are using the bq command-line tool, use the --service_account_name flag instead of authenticating as a service account. Configuration options

The following sections describe the configuration options.

Query string

The query string must be valid and written in GoogleSQL. Each run of a scheduled query can receive the following query parameters.

To manually test a query string with @run_time and @run_date parameters before scheduling a query, use the bq command-line tool.

Available parameters Parameter GoogleSQL Type Value @run_time TIMESTAMP Represented in UTC time. For regularly scheduled queries, run_time represents the intended time of execution. For example, if the scheduled query is set to "every 24 hours", the run_time difference between two consecutive queries is exactly 24 hours, even though the actual execution time might slightly vary. @run_date DATE Represents a logical calendar date. Example

The @run_time parameter is part of the query string in this example, which queries a public dataset named hacker_news.stories.

SELECT @run_time AS time,
  title,
  author,
  text
FROM `bigquery-public-data.hacker_news.stories`
LIMIT
  1000
Destination table

If the destination table for your results doesn't exist when you set up the scheduled query, BigQuery attempts to create the table for you.

If you are using a DDL or DML query, then in the Google Cloud console, choose the Processing location or region. Processing location is required for DDL or DML queries that create the destination table.

If the destination table does exist and you are using the WRITE_APPEND write preference, BigQuery appends data to the destination table and tries to map the schema. BigQuery automatically allows field additions and reordering, and accommodates missing optional fields. If the table schema changes so much between runs that BigQuery can't process the changes automatically, the scheduled query fails.

Queries can reference tables from different projects and different datasets. When configuring your scheduled query, you don't need to include the destination dataset in the table name. You specify the destination dataset separately.

The destination dataset and table for a scheduled query must be in the same project as the scheduled query.

Write preference

The write preference you select determines how your query results are written to an existing destination table.

If you're using a DDL or DML query, you can't use the write preference option.

Creating, truncating, or appending a destination table only happens if BigQuery is able to successfully complete the query. Creation, truncation, or append actions occur as one atomic update upon job completion.

Clustering

Scheduled queries can create clustering on new tables only, when the table is made with a DDL CREATE TABLE AS SELECT statement. See Creating a clustered table from a query result on the Using data definition language statements page.

Partitioning options

Scheduled queries can create partitioned or non-partitioned destination tables. Partitioning is available in the Google Cloud console, bq command-line tool, and API setup methods. If you're using a DDL or DML query with partitioning, leave the Destination table partitioning field blank.

You can use the following types of table partitioning in BigQuery:

To create a partitioned table by using a scheduled query in the Google Cloud console, use the following options:

Available parameters

When setting up the scheduled query, you can specify how you want to partition the destination table with runtime parameters.

Parameter Template Type Value run_time Formatted timestamp In UTC time, per the schedule. For regularly scheduled queries, run_time represents the intended time of execution. For example, if the scheduled query is set to "every 24 hours", the run_time difference between two consecutive queries is exactly 24 hours, even though the actual execution time may vary slightly.

See TransferRun.runTime.

run_date Date string The date of the run_time parameter in the following format: %Y-%m-%d; for example, 2018-01-01. This format is compatible with ingestion-time partitioned tables. Templating system

Scheduled queries support runtime parameters in the destination table name with a templating syntax.

Parameter templating syntax

The templating syntax supports basic string templating and time offsetting. Parameters are referenced in the following formats:

Parameter Purpose run_date This parameter is replaced by the date in format YYYYMMDD. run_time This parameter supports the following properties:


offset
Time offset expressed in hours (h), minutes (m), and seconds (s) in that order.
Days (d) are not supported.
Decimals are allowed, for example: 1.5h.

time_format
A formatting string. The most common formatting parameters are years (%Y), months (%m), and days (%d).
For partitioned tables, YYYYMMDD is the required suffix - this is equivalent to "%Y%m%d".

Read more about formatting datetime elements.

Usage notes: Parameter templating examples

These examples demonstrate specifying destination table names with different time formats, and offsetting the run time.

run_time (UTC) Templated parameter Output destination table name 2018-02-15 00:00:00 mytable mytable 2018-02-15 00:00:00 mytable_{run_time|"%Y%m%d"} mytable_20180215 2018-02-15 00:00:00 mytable_{run_time+25h|"%Y%m%d"} mytable_20180216 2018-02-15 00:00:00 mytable_{run_time-1h|"%Y%m%d"} mytable_20180214 2018-02-15 00:00:00 mytable_{run_time+1.5h|"%Y%m%d%H"}
or
mytable_{run_time+90m|"%Y%m%d%H"} mytable_2018021501 2018-02-15 00:00:00 {run_time+97s|"%Y%m%d"}_mytable_{run_time+97s|"%H%M%S"} 20180215_mytable_000137 Using a service account

You can set up a scheduled query to authenticate as a service account. A service account is a special account associated with your Google Cloud project. The service account can run jobs, such as scheduled queries or batch processing pipelines, with its own service credentials rather than an end user's credentials.

Read more about authenticating with service accounts in Introduction to authentication.

Specify encryption key with scheduled queries

You can specify

customer-managed encryption keys (CMEKs)

to encrypt data for a transfer run. You can use a CMEK to support transfers from

scheduled queries

.

When you specify a CMEK with a transfer, the BigQuery Data Transfer Service applies the CMEK to any intermediate on-disk cache of ingested data so that the entire data transfer workflow is CMEK compliant.

You cannot update an existing transfer to add a CMEK if the transfer was not originally created with a CMEK. For example, you cannot change a destination table that was originally default encrypted to now be encrypted with CMEK. Conversely, you also cannot change a CMEK-encrypted destination table to have a different type of encryption.

You can update a CMEK for a transfer if the transfer configuration was originally created with a CMEK encryption. When you update a CMEK for a transfer configuration, the BigQuery Data Transfer Service propagates the CMEK to the destination tables at the next run of the transfer, where the BigQuery Data Transfer Service replaces any outdated CMEKs with the new CMEK during the transfer run. For more information, see Update a transfer.

You can also use project default keys. When you specify a project default key with a transfer, the BigQuery Data Transfer Service uses the project default key as the default key for any new transfer configurations.

Set up scheduled queries

For a description of the schedule syntax, see Formatting the schedule. For details about schedule syntax, see Resource: TransferConfig.

Console
  1. Open the BigQuery page in the Google Cloud console.

    Go to BigQuery

  2. Run the query that you're interested in. When you are satisfied with your results, click Schedule.

  3. The scheduled query options open in the New scheduled query pane.

  4. On the New scheduled query pane:

  5. For a GoogleSQL SELECT query, select the Set a destination table for query results option and provide the following information about the destination dataset.

  6. Choose the Location Type.

  7. Advanced options:

  8. Additional configurations:

  9. Click Save.

bq There are two ways to schedule a query by using the bq command-line tool. Option 2 lets you schedule the query with more options.

Option 1: Use the bq query command.

To create a scheduled query, add the options destination_table (or target_dataset), --schedule, and --display_name to your bq query command.

bq query \
--display_name=name \
--destination_table=table \
--schedule=interval

Replace the following:

Optional flags:

If both `--replace` and `--append_table` aren't specified when scheduling the query, no write preference is set. Depending on the query, an error in subsequent scheduled runs might result.

For example, the following command creates a scheduled query named My Scheduled Query using the query SELECT 1 from mydataset.test. The destination table is mytable in the dataset mydataset. The scheduled query is created in the default project:

    bq query \
    --use_legacy_sql=false \
    --destination_table=mydataset.mytable \
    --display_name='My Scheduled Query' \
    --schedule='every 24 hours' \
    --replace=true \
    'SELECT
      1
    FROM
      mydataset.test'


Option 2: Use the bq mk command.

Scheduled queries are a kind of transfer. To schedule a query, you can use the bq command-line tool to make a transfer configuration.

Queries must be in StandardSQL dialect to be scheduled.

Enter the bq mk command and supply the following required flags:

Optional flags:

bq mk \
--transfer_config \
--target_dataset=dataset \
--display_name=name \
--params='parameters' \
--data_source=data_source

Replace the following:

Note: To write results to an ingestion-time partitioned table, see the instructions in Destination table. A scheduled query fails if you create a transfer configuration with the destination_table_name_template parameter set to an ingestion-time partitioned table while also supplying an error if setting to an ingestion-time partitioned the partitioning_field parameter. Note: You cannot configure notifications using the command-line tool.

For example, the following command creates a scheduled query transfer configuration named My Scheduled Query using the query SELECT 1 from mydataset.test. The destination table mytable is truncated for every write, and the target dataset is mydataset. The scheduled query is created in the default project, and authenticates as a service account:

bq mk \
--transfer_config \
--target_dataset=mydataset \
--display_name='My Scheduled Query' \
--params='{"query":"SELECT 1 from mydataset.test","destination_table_name_template":"mytable","write_disposition":"WRITE_TRUNCATE"}' \
--data_source=scheduled_query \
--service_account_name=abcdef-test-sa@abcdef-test.iam.gserviceaccount.com

The first time you run the command, you receive a message like the following:

[URL omitted] Please copy and paste the above URL into your web browser and follow the instructions to retrieve an authentication code.

Follow the instructions in the message and paste the authentication code on the command line.

API

Use the projects.locations.transferConfigs.create method and supply an instance of the TransferConfig resource.

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Set up scheduled queries with a service account View scheduled query status Console

To view the status of your scheduled queries, in the navigation menu, click Scheduling and filter for Scheduled Query. Click a scheduled query to get more details about it.

bq

Scheduled queries are a kind of transfer. To show the details of a scheduled query, you can first use the bq command-line tool to list your transfer configurations.

Enter the bq ls command and supply the transfer flag --transfer_config. The following flags are also required:

For example:

bq ls \
--transfer_config \
--transfer_location=us

To show the details of a single scheduled query, enter the bq show command and supply the transfer_path for that scheduled query or transfer config.

For example:

bq show \
--transfer_config \
projects/862514376110/locations/us/transferConfigs/5dd12f26-0000-262f-bc38-089e0820fe38
API

Use the projects.locations.transferConfigs.list method and supply an instance of the TransferConfig resource.

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Update scheduled queries Console

To update a scheduled query, follow these steps:

  1. In the navigation menu, click Scheduled queries or Scheduling.
  2. In the list of scheduled queries, click the name of the query that you want to change.
  3. On the Scheduled query details page that opens, click Edit.
  4. Optional: Change the query text in the query editing pane.
  5. Click Schedule query and then select Update scheduled query.
  6. Optional: Change any other scheduling options for the query.
  7. Click Update.
bq

Scheduled queries are a kind of transfer. To update scheduled query, you can use the bq command-line tool to make a transfer configuration.

Enter the bq update command with the required --transfer_config flag.

Optional flags:

bq update \
--target_dataset=dataset \
--display_name=name \
--params='parameters'
--transfer_config \
RESOURCE_NAME

Replace the following:

Note: To write results to an ingestion-time partitioned table, see the instructions in Destination table. A scheduled query fails if you create a transfer configuration with the destination_table_name_template parameter set to an ingestion-time partitioned table while also supplying an error if setting to an ingestion-time partitioned the partitioning_field parameter. Note: You cannot configure notifications using the command-line tool.

For example, the following command updates a scheduled query transfer configuration named My Scheduled Query using the query SELECT 1 from mydataset.test. The destination table mytable is truncated for every write, and the target dataset is mydataset:

bq update \
--target_dataset=mydataset \
--display_name='My Scheduled Query' \
--params='{"query":"SELECT 1 from mydataset.test","destination_table_name_template":"mytable","write_disposition":"WRITE_TRUNCATE"}'
--transfer_config \
projects/myproject/locations/us/transferConfigs/1234a123-1234-1a23-1be9-12ab3c456de7
API

Use the projects.transferConfigs.patch method and supply the transfer's Resource Name using the transferConfig.name parameter. If you don't know the transfer's Resource Name, use the bq ls --transfer_config --transfer_location=location command to list all transfers or call the projects.locations.transferConfigs.list method and supply the project ID using the parent parameter.

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Note: You can't update the location of a scheduled query. If you move a source or destination dataset used in a scheduled query, then you need to create a new scheduled query in the new location. Update scheduled queries with ownership restrictions

If you try to update a scheduled query you don't own, the update might fail with the following error message:

Cannot modify restricted parameters without taking ownership of the transfer configuration.

The owner of the scheduled query is the user associated with the scheduled query or the user who has access to the service account associated with the scheduled query. The associated user can be seen in the configuration details of the scheduled query. For information on how to update the scheduled query to take ownership, see Update scheduled query credentials. To grant users access to a service account, you must have the Service Account user role.

The owner restricted parameters for scheduled queries are:

Update scheduled query credentials

If you're scheduling an existing query, you might need to update the user credentials on the query. Credentials are automatically up to date for new scheduled queries.

Some other situations that could require updating credentials include the following:

Note: If you are not the owner of the schedule query, you must have the bigquery.transfers.update permission on your Google Cloud project to update the scheduled query credentials. For more information, see Required permissions. Console

To refresh the existing credentials on a scheduled query:

  1. Find and view the status of a scheduled query.

  2. Click the MORE button and select Update credentials.

  3. Allow 10 to 20 minutes for the change to take effect. You might need to clear your browser's cache.

Caution: Changing the credentials used in a scheduled query to a service account is not supported in the Google Cloud console. bq

Scheduled queries are a kind of transfer. To update the credentials of a scheduled query, you can use the bq command-line tool to update the transfer configuration.

Enter the bq update command and supply the transfer flag --transfer_config. The following flags are also required:

Optional flag:

For example, the following command updates a scheduled query transfer configuration to authenticate as a service account:

bq update \
--update_credentials \
--service_account_name=abcdef-test-sa@abcdef-test.iam.gserviceaccount.com
--transfer_config \
projects/myproject/locations/us/transferConfigs/1234a123-1234-1a23-1be9-12ab3c456de7
Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Set up a manual run on historical dates

In addition to scheduling a query to run in the future, you can also trigger immediate runs manually. Triggering an immediate run would be necessary if your query uses the run_date parameter, and there were issues during a prior run.

For example, every day at 09:00 you query a source table for rows that match the current date. However, you find that data wasn't added to the source table for the last three days. In this situation, you can set the query to run on historical data within a date range that you specify. Your query runs using combinations of run_date and run_time parameters that correspond to the dates you configured in your scheduled query.

After setting up a scheduled query, here's how you can run the query by using a historical date range:

Console

After clicking Schedule to save your scheduled query, you can click the Scheduled queries button to see the list of scheduled queries. Click any display name to see the query schedule's details. At the top right of the page, click Schedule backfill to specify a historical date range.

The chosen runtimes are all within your selected range, including the first date and excluding the last date.

Warning: The date ranges you provide are in UTC, but your query's schedule is displayed in your local time zone (see Example 2 to work around this issue).

Example 1

Your scheduled query is set to run every day 09:00 Pacific Time. You're missing data from January 1, January 2, and January 3. Choose the following historic date range:

Start Time = 1/1/19
End Time = 1/4/19

Your query runs using run_date and run_time parameters that correspond to the following times:

Example 2

Your scheduled query is set to run every day 23:00 Pacific Time. You're missing data from January 1, January 2, and January 3. Choose the following historic date ranges (later dates are chosen because UTC has a different date at 23:00 Pacific Time):

Start Time = 1/2/19
End Time = 1/5/19

Your query runs using run_date and run_time parameters that correspond to the following times:

After setting up manual runs, refresh the page to see them in the list of runs.

bq

To manually run the query on a historical date range:

Enter the bq mk command and supply the transfer run flag --transfer_run. The following flags are also required:

bq mk \
--transfer_run \
--start_time='start_time' \
--end_time='end_time' \
resource_name

Replace the following:

For example, the following command schedules a backfill for scheduled query resource (or transfer configuration): projects/myproject/locations/us/transferConfigs/1234a123-1234-1a23-1be9-12ab3c456de7.

  bq mk \
  --transfer_run \
  --start_time 2017-05-25T00:00:00Z \
  --end_time 2017-05-25T00:00:00Z \
  projects/myproject/locations/us/transferConfigs/1234a123-1234-1a23-1be9-12ab3c456de7

For more information, see bq mk --transfer_run.

API

Use the projects.locations.transferConfigs.scheduleRun method and supply a path of the TransferConfig resource.

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Set up alerts for scheduled queries

You can configure alert policies for scheduled queries based on row count metrics. For more information, see Set up alerts with scheduled queries.

Delete scheduled queries Console

To delete a scheduled query on the Scheduled queries page of the Google Cloud console, do the following:

  1. In the navigation menu, click Scheduled queries.
  2. In the list of scheduled queries, click the name of the scheduled query that you want to delete.
  3. On the Scheduled query details page, click Delete.

Alternatively, you can delete a scheduled query on the Scheduling page of the Google Cloud console:

  1. In the navigation menu, click Scheduling.
  2. In the list of scheduled queries, click the more_vert Actions menu for the scheduled query that you want to delete.
  3. Select Delete.

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

Disable or enable scheduled queries

To pause the scheduled runs of a selected query without deleting the schedule, you can disable the schedule.

To disable a schedule for a selected query, follow these steps:

  1. In the navigation menu of the Google Cloud console, click Scheduling.
  2. In the list of scheduled queries, click the more_vert Actions menu for the scheduled query that you want to disable.
  3. Select Disable.

To enable a disabled scheduled query, click the more_vert Actions menu for the scheduled query that you want to enable and select Enable.

Quotas

Scheduled queries are always run as batch query jobs and are subject to the same BigQuery quotas and limits as manual queries.

Although scheduled queries use features of BigQuery Data Transfer Service, they are not transfers and are not subject to the load jobs quota.

The identity used to execute the query determines which quotas are applied. This depends on the scheduled query's configuration:

Pricing

Scheduled queries are priced the same as manual BigQuery queries.

Supported regions Caution: Cross-region queries are not supported. The destination table for your scheduled query must be in the same region as the data being queried. The selected location for your scheduled query must also be the same region as the data being queried.

Scheduled queries are supported in the following locations.

Regions

The following table lists the regions in the Americas where BigQuery is available.

The following table lists the regions in Asia Pacific where BigQuery is available.

Region description Region name Details Delhi asia-south2 Hong Kong asia-east2 Jakarta asia-southeast2 Melbourne australia-southeast2 Mumbai asia-south1 Osaka asia-northeast2 Seoul asia-northeast3 Singapore asia-southeast1 Sydney australia-southeast1 Taiwan asia-east1 Tokyo asia-northeast1

The following table lists the regions in Europe where BigQuery is available.

The following table lists the regions in the Middle East where BigQuery is available.

Region description Region name Details Dammam me-central2 Doha me-central1 Tel Aviv me-west1

The following table lists the regions in Africa where BigQuery is available.

Region description Region name Details Johannesburg africa-south1 Multi-regions

The following table lists the multi-regions where BigQuery is available.

Multi-region description Multi-region name Data centers within member states of the European Union1 EU Data centers in the United States2 US Note: Selecting a multi-region location does not provide cross-region replication or regional redundancy, so there is no increase in dataset availability in the event of a regional outage. Data is stored in a single region within the geographic location.

1 Data located in the EU multi-region is only stored in one of the following locations: europe-west1 (Belgium) or europe-west4 (Netherlands). The exact location in which the data is stored and processed is determined automatically by BigQuery.

2 Data located in the US multi-region is only stored in one of the following locations: us-central1 (Iowa), us-west1 (Oregon), or us-central2 (Oklahoma). The exact location in which the data is stored and processed is determined automatically by BigQuery.

What's next

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4