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"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM