influxdb基本SQL操作1

数据库操作

  • 显示已存在的所有数据库

格式: show databases
示例如下:

  1.  
    > show databases;
  2.  
    name: databases
  3.  
    name
  4.  
    ----
  5.  
    _internal
  • 创建新数据库

格式:

  create database <dbname>    

说明:
dbname : 数据库名称
示例如下:

  1.  
    > create database testdb;
  2.  
    > show databases;
  3.  
    name: databases
  4.  
    name
  5.  
    ----
  6.  
    _internal
  7.  
    testdb
  8.  
    >
  • 删除数据库

格式:

drop database <dbname>       

说明:
dbname : 数据库名称
示例如下:

  1.  
    > drop database testdb;
  2.  
    > show databases;
  3.  
    name: databases
  4.  
    name
  5.  
    ----
  6.  
    _internal
  7.  
     
  8.  
    >

表操作

  • 显示指定数据库中已存在的表

格式: show measurements
示例如下:

  1.  
    > use testdb;
  2.  
    Using database testdb
  3.  
    > show measurements;
  • 创建新表并添加数据

InfluxDB没有提供单独的建表语句,可以通过以下方式创建数据库并添加数据。

格式:

insert <tbname>,<tags> <values> [timestamp]    

说明:
tbname : 数据表名称
tags : 表的tag域
values : 表的value域
timestamp :当前数据的时间戳(可选,没有提供的话系统会自带添加)

示例如下:

  1.  
    > use testdb;
  2.  
    Using database testdb
  3.  
    > insert students,stuid=s123 score= 89
  4.  
    > show measurements;
  5.  
    name: measurements
  6.  
    name
  7.  
    ----
  8.  
    students
  • 删除表

格式:

drop measurement  <tbname>    

说明:
tbname : 数据表名称

示例如下:

  1.  
    > use testdb;
  2.  
    Using database testdb
  3.  
    > drop measurement students;
  4.  
    > show measurements;
  5.  
    >

数据操作

  • 添加

格式:

  insert <tbname>,<tags> <values> [timestamp]     

说明:
tbname : 数据表名称
tags : 表的tag域
values : 表的value域
timestamp :当前数据的时间戳(可选,没有提供的话系统会自带添加)

示例如下:

  1.  
    > insert students,stuid=s123 score= 79
  2.  
    > insert students,stuid=s123 score= 89 1488821368327436809
  3.  
    > select * from students
  4.  
    name: students
  5.  
    time score stuid
  6.  
    ---- ----- -----
  7.  
    1488821368327436809 89 s123
  8.  
    1488821404414227498 79 s123
  • 查询

格式:

  1.  
    select <fields> from <tbname> [ into_clause ] [ where_clause ]
  2.  
    [ group_by_clause ] [ order_by_clause ] [ limit_clause ]
  3.  
    [ offset_clause ] [ slimit_clause ] [ soffset_clause ]

说明:
fields : 要查询的字段,查询全部可以用*
tbname : 数据表名称
into_clause : select ... into (可选)
where_clause : where条件域(可选)
group_by_clause : group by相关(可选)
order_by_clause : order by相关(可选)
limit_clause : limit相关(可选)
offset_clause : offset相关(可选)
slimit_clause : slimit相关(可选)
soffset_clause : soffset相关(可选)

示例如下:

  1.  
    > use testdb;
  2.  
    Using database testdb
  3.  
    > show measurements;
  4.  
    name: measurements
  5.  
    name
  6.  
    ----
  7.  
    students
  8.  
     
  9.  
    > select * from students
  10.  
    name: students
  11.  
    time score stuid
  12.  
    ---- ----- -----
  13.  
    1488821368327436809 89 s123
  14.  
    1488821404414227498 79 s123
  15.  
    1488822192864587535 69 s123
  16.  
    1488822196951305763 39 s123
  17.  
     
  18.  
    > select * from students where score > 70;
  19.  
    name: students
  20.  
    time score stuid
  21.  
    ---- ----- -----
  22.  
    1488821368327436809 89 s123
  23.  
    1488821404414227498 79 s123
  24.  
     
  25.  
    > select * from students where score > 70 limit 1;
  26.  
    name: students
  27.  
    time score stuid
  28.  
    ---- ----- -----
  29.  
    1488821368327436809 89 s123
  30.  
     
  31.  
    >
  • 更新

tags 和 timestamp相同时数据会执行覆盖操作,相当于InfluxDB的更新操作。

示例如下:

  1.  
    > insert students,stuid=s123 score= 39
  2.  
    > select * from students
  3.  
    name: students
  4.  
    time score stuid
  5.  
    ---- ----- -----
  6.  
    1488822338410283027 39 s123
  7.  
     
  8.  
    > insert students,stuid=s123 score= 99 1488822338410283027
  9.  
    > select * from students
  10.  
    name: students
  11.  
    time score stuid
  12.  
    ---- ----- -----
  13.  
    1488822338410283027 99 s123
  14.  
     
  15.  
    >
  • 删除

格式:

delete from <tbname> [where_clause]     

说明:
tbname : 表名称
where_clause : where条件(可选)

删除所有数据:

  1.  
    > delete from students;
  2.  
    > select * from students;
  3.  
    >

