InfluxDB: InfluxDB http api Query
In the last article on InfluxDB: InfluxDB's InfluxDB http api write operation, we introduced the process of using InfluxDB's http api to write data, this article introduces how to use InfluxDB's http api to query data. For more details about InfluxDB, see the InfluxDB series learning tutorial directory.
InfluxDB technology exchange group: 580487672 (click to join)
I. Description
According to the official documentation, using HTTP APIs for queries is a relatively preliminary method. We recommend that you use third-party language libraries and client management programs for query operations.
Ii. How to query HTTP APIs in InfluxDB
The http api is used to query InfluxDB mainly by sending GET requests to the/query end of InfluxDB. The call example is as follows:
curl -GET 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT value FROM cpu_load_short WHERE region='us-west'"
The parameter db specifies the database to be queried, and q indicates the query statement to be executed.
You can also generate an HTTP request URL on the page, as shown in the following code:
After entering a Statement on the interface, click Generate url to generate the http request URL.
After execution in the browser, a string in json format is returned.
If an error occurs in the query, the keyword "error" + error message is returned.
3. InfluxDB queries multiple data entries through HTTP APIs
We may need to use InfluxDB for multiple queries. The format of multiple queries provided by http api is as follows:
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT value FROM cpu_load_short WHERE region='us-west';SELECT count(value) FROM cpu_load_short WHERE region='us-west'"
The format is the same as that of a single query, but multiple statements must be separated by semicolons.
The returned value is also a json string containing the result.
Iv. Formatting output of InfluxDB HTTP query
1) specified time format
You can useepoch
Parameter specifies the output time format. Optional values include epoch = [h, m, s, MS, u, ns].
For example:
curl -G 'http://localhost:8086/query' --data-urlencode "db=mydb" --data-urlencode "epoch=s" --data-urlencode "q=SELECT value FROM cpu_load_short WHERE region='us-west'"
In this way, the time data in seconds is obtained.
2) Specify the data size for each query
Availablechunk_size
Parameter to specify the size of each result. For example, if I want to return the data of 200 points each time, it is as follows:
curl -G 'http://localhost:8086/query' --data-urlencode "db=mydb" --data-urlencode "chunk_size=200" --data-urlencode "q=SELECT value FROM cpu_load_short WHERE region='us-west'"
In this way, the query results will return data of 200 points.
Now, we will first introduce you to InfluxDB about http api operations. For more information about InfluxDB, see the next article of Linux University.
For more details about InfluxDB, see the InfluxDB series learning tutorial directory.
InfluxDB technology exchange group: 580487672 (click to join)