使用 reduce() 构建 JSON。
使用 http.post() 发送 JSON 数据
使用 reduce() 函数创建一个 JSON 对象,作为 http.post() 的主体。
导入array包以查询数据并构造表格,以及http package以通过http传输JSON。
使用
array.from()查询数据并构建表格。或者,使用另一种方法 使用 Flux 查询数据。使用
reduce()函数构建一个 JSON 对象,然后使用yield()来存储 reduce 的输出。这个表格看起来像:字段 标签 示例字段:[“3"4"1 {示例标签键:[“bar"bar"bar 使用 map() 函数将两个组件组合成一个 JSON 对象,然后使用第二个
yield()函数将该对象存储为final JSON。该表看起来像:字段 标签 最终 示例字段:[“3"4"1 {示例标签键:[“bar"bar"bar {示例标签键:[“bar"bar"bar] , 示例字段:[“3"4"1]} 使用
findRecord()函数从最后一列提取值,即 JSON。使用
http.post()来指定一个 URL 以发送 JSON。在这个例子中,我们使用 Post Test Server 作为发送 JSON 的 URL,并测试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),
)