删除指定条件的数据:

  1.  
    > select * from students;
  2.  
    name: students
  3.  
    time score stuid
  4.  
    ---- ----- -----
  5.  
    1488820352594964019 89 s123
  6.  
    1488820356463338534 79 s123
  7.  
     
  8.  
     
  9.  
    > delete from students where stuid='s123' and time=1488820352594964019;
  10.  
    > select * from students;
  11.  
    name: students
  12.  
    time score stuid
  13.  
    ---- ----- -----
  14.  
    1488820356463338534 79 s123
  15.  
     
  16.  
    >

其它

  • 控制台执行单次查询

格式:

influx -execute '<query>'

类似 mysql -e 的功能,示例代码如下:

  1.  
    [root@localhost ~] # influx -execute 'show databases'
  2.  
    name: databases
  3.  
    name
  4.  
    ----
  5.  
    _internal
  6.  
    testdb
  7.  
     
  8.  
    [root@localhost ~] #
  • 指定查询结果以csv或json格式输出

格式:

influx -format=[format]     

说明:

format : 启动格式,支持column,csv,json三种格式,默认为column

示例如下:

  1.  
    [root@localhost ~] # influx -format=csv
  2.  
    Visit https: //enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
  3.  
    Connected to http: //localhost:8086 version 1.1.0
  4.  
    InfluxDB shell version: 1.1.0
  5.  
    > show databases;
  6.  
    name,name
  7.  
    databases,_internal
  8.  
    databases,testdb
  9.  
    > exit
  10.  
    [root@localhost ~] # influx -format=json
  11.  
    Visit https: //enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
  12.  
    Connected to http: //localhost:8086 version 1.1.0
  13.  
    InfluxDB shell version: 1.1.0
  14.  
    > show databases;
  15.  
    { "results":[{"series":[{"name":"databases","columns":["name"],"values":[["_internal"],["testdb"]]}]}]}
  16.  
    > exit
  17.  
    [root@localhost ~] # influx -format=json -pretty
  18.  
    Visit https: //enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
  19.  
    Connected to http: //localhost:8086 version 1.1.0
  20.  
    InfluxDB shell version: 1.1.0
  21.  
    > show databases;
  22.  
    {
  23.  
    "results": [
  24.  
    {
  25.  
    "series": [
  26.  
    {
  27.  
    "name": "databases",
  28.  
    "columns": [
  29.  
    "name"
  30.  
    ],
  31.  
    "values": [
  32.  
    [
  33.  
    "_internal"
  34.  
    ],
  35.  
    [
  36.  
    "testdb"
  37.  
    ]
  38.  
    ]
  39.  
    }
  40.  
    ]
  41.  
    }
  42.  
    ]
  43.  
    }
  44.  
    >

 

  • 用户管理
    可以直接在web管理页面做操作,也可以命令行。
  1.  
    #显示用户
  2.  
    show users
  3.  
    #创建用户
  4.  
    create user "username" with password 'password'
  5.  
    #创建管理员权限用户create user "username" with password 'password' with all privileges
  6.  
    #删除用户
  7.  
    drop user "username"

 

  • 连续查询(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 
  1.  
    - cq_name:连续查询名字;
  2.  
    - db_name:数据库名字;
  3.  
    - sum(count):计算总和;
  4.  
    - table_name:当前表名;
  5.  
    - new_table_name:存新的数据的表名;
  6.  
    - 30m:时间间隔为30分钟
  • 删除Continous Queries
drop continous query cp_name on db_name
  • 数据保存策略(Retention Policies)
    influxDB是没有提供直接删除数据记录的方法,但是提供数据保存策略,主要用于指定数据保留时间,超过指定时间,就删除这部分数据。

    • 查看当前数据库Retention Policies
show retention policies on "db_name"
  •  创建新的Retention Policies
  •  
  • 修改Retention Policies
  • alter retention policy "rp_name" on "db_name" duration 30d default
    1.  
      create retention policy "rp_name" on "db_name" duration 3w replication 1 default
    2.  
      - rp_name:策略名;
    3.  
      - db_name:具体的数据库名;
    4.  
      - 3w:保存3周,3周之前的数据将被删除,influxdb具有各种事件参数,比如:h(小时),d(天),w(星期);
    5.  
      - replication 1:副本个数,一般为1就可以了;
    6.  
      - default:设置为默认策略

     

  • 删除Retention Policies
drop retention policy "rp_name"
  • 数据库与表的操作
    可以直接在web管理页面做操作,当然也可以命令行。
  1.  
    #创建数据库
  2.  
    create database "db_name"
  3.  
    #显示所有的数据库
  4.  
    show databases
  5.  
    #删除数据库
  6.  
    drop database "db_name"
  7.  
    #使用数据库
  8.  
    use db_name
  9.  
    #显示该数据库中所有的表
  10.  
    show measurements
  11.  
    #创建表,直接在插入数据的时候指定表名
  12.  
    insert test,host=127.0.0.1,monitor_name=test count=1
  13.  
    #删除表
  14.  
    drop measurement "measurement_name"

 

转载于:https://www.cnblogs.com/laraine/p/11188861.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值