Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger On-Demand scheduled Query in Google Bigquery

I created a query in Google Bigquery which is connected to a Topic which triggers the Cloud function. I want to schedule the query on-demand run. Once I schedule the query I am not able to find any option in UI to run that particular Query. Please let me know if some more clarification is required. I only see the following options in scheduled query UI page.

enter image description here

like image 781
Vipendra Singh Avatar asked Feb 27 '20 08:02

Vipendra Singh


People also ask

How do I create a scheduled query in BigQuery?

Open the BigQuery page in the Cloud Console. Run the query that you're interested in. When you are satisfied with your results, click Schedule and Create new scheduled query. The scheduled query options open in the New scheduled query pane. For Name for the scheduled query, enter a name such as My scheduled query.

How do I run a scheduled query in Google Analytics?

Set “runQuery” as the function to execute. Click on deploy! If you have correctly set up everything, your scheduled query will now start to run as soon as a new daily table has been exported to BigQuery! This means that you can sit back, relax and enjoy that your Google Analytics data is always as fresh as it can be.

How to trigger a cloud run action on a BigQuery event?

Now, try out the BigQuery -> Cloud Run trigger and action. Go to the BigQuery console and insert a row or two: Watch as a new table called created_by_trigger gets created! You have successfully triggered a Cloud Run action on a database event in BigQuery.

How do I make a query run on demand?

If your query is scheduled to run in any time frame (daily, weekly, etc), you can make it run on-demand using the option "Schedule backfill". This option ask you to provide a start date and an end date, so it force all runs that were supposed to run in the given time window (yes, using this option the number of runs will depend of the schedule).


2 Answers

UPDATE Jul/2021: Google improved its UI, so now it is possible to run scheduled queries on-demand (check Julio's answer for details). However, according to him, the command line option described here runs fasters than the UI option.


Sadly, you can't. I don't know if this is a bug or not (probably a bad UI design), but you can't run on-demand scheduled queries via the UI.

This only applies to scheduled queries set to run on-demand. If your query is scheduled to run in any time frame (daily, weekly, etc), you can make it run on-demand using the option "Schedule backfill". This option ask you to provide a start date and an end date, so it force all runs that were supposed to run in the given time window (yes, using this option the number of runs will depend of the schedule). That is, if your query is set to run daily, just provide one day time span that your query will run once.

Alternatively, if you really need to run it on-demand, you will need to use the command line or the API, as shown below.

Command line solution (using Cloud Shell):

Run the command:

bq mk --transfer_run --run_time "$(date --iso-8601=seconds)" \
  projects/[YOUR_PROJECT]/locations/us/transferConfigs/[YOUR_SCHEDULED_QUERY_GUID]

Notice that YOUR_SCHEDULED_QUERY_ID is a GUID (it is not the the Scheduled Query name, it looks like 1234a123-1234-1a23-1be9-12ab3c456de7). You can copy it from the browser URL or get from a list of all scheduled queries running the command bq ls --transfer_config --transfer_location='us'.

In all places, change us for any other location you may be using (e.g. eu).

API solution

Use transferConfigs.startManualRuns API using the same parameters as the command line option.

Notice the parameter requestedRunTime is mandatory. Its value is only relevant if you use @run_time parameter in your query, otherwise it is not used. You can even hardcode a date if you want. So just populate it with any valid datetime in the format 2020-08-04T00:00:0Z and you're done (in the command line solution, I populate it with the command date --iso-8601=seconds).

like image 86
Diego Queiroz Avatar answered Oct 14 '22 05:10

Diego Queiroz


A bit more late to the party, but you can run it on-demand by clicking on "Schedule backfill", in the pop-up window fill in the boxes with dates from yesterday to today and that's it. This works right for queries which don't depend on current dates (joins, copies, etc).

UPDATE: Now it is even more easier, they implemented a button to run it directly once. Still, I consider it is faster to use Diego Queiroz 's solution, as it runs the process 2x faster than this way.

scheduled_queries

like image 6
Julio Avatar answered Oct 14 '22 05:10

Julio