文档文档

创建任务

Create tasks with the InfluxDB user interface (UI), influx command line interface (CLI), or /api/v2 API. (使用 InfluxDB 用户界面 (UI)、influx 命令行界面 (CLI) 或 /api/v2 API 创建任务。)

Before creating a task, review the basics for writing a task. (在创建任务之前,请查看编写任务的基础知识。)

Create a task in the InfluxDB UI (在 InfluxDB UI 中创建任务)

The InfluxDB UI provides multiple ways to create a task (InfluxDB UI 提供了多种创建任务的方法)

Create a task from the Data Explorer (从数据浏览器创建任务)

  1. In the navigation menu on the left, select Data Explorer. (在左侧导航菜单中,选择**数据浏览器**。)

  2. Build a query and click Save As in the upper right. (构建查询,然后在右上角单击**另存为**。)

  3. Select the Task heading. (选择 **任务** 标题。)

  4. Specify the task options. See Task options for detailed information about each option. (指定任务选项。有关每个选项的详细信息,请参阅任务选项。)

  5. Click Save as Task. (单击**另存为任务**。)

Create a task in the Task UI (在任务 UI 中创建任务)

  1. In the navigation menu on the left, select Tasks. (在左侧导航菜单中,选择**任务**。)

  2. Click Create Task in the upper right. (单击右上角的**创建任务**。)

  3. In the left panel, specify the task options. See Task options for detailed information about each option. (在左侧面板中,指定任务选项。有关每个选项的详细信息,请参阅任务选项。)

  4. In the right panel, enter your task script. (在右侧面板中,输入您的任务脚本。)

    Leave out the option tasks assignment (忽略 option tasks 的赋值)

    When creating a new task in the InfluxDB Task UI, leave the code editor empty. When you save the task, the Task UI uses the task options you specify in the Task options form to populate option task = {task_options} for you. (在 InfluxDB 任务 UI 中创建*新*任务时,请将代码编辑器留空。保存任务时,任务 UI 使用您在**任务选项**表单中指定的 任务选项 来填充 option task = {task_options}。)

    When you edit the saved task, you’ll see the injected option task = {task_options}. (编辑已保存的任务时,您将看到注入的 option task = {task_options}。)

  5. Click Save in the upper right. (单击右上角的**保存**。)

Import a task (导入任务)

  1. In the navigation menu on the left, select Tasks. (在左侧导航菜单中,选择**任务**。)

  2. Click Create Task in the upper right. (单击右上角的**创建任务**。)

  3. In the left panel, specify the task options. See Task options for detailed information about each option. (在左侧面板中,指定任务选项。有关每个选项的详细信息,请参阅任务选项。)

  4. Paste a raw Flux task in the code editor to the right of the task options fields. (将原始 Flux 任务粘贴到任务选项字段右侧的代码编辑器中。)

  5. Click Save in the upper right. (单击右上角的**保存**。)

Create a task from a template (从模板创建任务)

  1. In the navigation menu on the left, select Settings > Templates. (在左侧导航菜单中,选择**设置** > **模板**。)

  2. Find the template you want to use and click its Resources list to expand the list of resources. (找到要使用的模板,然后单击其**资源**列表以展开资源列表。)

  3. In the Resources list, click the task you want to use. (在**资源**列表中,单击要使用的任务。)

Clone a task (克隆任务)

  1. In the navigation menu on the left, select Tasks. (在左侧导航菜单中,选择**任务**。)

  2. Find the task you would like to clone and click the icon located far right of the task name. (找到要克隆的任务,然后单击位于任务名称最右侧的 **** 图标。)

  3. Click Clone. (单击**克隆**。)

Create a task using the influx CLI (使用 influx CLI 创建任务)

Use the influx task create command to create a new task. It accepts either a file path or raw Flux. (使用 influx task create 命令创建一个新任务。它接受文件路径或原始 Flux。)

Create a task using a file (使用文件创建任务)

# 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

Create a task using raw 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

Create a task using the InfluxDB API (使用 InfluxDB API 创建任务)

Use the /api/v2/tasks InfluxDB API endpoint to create a task. (使用 /api/v2/tasks InfluxDB API 端点创建任务。)

POST https://127.0.0.1:8086/api/v2/tasks/

Provide the following in your API request (在您的 API 请求中提供以下内容)

Request headers (请求头)
  • Content-Type: application/json
  • Authorization: Token INFLUX_API_TOKEN
Request body (请求体)

JSON object with the following fields (具有以下字段的 JSON 对象)

  • flux: raw Flux task string that contains a task option and a query. (flux:原始 Flux 任务字符串,包含 task 选项和查询。)
  • orgID: your InfluxDB organization ID (orgID:您的 InfluxDB 组织 ID)
  • status: task status (“active” or “inactive”) (status:任务状态(“active”或“inactive”))
  • description: task description (description:任务描述)
curl --request POST 'https://127.0.0.1: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"
}'

Was this page helpful? (此页面对您有帮助吗?)

Thank you for your feedback! (感谢您的反馈!)


Flux 的未来

Flux 将进入维护模式。 您可以继续像现在一样使用它,无需对代码进行任何更改。

了解更多

InfluxDB 3 开源版本现已推出公开 Alpha 版

InfluxDB 3 开源版本现已提供 alpha 测试版,并获得 MIT 或 Apache 2 许可。

我们正在发布两个产品作为 alpha 版本的一部分。

InfluxDB 3 Core 是我们新的开源产品。 它是用于时间序列和事件数据的最新数据引擎。 InfluxDB 3 Enterprise 是一个商业版本,它建立在 Core 的基础上,增加了历史查询功能、读取副本、高可用性、可扩展性和细粒度的安全性。

有关如何入门的更多信息,请查看