您可以设置监控聚合操作来检查带有JavaScript表达式的查询以了解查询错误,并在查询失败时通过电子邮件自动通知您。可以完成此操作,只需在调用监控信息并填写生成的表单后执行以下步骤即可。
例如,如果您正在使用Python客户端库,则可以使用以下代码:
from google.cloud import bigquery_datatransfer_v1
client = bigquery_datatransfer_v1.DataTransferServiceClient()
parent = client.common_project_path(project_id)
transfer_config = bigquery_datatransfer_v1.TransferConfig()
transfer_config.destination_dataset_id = dst_dataset_id
transfer_config.display_name = "My Scheduled Query"
transfer_config.data_source_id = "scheduled_query"
params = {
"query": "SELECT * FROM my_table",
"destination_table_name_template": "my_table_",
"write_disposition": "WRITE_APPEND",
}
transfer_config.params = params
start_time = "2009-07-12T01:00:00Z"
interval = "3600s"
schedule_options = bigquery_datatransfer_v1.ScheduleOptions(
start_time=start_time, interval=interval
)
transfer_config.schedule_options = schedule_options
response = client.create_transfer_config(
parent, transfer_config, authorization_code=authorization_code
)
请确保在设置monitoring_config时将监控类型设置为 QUERY_DURATION,错误阈值设置为0,并提供聚合表达式,该表达式在查询实例失败时返回1。以下是一个示例:
TRANSFER_CONFIG_NAME = 'projects/PROJECT_NUMBER/locations/us/transferConfigs/TRANSFER_CONFIG_ID'
monitoring_config = {
'exponentialBackoffPolicy': {
'numTries': 5,
'retryDelay': '600s'
},
'bigqueryTable': {
'tableReference': {
'projectId': bigquery_project,
'datasetId': bigquery_dataset,
'tableId': bigquery_table
},
'query': 'SELECT COUNT(*) > 0 FROM `{}`'.format(bigquery_table),
'severity': 'ERROR