创建任务
通过 InfluxDB 用户界面 (UI)、influx 命令行界面 (CLI) 或 /api/v2 API 创建任务。
在创建任务之前,请查看编写任务的基础知识。
在InfluxDB UI中创建任务
InfluxDB 用户界面提供多种创建任务的方法:
从数据探索器创建任务
在左侧的导航菜单中,选择 数据浏览器。
构建一个查询并点击右上角的 另存为。
选择任务标题。
指定任务选项。有关每个选项的详细信息,请参见任务选项。
点击 另存为任务。
在任务用户界面中创建任务
在左侧导航菜单中,选择 任务。
点击 创建任务 在右上角。
在左侧面板中,指定任务选项。 查看 任务选项 以获取有关每个选项的详细信息。
在右侧面板中,输入你的任务脚本。
省略选项任务分配
在InfluxDB任务用户界面创建一个新任务时,请保持代码编辑器为空。 当您保存任务时,任务用户界面会使用您在任务选项表单中指定的任务选项为您填充
option task = {task_options}。当您编辑已保存的任务时,您将看到注入的
option task = {task_options}。点击保存在右上角。
导入任务
在左侧导航菜单中,选择 任务。
点击 创建任务 在右上角。
在左侧面板中,指定任务选项。 查看 任务选项 以获取每个选项的详细信息。
将原始 Flux 任务粘贴到任务选项字段右侧的代码编辑器中。
点击 保存 在右上角。
从模板创建任务
在左侧的导航菜单中,选择 设置 > 模板。
找到您想使用的模板,然后点击其 Resources 列表以展开资源列表。
在资源列表中,单击您想要使用的任务。
克隆一个任务
在左侧导航菜单中,选择 任务。
找到您想要克隆的任务,然后点击任务名称最右侧的 图标。
点击 克隆。
使用 influx CLI 创建任务
使用 influx task create 命令创建一个新的任务。
它接受文件路径或原始 Flux。
使用文件创建任务
# Syntax
influx task create --org <org-name> -f </path/to/task-script>
# Example
influx task create --org my-org -f /tasks/cq-mean-1h.flux
使用原始Flux创建任务
influx task create --org my-org - # <return> to open stdin pipe
option task = {
name: "task-name",
every: 6h
}
# ... Task script ...
# Linux & macOS: <ctrl-d> to close the pipe and submit the command
# Windows: <enter>, then <ctrl-d>, then <enter> to close the pipe and submit the command
使用InfluxDB API创建任务
使用/api/v2/tasks InfluxDB API 端点来创建任务。
POST http://localhost:8086/api/v2/tasks/
在您的API请求中提供以下内容:
请求头
- 内容类型: application/json
- 授权: 令牌
INFLUX_API_TOKEN
请求体
具有以下字段的JSON对象:
- flux: 原始 Flux 任务字符串,包含一个
task选项 和一个查询。 - orgID: 你的 InfluxDB 组织 ID
- 状态: 任务状态(“活动”或“非活动”)
- 描述: 任务描述
curl --request POST 'http://localhost:8086/api/v2/tasks' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token INFLUX_API_TOKEN' \
--data-raw '{
"flux": "option task = {name: \"CPU Total 1 Hour New\", every: 1h}\n\nfrom(bucket: \"telegraf\")\n\t|> range(start: -1h)\n\t|> filter(fn: (r) =>\n\t\t(r._measurement == \"cpu\"))\n\t|> filter(fn: (r) =>\n\t\t(r._field == \"usage_system\"))\n\t|> filter(fn: (r) =>\n\t\t(r.cpu == \"cpu-total\"))\n\t|> aggregateWindow(every: 1h, fn: max)\n\t|> to(bucket: \"cpu_usage_user_total_1h\", org: \"INFLUX_ORG\")",
"orgID": "INFLUX_ORG_ID",
"status": "active",
"description": "This task downsamples CPU data every hour"
}'