Get one (last) value from infuxdb

I am trying to get the last value inserted into a table in infuxdb. What I need to do is send it to another system via HTTP.

I would like to do all this in a bash script, but I am also open to Python.

 $ curl -sG 'https://influx.server:8086/query' --data-urlencode "db=iotaWatt" --data-urlencode "q=SELECT LAST(\"value\") FROM \"grid\" ORDER BY time DESC" | jq -r

{
  "results": [
    {
      "statement_id": 0,
      "series": [
        {
          "name": "grid",
          "columns": [
            "time",
            "last"
          ],
          "values": [
            [
              "2018-01-17T04:15:30Z",
              690.1
            ]
          ]
        }
      ]
    }
  ]
}

What I'm struggling with is turning this value into a clean format that I can use. I really don't want to use sed, and I tried jq, but it complains that the data is a string, not an index:

jq: error (at <stdin>:1): Cannot index array with string "series"

Anyone have a good suggestion?

+4
source share
2 answers

Pipe that twists to jqbelow

$ your_curl_stuff_here | jq '.results[].series[]|.name,.values[0][]'
"grid"
"2018-01-17T04:15:30Z"
690.1

Results can be stored in a bash array and used later.

$ results=( $(your_curl_stuff_here | jq '.results[].series[]|.name,.values[0][]') )
$ echo "${results[@]}"
"grid" "2018-01-17T04:15:30Z" 690.1
# Individual values could be accessed using "${results[0]}" and so, mind quotes

All good :-)

+1
source

Given the JSON shown, the jq request is:

.results[].series[].values[]

gives:

[
  "2018-01-17T04:15:30Z",
  690.1
]

, , , , infuxdb, , , :

.results[-1].series[-1].values[-1]

, .

, [] .

+1

Source: https://habr.com/ru/post/1692290/


All Articles