文档文档

JSON v2 输入数据格式

使用 json_v2 输入数据格式将 JSON 对象或对象数组解析为 Telegraf 指标。

该解析器支持 GJSON 路径语法 以查询 JSON。

要测试您的 GJSON 路径,请使用 GJSON Playground

您可以在 Telegraf 仓库的 此处 找到多个示例。

配置

通过描述您想要的指标来配置此解析器,方法是从输入中定义字段和标签。配置分为名为 fieldtagobject 的配置子表。在下面的示例中,您可以看到可以为每个配置表定义的所有可能的配置键。在接下来的章节中,将更详细地定义这些配置键。

 [[inputs.file]]
    urls = []
    data_format = "json_v2"

    [[inputs.file.json_v2]]
        measurement_name = "" # A string that will become the new measurement name
        measurement_name_path = "" # A string with valid GJSON path syntax, will override measurement_name
        timestamp_path = "" # A string with valid GJSON path syntax to a valid timestamp (single value)
        timestamp_format = "" # A string with a valid timestamp format (see below for possible values)
        timestamp_timezone = "" # A string with with a valid timezone (see below for possible values)

        [[inputs.file.json_v2.field]]
            path = "" # A string with valid GJSON path syntax
            rename = "new name" # A string with a new name for the tag key
            type = "int" # A string specifying the type (int,uint,float,string,bool)
            optional = false # true: suppress errors if configured path does not exist

        [[inputs.file.json_v2.tag]]
            path = "" # A string with valid GJSON path syntax
            rename = "new name" # A string with a new name for the tag key
            type = "float" # A string specifying the type (int,uint,float,string,bool)
            optional = false # true: suppress errors if configured path does not exist

        [[inputs.file.json_v2.object]]
            path = "" # A string with valid GJSON path syntax
            timestamp_key = "" # A JSON key (for a nested key, prepend the parent keys with underscores) to a valid timestamp
            timestamp_format = "" # A string with a valid timestamp format (see below for possible values)
            timestamp_timezone = "" # A string with with a valid timezone (see below for possible values)
            disable_prepend_keys = false (or true, just not both)
            included_keys = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) that should be only included in result
            excluded_keys = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) that shouldn't be included in result
            tags = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) to be a tag instead of a field
            optional = false # true: suppress errors if configured path does not exist
            [inputs.file.json_v2.object.renames] # A map of JSON keys (for a nested key, prepend the parent keys with underscores) with a new name for the tag key
                key = "new name"
            [inputs.file.json_v2.object.fields] # A map of JSON keys (for a nested key, prepend the parent keys with underscores) with a type (int,uint,float,string,bool)
                key = "int"

根配置选项

  • measurement_name (可选):将测量名称设置为提供的字符串。
  • measurement_name_path (可选):您可以使用 GJSON 路径语法 定义查询,以从 JSON 输入设置测量名称。查询必须返回单个数据值,否则将使用默认测量名称。这优先于 measurement_name
  • timestamp_path (可选):您可以使用 GJSON 路径语法 定义查询,以从 JSON 输入设置时间戳。查询必须返回单个数据值,否则将默认为当前时间。
  • timestamp_format (可选,但当定义 timestamp_path 时为必需):必须设置为 unixunix_msunix_usunix_ns 或 Go “参考时间”,其定义为特定时间:Mon Jan 2 15:04:05 MST 2006
  • timestamp_timezone (可选,但需要 timestamp_path):此选项应设置为 Unix TZ 值,例如 America/New_YorkLocal 以使用系统时区,或 UTC。默认为 UTC

数组和对象

以下描述了解析数组和对象时的高级方法

  • 数组:数组中的每个元素都被视为单独的指标
  • 对象:对象中的每个键值对都被视为单个指标

当处理嵌套数组和对象时,上述规则在解析器创建指标时继续适用。当一个对象有多个数组作为值时,这些数组将成为单独的指标,仅包含对象中的非数组值。在下面,您可以看到此行为的示例,其中输入 JSON 包含一个书籍对象数组,该数组具有字符的嵌套数组。

JSON 示例

{
    "book": {
        "title": "The Lord Of The Rings",
        "chapters": [
            "A Long-expected Party",
            "The Shadow of the Past"
        ],
        "author": "Tolkien",
        "characters": [
            {
                "name": "Bilbo",
                "species": "hobbit"
            },
            {
                "name": "Frodo",
                "species": "hobbit"
            }
        ],
        "random": [
            1,
            2
        ]
    }
}

配置示例

[[inputs.file]]
    files = ["./testdata/multiple_arrays_in_object/input.json"]
    data_format = "json_v2"
    [[inputs.file.json_v2]]
        [[inputs.file.json_v2.object]]
            path = "book"
            tags = ["title"]
            disable_prepend_keys = true

预期指标

file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",chapters="A Long-expected Party"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",chapters="The Shadow of the Past"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",name="Bilbo",species="hobbit"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",name="Frodo",species="hobbit"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",random=1
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",random=2

您可以在 telegraf 仓库的 [testdata][] 文件夹下找到更复杂的示例。

类型

对于每个字段,您可以选择为每个指标定义类型。以下规则适用于此配置

  • 如果显式定义了类型,则解析器将强制执行此类型,并在可能的情况下将数据转换为定义的类型。如果类型无法转换,则解析器将失败。
  • 如果未定义类型,则解析器将使用 JSON 中定义的默认类型(int、float、string)。

您可以设置的类型值

  • int、bool、floats 或字符串(带有有效数字)可以转换为 int。
  • uint、bool、floats 或字符串(带有有效数字)可以转换为 uint。
  • string,任何数据都可以格式化为字符串。
  • float,字符串值(带有有效数字)或整数可以转换为 float。
  • bool,字符串值“true”或“false”(不区分大小写)或整数值 01 可以转换为 bool。

此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

InfluxDB 3 开源版现已公开发布 Alpha 版本

InfluxDB 3 开源版现已可用于 Alpha 测试,根据 MIT 或 Apache 2 许可授权。

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

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

有关如何开始使用的更多信息,请查看