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 <measurement_name[,measurement_name]> WHERE <tag_key>='<tag_value>' --删除key
SHOW CONTINUOUS QUERIES   --查看连续执行命令
SHOW QUERIES  --查看最后执行命令
KILL QUERY <qid> --结束命令
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 <retentionpolicy> ON <database>
创建保留期 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 <cq_name> ON <database_name>  --删除
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'"

 

influxdb 的定时聚合

 

 

 

 

posted @ 2016-10-31 23:38  众里寻,阑珊处  阅读(1585)  评论(0编辑  收藏  举报
返回顶部