文档文档

使用 reduce() 构建 JSON。

使用 http.post() 在 JSON 正文中发送数据

使用 reduce() 函数创建一个 JSON 对象,以包含在带有 http.post() 的正文中。

  1. 导入 array 包以查询数据和构建表,以及 http 包 以通过 http 传输 JSON。

  2. 使用 array.from() 查询数据并构建表。或者,使用另一种方法使用 Flux 查询数据

  3. 使用 reduce() 函数构建 JSON 对象,然后使用 yield() 存储 reduce 的输出。此表如下所示

    字段标签
    example-field:["3"4"1{example-tag-key:["bar"bar"bar
  4. 使用 map() 函数将两个组件组合在一起成为一个 JSON 对象,然后使用第二个 yield() 函数将此对象存储为 final JSON。此表如下所示

    字段标签最终
    example-field:["3"4"1{example-tag-key:["bar"bar"bar{example-tag-key:["bar"bar"bar] , example-field:["3"4"1]}
  5. 使用 findRecord() 函数从最后一列(JSON)中提取值。

  6. 使用 http.post() 指定要将 JSON 发送到的 URL。在此示例中,我们使用 Post Test Server 作为 URL 发送 JSON,并测试 http.post() 函数。

import "array"
import "http"

 
data = array.from(
        rows: [
            {_time: 2020-01-01T00:00:00Z, _field: "example-field", _value: 3, foo: "bar"},
            {_time: 2020-01-01T00:01:00Z, _field: "example-field", _value: 4, foo: "bar"},
            {_time: 2020-01-01T00:02:00Z, _field: "example-field", _value: 1, foo: "bar"},
        ],
    )
  
    |> reduce(
            fn: (r, accumulator) => ({tag:accumulator.tag + "\"" + r.foo, 
                                    field : accumulator.field + "\"" + string(v:r._value)
                                    }),
            identity: {tag: "{example-tag-key:[", 
                    field: "example-field:[" }
    )
    |> yield(name: "output of reduce")
    |> map(fn: (r) => ({ r with tag: r.tag + "]" }))
    |> map(fn: (r) => ({ r with field: r.field + "]}" }))
    |> map(fn: (r) => ({ r with final: r.tag + " , " + r.field}))
    |> yield(name: "final JSON")
    |> findRecord(
        fn: (key) => true,
        idx: 0,
        )


http.post(
    url: "https://ptsv2.com/t/c4x38-1656014222/post",
    headers: {"Content-type": "application/json"},
    data: bytes(v: data.final),
    )

此页内容是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

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

InfluxDB 3 开源现在可用于 alpha 测试,根据 MIT 或 Apache 2 许可获得许可。

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

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

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