參考官方開源文檔
使用HTTP API查詢數據https://docs.influxdata.com/influxdb/v1.7/guides/querying_data/數據庫
使用HTTP的API查詢數據
HTTP API是在InfluxDB中查詢數據的主要方法(有關查詢數據庫的其餘方法,請參閱命令行界面和客戶端庫)。數組
注意:如下示例使用
curl
命令行工具,該工具使用URL傳輸數據。學習的基礎知識curl
與HTTP腳本指南。bash
API查詢語句
查詢語句以下:curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"
微信
在前面的篇章中,我已經建立了testdb
數據庫,以及插入了數據。 首先查看一下當前InfluxDB中的數據,以下:網絡
> show databases name: databases name ---- _internal mydb testdb > > use testdb Using database testdb > > show measurements name: measurements name ---- cpu_load_short tobeornottobe > > select * from cpu_load_short name: cpu_load_short time direction host region value ---- --------- ---- ------ ----- 1422568543702900257 in server01 us-west 2 1422568543702900257 server02 us-west 0.55 1434055562000000000 server01 us-west 0.64 1546849598178339889 server02 0.67 1546850175491084332 server02 0.67 1546850460880063366 server02 0.67 > > select * from cpu_load_short where region = 'us-west' name: cpu_load_short time direction host region value ---- --------- ---- ------ ----- 1422568543702900257 in server01 us-west 2 1422568543702900257 server02 us-west 0.55 1434055562000000000 server01 us-west 0.64 >
下面使用API請求查詢以下:app
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ "2015-01-29T21:55:43.702900257Z", 2 ], [ "2015-01-29T21:55:43.702900257Z", 0.55 ], [ "2015-06-11T20:46:02Z", 0.64 ] ] } ] } ] } [root@server81 ~]#
能夠從上面看出,能夠正確查詢出條件爲region = 'us-west'
的三條數據。curl
InfluxDB返回數據的格式是JSON格式。查詢結果顯示在"results"數組中。若是發生錯誤,InfluxDB會設置一個"error"帶有錯誤解釋。ide
例如查詢region = 'us-south'
這個在數據中是沒有的。下面執行看看:工具
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-south'" { "results": [ { "statement_id": 0 } ] } [root@server81 ~]#
注意:附加pretty=true
到URL能夠啓用漂亮的JSON輸出。雖然這對於調試或直接使用相似工具查詢頗有用curl,但不建議將其用於生產,由於它會消耗沒必要要的網絡帶寬。學習
若是沒有pretty=true
,那麼執行會是怎麼樣的結果呢?執行以下:
[root@server81 ~]# curl -G 'http://localhost:8086/query' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'" {"results":[{"statement_id":0,"series":[{"name":"cpu_load_short","columns":["time","value"],"values":[["2015-01-29T21:55:43.702900257Z",2],["2015-01-29T21:55:43.702900257Z",0.55],["2015-06-11T20:46:02Z",0.64]]}]}]} [root@server81 ~]#
API進行多個查詢語句
在單個API調用中向InfluxDB發送多個查詢。只需使用分號分隔每一個查詢,例如:curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west';SELECT count(\"value\") FROM \"cpu_load_short\" WHERE \"region\"='us-west'"
執行請求以下:
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west';SELECT count(\"value\") FROM \"cpu_load_short\" WHERE \"region\"='us-west'" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ "2015-01-29T21:55:43.702900257Z", 2 ], [ "2015-01-29T21:55:43.702900257Z", 0.55 ], [ "2015-06-11T20:46:02Z", 0.64 ] ] } ] }, { "statement_id": 1, "series": [ { "name": "cpu_load_short", "columns": [ "time", "count" ], "values": [ [ "1970-01-01T00:00:00Z", 3 ] ] } ] } ] } [root@server81 ~]#
能夠從上面的返回結果看出,兩個查詢語句的結果都會合併到result數組中返回。
查詢數據的其餘選項
設置時間戳格式
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"
下面對比查看兩個請求,一個有寫--data-urlencode "epoch=s"
條件,第二個沒有該條件,分析一下返回結果中的時間格式。
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1422568543, 2 ], [ 1422568543, 0.55 ], [ 1434055562, 0.64 ] ] } ] } ] } [root@server81 ~]# [root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ "2015-01-29T21:55:43.702900257Z", 2 ], [ "2015-01-29T21:55:43.702900257Z", 0.55 ], [ "2015-06-11T20:46:02Z", 0.64 ] ] } ] } ] } [root@server81 ~]#
能夠從上面的格式中看出,第一個查詢出來的時間戳爲1422568543
,第二個查詢出來的時間戳是2015-01-29T21:55:43.702900257Z
。
關於查詢是2015-01-29T21:55:43.702900257Z
的解釋:InfluxDB中的全部內容都以UTC格式存儲和報告。默認狀況下,時間戳以RFC3339 UTC返回,並具備納秒級精度。
關於查詢是1422568543
的解釋:設置epoch參數,則能夠設置返回的時間戳格式,格式爲epoch=[h,m,s,ms,u,ns]
。 例如,設置秒級的時間戳則是epoch=s
。
那麼若是查詢毫秒(ms
)級別的呢?下面來查詢看看:curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=ms" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=ms" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1422568543702, 2 ], [ 1422568543702, 0.55 ], [ 1434055562000, 0.64 ] ] } ] } ] } [root@server81 ~]#
秒級的時間戳:1422568543 毫秒級的時間戳:1422568543702 能夠看出精度愈來愈高,多了最後的702三位。
最大行限制
該max-row-limit
配置選項容許用戶限制返回結果的最大數量,以防止InfluxDB運行內存溢出。默認狀況下,max-row-limit
配置選項設置爲0
。該默認設置容許每一個請求返回無限數量的行。
最大行限制僅適用於非分塊查詢。分塊查詢能夠返回無限數量的點。
分塊
經過設置chunked=true
查詢字符串參數,可使用分塊返回結果。
下面使用實操來演示一下分塊的返回效果,首先查詢一下數據以下:curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=ms" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\""
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=ms" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\"" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1422568543702, 2 ], [ 1422568543702, 0.55 ], [ 1434055562000, 0.64 ], [ 1546849598178, 0.67 ], [ 1546850175491, 0.67 ], [ 1546850460880, 0.67 ] ] } ] } ] } [root@server81 ~]#
能夠從返回的結果看出,全部查詢的結果都是寫入一個result數組裏面的。可否分紅多個result數組返回結果呢?
下面來設置分塊參數執行語句以下: 設置參數--data-urlencode "chunked=true" --data-urlencode "chunk_size=1"
一條數據爲一個返回result數組。 總體語句以下: curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=ms" --data-urlencode "chunked=true" --data-urlencode "chunk_size=1" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\""
[root@server81 ~]# curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testdb" --data-urlencode "epoch=ms" --data-urlencode "chunked=true" --data-urlencode "chunk_size=1" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\"" { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1422568543702, 2 ] ], "partial": true } ], "partial": true } ] } { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1422568543702, 0.55 ] ], "partial": true } ], "partial": true } ] } { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1434055562000, 0.64 ] ], "partial": true } ], "partial": true } ] } { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1546849598178, 0.67 ] ], "partial": true } ], "partial": true } ] } { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1546850175491, 0.67 ] ], "partial": true } ], "partial": true } ] } { "results": [ { "statement_id": 0, "series": [ { "name": "cpu_load_short", "columns": [ "time", "value" ], "values": [ [ 1546850460880, 0.67 ] ] } ] } ] } [root@server81 ~]#
好了,從上面能夠看出,已經將每一條查詢結果都分塊到各自的result數組之中了。
本文分享自微信公衆號 - DevOps社羣(DevOpsCommunity)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。