influxdb常用操作

插入数据
insert test,host=127.0.0.1 ,monitor_name=test count=1

删除表
drop measurement "measurement"

命令行插入数据
use testDb
insert test,host=127.0.0.1, monitor_name=test count=1

通过http接口
curl -i -XPOST 'http://127.0.0.1:8086/write?db=testdb' --data-binary 'test,host=127.0.0.1,monitor_name=test count=1'

test  表名
host=127.0.0.1,monitor_name=test  tag
count=1 field
查询数据库中的数据
select * from test order by time desc
通过http接口
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testDb" --data-urlencode "q=select * from test order by desc"

数据保存策略
influxdb数据保存策略
influxdb 是没有直接删除数据记录的方法,但是提供数据保存策略,主要用于指定数据保留时间,超过指定时间,就删除这部分数据
查看当前数据库的数据保存策略
show retention policies on "test"

创建新的数据保存策略
create retention policy "rp_name" on "db_nme" duration 3w replication 1 default
policy_default
create retention policy "policy_default" on "tg_agency" duration 24h replication 1 default
rp_name  策略名
db_nme  具体的数据库名
3w 保存3周 三周之前的数据将被删除,influxdb具有各种事件参数,比如 h(小时)
d(天),w(周)
replication  副本个数,一般为1就可以了
default 设置为默认策略

修改数据保存策略
alter replication policy "rp_name" on "db_name" duration 20d default

删除数据保存策略
drop retention policy "rp_name"

连续查询 Continous Queries
当数据超过保存策略里指定的时间之后就会被删除掉,但是这时候可能并不想数据被完全删掉,怎么办
influxdb 提供了联系查询,可以做数据统计采样
查看数据库的Continous Queries (翻译过来就是连续查询的意思)
show continuous queries

创建新的Continous Queries
create continous query cq_name on db_name begin select sum(count) into 
new_table_name from table_name group by time(30m) end

cq_name 连续查询名字
db_name 数据库名字
sum(count) 计算总和
table_name 当前表名
new_table_name 存新的数据的表名
30M 时间间隔为30分钟

删除Continous Queries
drop continous query cp_name on db_name

用户管理
可以直接在web管理页面做操作,也可以在命令行
#显示用户#
show users
#创建用户#
create user "username" with password "password"
#创建管理员权限用户#
create user "user_name" with password "password" with all privileges
#删除用户#
drop user "username"

[
  {
    "name": "events",
    "columns": ["state", "email", "type"],
    "points": [
      ["ny", "[email protected]", "follow"],
      ["ny", "[email protected]", "open"]
    ]
  },
  {
    "name": "errors",
    "columns": ["class", "file", "user", "severity"],
    "points": [
      ["DivideByZero", "example.py", "[email protected]", "fatal"]
    ]
  }
]
events就是一个series 类似关系型数据库里的table
格式是json 可以在一个POST请求发送多个series ,每个series里面
的points可以是很多个,但是索引要和columns对应(columns相当于一条记录的列名)

上面的数据没有包含time列(columns),influxdb会自己加上,不过也可以指定time,比如
[
 {
    "name":"response_times",
    "columns": ["time", "value"],
    "points" :[
        [138874382748,234.5],
        [138838737776,120.1],
        [137678766876,340.9]
    ]
 }
]
time在inluxdb里面是很重要的,比较influxdb是time series database
在influxdb 还有个sequence_number 字段是数据库维护的,类似MySQL的主键概念

influxdb 增删改查都是通过http API来完成,甚至支持正则表达式删除数据,还有计划任务(定时任务)
比如
发送POST请求到 /db/:name/scheduled_deletes ,body如下
{
    "regex": "stats\..*",
    "olderThan": "14d",
    "runAt": 3
}
这个查询会删除大于14天的数据,并且任何以stats开头的数据,并且每天3:00AM运行

查询语言
influxdb 提供了类似SQL的查询语言
举例:
select * from events where state == 'NY';
select * from log_lines where line =~ /error/i;  //正则
select * from events where customer_id == 23 and type == 'click'
select * from response_times where value > 500;
select * from events where email !~ /.*gmail.*/;
select * from nagios_checks where status != 0;
select * from events
where (email =~ /.*gmail.*/ or email =~ /.*yahoo.*/) and state == 'ny';
delete from response_times where time > now() - 1h

select value from runtime.memory.allocated where time > now()-10m;

curl -POST http://39.107.77.94:8086/query --data-urlencode "q=CREATE DATABASE tg_agency"

猜你喜欢

转载自blog.csdn.net/u010129985/article/details/82424806
今日推荐