使用 reduce() 构建 JSON。
使用 http.post()
在 JSON 正文中发送数据
使用 reduce() 函数创建一个 JSON 对象,以包含在带有 http.post()
的正文中。
使用
array.from()
查询数据并构建表。或者,使用另一种方法使用 Flux 查询数据。使用
reduce()
函数构建 JSON 对象,然后使用yield()
存储 reduce 的输出。此表如下所示字段 标签 example-field:["3"4"1 {example-tag-key:["bar"bar"bar 使用 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]} 使用
findRecord()
函数从最后一列(JSON)中提取值。使用
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),
)
此页内容是否对您有帮助?
感谢您的反馈!