赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 日志服务
  4. 开发参考
  5. 日志服务SDK
  6. Python SDK
  7. 管理定时SQL
  8. 更新定时SQL任务

更新定时SQL任务

  • 管理定时SQL
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

调用UpdateScheduledSQL接口更新定时SQL任务。

前提条件

您已完成以下操作:

  • 开通日志服务。

  • 初始化日志服务Python SDK。

参数说明

def update_scheduled_sql(self, project_name, scheduled_sql):

请求参数

名称

类型

是否必填

描述

示例值

project_name

String

是

Project名称。

ali-test-project

scheduled_sql

object

是

定时 SQL 任务配置

-

scheduled_sql参数说明:

名称

类型

是否必填

描述

示例值

name

String

是

定时SQL任务名称。其命名规则如下:

  • 同一个Project下,不可重复。

  • 只能包含小写字母、数字、短划线(-)和下划线(_)。

  • 必须以小写字母或者数字开头和结尾。

  • 长度为4~63字符。

test-001

displayName

String

是

任务显示名。

test-001

description

String

否

任务描述。

创建一个定时SQL任务。

schedule

object

是

任务调度配置。

-

configuration

object

是

定时SQL配置信息。

-

schedule参数说明:

名称

类型

是否必填

描述

示例值

type

String

是

定时SQL类型。支持5种枚举值:

  • Cron:根据cronExpression参数指定的Cron表达式执行调度。

  • Hourly:每小时执行一次,整点执行。

  • Daily:根据hour参数指定的小时,每天执行一次。

  • Weekly:根据dayOfWeek与hour参数指定的日期和小时,每周执行一次。

  • FixedRate:每隔一段时间执行一次,通过interval参数指定执行间隔。

Cron

cronExpression

String

否

Cron表达式。

0/5 * * * *

runImmediately

bool

否

定时任务是否立即执行。

False

timeZone

String

否

Cron表达式所在时区,默认为空,表示东八区。

+0800

delay

int

否

任务延迟执行(单位是秒:s),不能大于86400。

4

interval

String

否

当type为FixRate时,interval参数表示执行的间隔,不能大于30天。支持 d(天)、h(小时)、m(分钟)、s(秒)四种单位。例如3h。不支持多种单位组合,例如不支持3h5m。

1m

configuration参数说明:

名称

类型

是否必填

描述

示例值

script

String

是

定时SQL分析语句。

* | select *

sqlType

String

是

SQL类型。

searchQuery

destEndpoint

String

是

目标Endpoint。

cn-hangzhou-intranet.log.aliyuncs.com

destProject

String

是

目标Project。

project-demo

sourceLogstore

String

是

源库。

source-logstore-demo

destLogstore

String

是

目标库。

dest-logstore-demo

roleArn

String

是

执行SQL授权角色ARN。

acs:ram::123456789:role/aliyunlogetlrole

destRoleArn

String

是

写目标授权角色ARN。

acs:ram::123456789:role/aliyunlogetlrole

fromTimeExpr

String

是

SQL时间窗口开始。

@m-1m

toTimeExpr

String

是

SQL时间窗口结束。

@m

maxRunTimeInSeconds

int

是

SQL超时最长时间,单位:秒,取值范围 60~1800。

600

resourcePool

String

是

资源池类型(enhanced 表示增强型)。

enhanced

maxRetries

int

是

SQL超时最大次数,取值范围 1~100。

20

fromTime

int

是

开始时间。更多信息,请参见创建定时SQL-日志库导入时序库。

1712592000

toTime

int

是

结束时间。更多信息,请参见创建定时SQL-日志库导入时序库。

0

dataFormat

String

是

支持三种配置:log2log、log2metric和metric2metric。

log2log

parameters

object

是

SQL配置。更多信息请参见创建定时SQL-日志库导入时序库。

{
 addLabels: "{}",
 hashLabels: "[]",
 labelKeys: "[\"your label1\",\"your label2\"]",
 metricKeys: "[\"your Indicator1\",\"your Indicator2\"]",
 metricName: "",
 timeKey: ""
}

返回参数

返回参数说明,请参见UpdateScheduledSQL - 更新定时SQL任务。

示例代码

import os
import time
from aliyun.log import LogClient
from aliyun.log.scheduled_sql import *

accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')  # The AccessKeyId
accessKeySecret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')  # The AccessKeySecret

