I want to post data of one influxdb to another using http.post, but its not working. i am using Task to do it.Here is my code below. I could not use To function because of data diode limitation. Please help
import “json”
import “http”
import “array”
option task = {
name: “TTEST”,
every: 10s,
}
lastReported = from(bucket: “RTR”)
|> range(start: -1m)
|> filter(fn: (r) => r._measurement == “cpu”)
|> yield(name: “last”)
|> findColumn(fn: (key) => true, column: “_value”)
http.post(
url: “http://localhost:8086/api/v2/write/?orgID=73b2e92f943063f3&bucket=ANK&precision=ns”,
headers: {
Authorization: “Bearer Lx0FOdmHOxkOM_wMxpMPvCiJXVsUdhv3DbHqzS6CSUT9yTUmoUEl0sfoJMnykFAafjOAtLNg-wV-tln7gk2LPA==”,
“Content-type”: “application/json”,
},
data: json.encode(v: lastReported),
)
The write
endpoint requires data in the line protocol format, not JSON.
1 Like
Thanks Vlasta…now its working
import “http”
option task = {
name: “TTEST”,
every: 1m,
}
lastReported = from(bucket: “RTR”)
|> range(start: -1m)
|> filter(fn: (r) => r[“_measurement”] == “cpu”)
|> filter(fn: (r) => r[“_field”] == “usage_user”)
|> filter(fn: (r) => r[“cpu”] == “cpu-total”)
|> yield(name: “last”)
|> findColumn(fn: (key) => true, column: “_value”)
http.post(
url: “http://localhost:8086/api/v2/write/?org=XXXX&bucket=A5&precision=ns”,
headers: {
Authorization: “Token Lx0FOdmHOxkOM_wMxpMPvCiJXVsUdhv3DbHqzS6CSUT9yTUmoUEl0sfoJMnykFAafjOAtLNg-wV-tln7gk2LPA==”,
“Content-type”: “text/plain”,
},
data: bytes(v: “mem,host=host1 used_percent=” + string(v: lastReported[0])),
)
Hello @aceankit99,
I believe that http.post() you can only send one value at a time:
You’d have to do something like:
lastReported[0]
Or you’d have to use the reduce function to create a json object. Something like:
import "json"
import "http"
import "array"
lastReported = from(bucket: "engineering's Bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "CBBandwidthAlert")
|> filter(fn: (r) => r["_field"] == "code")
|> group()
|> map(fn: (r) => ({ r with index: 1 }))
|> cumulativeSum(columns: ["index"])
|> map(fn: (r) => ({ r with gindex: r.index / 250 }))
|> group(columns: ["gindex"])
|> reduce(
fn: (r, accumulator) => ({ _value:accumulator._value + "\"" + r.host + "\","}),
identity: {_value: "{hosts:["}
)
|> map(fn: (r) => ({ r with _value: r._value + "]}" }))
|> yield()
1 Like
@aceankit99
Of course! Out of curiosity what are you doing with InfluxDB? I’m curious to learn about what community is doing.
I am working on an IOT solution where we have to pass data from one influx instance to another influx instance through OPDS 1000
@aceankit99 so cool! What type of IoT solution?
mainly monitoring of edge devices .