[Debian] influxdb的查詢 用戶管理 api 刪除等命令
#Step 01 –
influxdb的查詢 用戶管理 api 刪除等命令
influxdb命令使用
1、資料構成
INSERT cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000
第一部分:“cpu_load_short,host=server01,region=us-west”
第一部分稱為key,key中包含了measurement name(類似表)和tags(tags又分為tag key和tag value,tags可以有多個)
注意:在tag value中的空格應以“\”加上空格表示,tags中的值必須是string類型,其實是起到索引的作用
第二部分:“value=0.64,value2=0.86”
第二部分稱為Field,同樣和tags的形式相同,都是鍵值對的形式,但是tags中的值必須是string類型,而Field中的值可以為Integer、float、Boolean、string類型,
若為Integer類型,則值後必須加“i”,否則該值為float類型,
比如value=23意味著這個值23是float類型,
而value=23i,意味著值23是Integer類型。
Boolean類型的值的表示方式有很多,直接寫成:t, T, true, TRUE, f, F, false或 FALSE都可以。
第三部分(可選):“1434055562000000000”
第三部分稱為Timestamp,是時間戳記,如果該部分省略,則預設將當前時間的時間戳記插入資料庫,否則按照使用者輸入的時間戳記插入。
注意:influxdb預設使用UTC時區展示資料
2、創建及使用資料庫
CREATE DATABASE "testDB" --創建資料庫
show databases --展示所有資料庫
use testDB使用 --資料庫
3、增刪改查命令
查詢表資訊
SHOW MEASUREMENTS --查詢當前資料庫中含有的表
SHOW FIELD KEYS --查看當前資料庫所有表的欄位
SHOW series from pay --查看key數據
SHOW TAG KEYS FROM "pay" --查看key中tag key值
SHOW TAG VALUES FROM "pay" WITH KEY = "merId" --查看key中tag 指定key值對應的值
SHOW TAG VALUES FROM cpu WITH KEY IN ("region", "host") WHERE service = 'redis'
DROP SERIES FROM WHERE ='' --刪除key
SHOW CONTINUOUS QUERIES --查看連續執行命令
SHOW QUERIES --查看最後執行命令
KILL QUERY --結束命令
SHOW RETENTION POLICIES ON mydb --查看保留數據
查詢資料
SELECT * FROM /.*/ LIMIT 1 --查詢當前資料庫下所有表的第一行記錄
select * from pay order by time desc limit 2
select * from db_name."POLICIES name".measurement_name --指定查詢資料庫下資料保留中的表資料 POLICIES name資料保留
刪除資料
delete from "query" --刪除表所有資料,則表就不存在了
drop MEASUREMENT "query" --刪除表(注意會把資料保留刪除使用delete不會)
DELETE FROM cpu
DELETE FROM cpu WHERE time < '2000-01-01T00:00:00Z'
DELETE WHERE time < '2000-01-01T00:00:00Z'
DROP DATABASE “testDB” --刪除資料庫
DROP RETENTION POLICY "dbbak" ON mydb --刪除保留資料為dbbak資料
DROP SERIES from pay where tag_key='' --刪除key中的tag
SHOW SHARDS --查看資料存儲檔
DROP SHARD 1
SHOW SHARD GROUPS
SHOW SUBSCRIPTIONS
4、函數使用
mean-平均值
sum-總和
min-最小值
max-最大值
count-總個數
select * from pay order by time desc limit 2
select mean(allTime) from pay where time >= today() group by time(10m) time_zone(+8)
select * from pay time_zone(+8) limit 2
SELECT sum(allTime) FROM "pay" WHERE time > now() - 10s
select count(allTime) from pay where time > now() - 10m group by time(1s)
5、用戶管理命令
複製代碼
SHOW USERS
CREATE USER jdoe WITH PASSWORD '1337password' -- Create a normal database user.
CREATE USER jdoe WITH PASSWORD '1337password' WITH ALL PRIVILEGES -- Create an admin user.
REVOKE ALL PRIVILEGES FROM jdoe revoke admin privileges from jdoe
REVOKE READ ON mydb FROM jdoe -- revoke read privileges from jdoe on mydb
SHOW GRANTS FOR jdoe -- show grants for jdoe
GRANT ALL TO jdoe -- grant admin privileges
GRANT READ ON mydb TO jdoe -- grant read access to a database
DROP USER jdoe
複製代碼
6、資料保留命令
複製代碼
查看保留期 SHOW RETENTION POLICIES ON mydb
修改保留期 ALTER RETENTION POLICY default ON online DEFAULT
刪除保留期 DROP RETENTION POLICY ON
創建保留期 CREATE RETENTION POLICY "rp_name" ON "db_name" DURATION 30d REPLICATION 1 DEFAULT
rp_name:策略名
db_name:具體的資料庫名
30d:保存30天,30天之前的資料將被刪除
它具有各種時間參數,比如:h(小時),w(星期)m minutes h hours d days w weeks INF infinite
REPLICATION 1:副本個數,這裡填1就可以了
DEFAULT 設為默認的策略
複製代碼
7、創建持續性資料處理結果 提供後續查詢
複製代碼
-- selects from default retention policy and writes into 6_months retention policy
CREATE CONTINUOUS QUERY "10m_event_count"
ON db_name
BEGIN
SELECT count(value)
INTO "6_months".events
FROM events
GROUP BY time(10m)
END;
-- this selects from the output of one continuous query in one retention policy and outputs to another series in another retention policy
CREATE CONTINUOUS QUERY "1h_event_count"
ON db_name
BEGIN
SELECT sum(count) as count
INTO "2_years".events
FROM "6_months".events
GROUP BY time(1h)
END;
-- this customizes the resample interval so the interval is queried every 10s and intervals are resampled until 2m after their start time
-- when resample is used, at least one of "EVERY" or "FOR" must be used
CREATE CONTINUOUS QUERY "cpu_mean"
ON db_name
RESAMPLE EVERY 10s FOR 2m
BEGIN
SELECT mean(value)
INTO "cpu_mean"
FROM "cpu"
GROUP BY time(1m)
END;
DROP CONTINUOUS QUERY ON --刪除
SHOW CONTINUOUS QUERIES --查看連續執行命令
複製代碼
8、http api
1. 普通保存
curl -i -X POST 'http://127.0.0.1:8086/write?db=online' --data-binary 'pay,host=1,merId=1234567890,orderFlag=1 allTime=347,ecifTime=39,icqTime=88'
2.Write points from a file by passing @filename to curl.
cpu_data.txt內容如下:
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257
Write the data in cpu_data.txt to the mydb database with:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt
3.單查詢
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'"
4.多查詢
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'"
5.格式化time
epoch=[h,m,s,ms,u,ns]
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'"
#Step 02– influx 清除 資料
> use telegraf
Using database telegraf
>
>
> show series where host='skynet'
> show series where host='Debian10-6-TIG-Temp'
key
---
cpu,cpu=cpu-total,host=skynet
cpu,cpu=cpu0,host=skynet
cpu,cpu=cpu1,host=skynet
cpu,cpu=cpu2,host=skynet
cpu,cpu=cpu3,host=skynet
disk,device=rootfs,fstype=rootfs,host=skynet,path=/
disk,device=sda1,fstype=ext4,host=skynet,path=/
disk,device=sda1,fstype=ext4,host=skynet,path=/var/lib/docker/aufs
diskio,host=skynet,name=sda
diskio,host=skynet,name=sda1
diskio,host=skynet,name=sda2
diskio,host=skynet,name=sda5
kernel,host=skynet
mem,host=skynet
processes,host=skynet
swap,host=skynet
system,host=skynet
>
>
> drop series where host='skynet'
> drop series where host='Debian10-6-TIG-Temp'
>
> show series where host='skynet'
>
>
沒有留言:
張貼留言