endpoint = "cn-hangzhou.log.aliyuncs.com"  # The  source endpoint of the project's region
roleArn = "acs:ram::141******5616316:role/aliyunserviceroleforslsaudit"  # The roleArn
project = "demo-test-project"  # The source project name
source_logstore = "test-logstore"  # The source logstore name
source_metricstore = ""  # The source metricstore name

dest_endpoint = "cn-hangzhou.log.aliyuncs.com"  # The endpoint of the destination project's region
dest_role_arn = "acs:ram::141******5616316:role/aliyunserviceroleforslsaudit"  # The destination roleArn
dest_project = "demo-test-project"  # The destination project name
dest_logstore = "test-logstore2"  # The destination logstore name
dest_metricstore = ""  # The destination metricstore name

from_time = int(time.time()) - 300  # The start time of the scheduled SQL task

job_name = "test-001"  # The job name
display_name = "test-001-update"  # The display name
description = "修改一个定时SQL任务"  # The description

script = "* | select *"  # The SQL script
promql = "* | select promql_query_range('key{}') from metrics limit 1000"  # The PromQL script

instance_id = ""  # The job instanceId for schedule sql
delay_seconds = 0  # the delay seconds for schedule sql

# three possible values for the variable data_format  : "log2log" , "log2metric" ,"metric2metric"
data_format = "log2log"

# Possible values for the variable schedule_type: "FixedRate", "Daily", "Weekly", "Hourly", "Cron"
# schedule_type = "FixedRate"
schedule_type = "FixedRate"

client = LogClient(endpoint, accessKeyId, accessKeySecret)


def update_schedule_sql():
    scheduled_sql_config = get_schedule_sql()

    configuration = scheduled_sql_config['configuration']
    schedule = scheduled_sql_config['schedule']

    configuration['fromTimeExpr'] = '@m-3m'
    configuration['maxRetries'] = 25

    schedule['type'] = 'Cron'
    schedule['timeZone'] = '+0800'
    schedule['interval'] = ''
    schedule['dayOfWeek'] = ''
    schedule['hour'] = ''
    schedule['cronExpression'] = '*/2 * * * *'

    schedule = dict_to_schedule_rule(schedule)
    configuration = dict_to_scheduled_sql_config(configuration)

    schedule_sql = ScheduledSQL()
    schedule_sql.setSchedule(schedule)
    schedule_sql.setConfiguration(configuration)
    schedule_sql.setName(job_name)
    schedule_sql.setDisplayName(display_name)
    schedule_sql.setDescription(description)

    update_schedule_sql_response = client.update_scheduled_sql(dest_project, schedule_sql)
    update_schedule_sql_response.log_print()


def get_schedule_sql():
    get_schedule_sql_response = client.get_scheduled_sql(dest_project, job_name)
    scheduled_sql_config = get_schedule_sql_response.scheduled_sql_config
    return scheduled_sql_config


if __name__ == "__main__":
    update_schedule_sql()
    pass

示例返回结果

UpdateScheduledSqlJobResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 01 Nov 2024 08:18:08 GMT', 'x-log-time': '1730449088', 'x-log-requestid': '67248EC069A98605C79EED79'}

Process finished with exit code 0

相关文档

  • 管理定时SQL的API请参见:

    • CreateScheduledSQL - 创建定时SQL任务

    • DeleteScheduledSQL - 删除定时SQL任务

    • UpdateScheduledSQL - 更新定时SQL任务

    • EnableScheduledSQL - 开启定时SQL

    • DisableScheduledSQL - 关闭定时SQL

    • ListScheduledSQLs - 列出定时SQL任务

    • GetScheduledSQL - 获取定时SQL任务

  • 更多示例代码,请参见Aliyun Log Python SDK on GitHub。

相关文章

创建定时SQL任务 2025-04-22 10:33

调用CreateScheduledSQL接口创建定时SQL任务。 前提条件 您已完成以下操作:

更新定时SQL任务 2025-04-22 10:33

调用UpdateScheduledSQL接口更新定时SQL任务。 前提条件 您已完成以下操作:

删除定时SQL任务 2025-04-22 10:33

调用DeleteScheduledSQL接口删除定时SQL任务。 前提条件 您已完成以下操作:

开启定时SQL 2025-04-22 10:33

调用EnableScheduledSQL接口开启定时SQL。 前提条件 已开通日志服务。 已安装Python、Python开发环境

关闭定时SQL 2025-04-22 10:33

调用DisableScheduledSQL接口关闭定时SQL。 前提条件 您已完成以下操作: <

列出定时SQL任务 2025-04-22 10:33

调用ListScheduledSQLs接口列出定时SQL任务。 前提条件 您已完成以下操作: <

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号