BigQuery调度是使用BigQuery本身的调度器来推定时任务并在特定时间或具有约束条件时运行查询作业。它可以通过web UI、命令行、REST API和客户端库来调度查询。
Cloud Scheduler是一项完全托管的服务,它可以在特定时间或固定间隔内运行作业。与BigQuery调度不同,它可以与其他GCP服务(如Pub/Sub、App Engine和Cloud Functions)结合使用来完成更复杂的任务。
以下是Cloud Scheduler的代码示例:
import os
from google.oauth2 import service_account
from google.cloud import scheduler_v1 as scheduler
credentials = service_account.Credentials.from_service_account_file(
os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
client = scheduler.CloudSchedulerClient(credentials=credentials)
parent = client.location_path('[PROJECT_ID]', '[LOCATION]')
job = {
'http_target': {
'uri': 'http://example.com',
'http_method': 'POST',
'headers': {
'Content-type': 'application/json'
},
'body': 'Hello world!'
},
'schedule': '* 1 * * *',
'time_zone': 'America/New_York'
}
response = client.create_job(parent, job)
import os
from google.oauth2 import service_account
from google.cloud import scheduler_v1 as scheduler
credentials = service_account.Credentials.from_service_account_file(
os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
client = scheduler.CloudSchedulerClient(credentials=credentials)
parent = client.location_path('[PROJECT_ID]', '[LOCATION]')
job = {
'pubsub_target': {
'topic_name': 'projects/[PROJECT_ID]/topics/[TOPIC_NAME]',
'data': 'Hello world!'
},
'schedule': '* 1 * * *',
'time_zone': 'America/New_York'
}
response = client.create_job(parent, job)