Categories
Uncategorized

InfluxDB from principles to practical – InfluxDB common underlying operating

0x00 basis of operation will be introduced in this paper InfluxDB common base operation, to help the reader establish perceptions of InfluxDB, fast hands to play with, continuous query (Continuous Queies), Group by, Series, Line Protocol (Line Protocol), InfluxQL and other advanced features and details will be gradually introduced in subsequent articles. InfluxDB support SQL-like user interface, although because of the characteristics of time series data, InfluxDB non CRUD does not support update data, delete a single data operations, but considering that most readers of the corresponding CRUD relational databases are more familiar with, in this paper, or in accordance CRUD principle, introduce the database (database), a retention policy (retention policy), table (Measurement), data is written, data query, data update, delete data and other content. InfluxDB influx support InfluxDB API 2, and command line interfaces modes of operation, herein, will influx operation command as an example, the foregoing description. Operation examples in this article, based on the latest stable version InfluxDB, InfluxDB-v1.7.8.0×01 database (Database) 1. Create a database with the CREATE statement to create a database named telegraf.

$ influx
Connected to http://localhost:8086 version 1.7.8
InfluxDB shell version: 1.7.8
> create database telegraf
>

2. Check the database through the SHOW statement to see all of the current database.

> show databases;
name: databases
name
----
_internal
monito
telegraf
>

3. Delete the database through the DROP statement to delete a database named telegraf. > Drop database telegraf> 4. USE statement by using the database, use the database named telegraf.

> use telegraf
Using database telegraf
>

0x02 retention policy (Retention Policy) 1. View the database corresponding retention policy statement by SHOW, view the database telegraf corresponding retention policy.

> show retention policies on telegraf
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        true
>

2. Create a retention policy by the CREATE statement, create a retention policy called rp-one-year’s.

> create retention policy "rp-one-year" on "telegraf" duration 365d replication 1
>
> show retention policies on telegraf
name        duration  shardGroupDuration replicaN default
----        --------  ------------------ -------- -------
autogen     0s        168h0m0s           1        true
rp-one-year 8760h0m0s 168h0m0s           1        false
> 

3. Change the retention policy by ALTER statement to change the retention policy rp-one-year retention policy as the default database telegraf of.

> alter retention policy "rp-one-year" on "telegraf" duration 365d replication 1 default
>
> show retention policies on telegraf
name        duration  shardGroupDuration replicaN default
----        --------  ------------------ -------- -------
autogen     0s        168h0m0s           1        false
rp-one-year 8760h0m0s 168h0m0s           1        true
>

4. Remove the retention policy by DROP statement to delete database telegraf retention policies rp-one-year.

> drop retention policy "rp-one-year" on "telegraf"
>
> show retention policies on telegraf
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        false
>

0x03 table (Measurement) 1. Create a table with different relational data like MySQL, in the InfluxDB no need to explicitly create the table, when the write timing data, according to a row InfluxDB server protocol, a corresponding table is automatically created on demand. Telegraf in the database, insert the data, automatically creates a table devops-idc-sz.

> use telegraf
Using database telegraf
>
> show measurements
>
> insert devops-idc-sz,host=server01 cpu=23.1,mem=0.63
>
> show measurements
name: measurements
name
----
devops-idc-sz
>

2. Check the measurements SHOW statement by ,, view all tables in the database telegraf.

> show measurements
name: measurements
name
----
devops-idc-sz
>

3. Remove table through the DROP statement, deleting all data table devops-idc-sz and corresponding.

> drop measurement devops-idc-sz
>
> show measurements
> 

0x04 data write timing and the data line through the INSERT statement protocols, inserted into table 3 DevOps environment devops-idc-sz recorded in time series data corresponding to time 2019/8/30 17:44:53.

> insert devops-idc-sz,host=server01 cpu=16.1,mem=0.43 1567158293000000000
> insert devops-idc-sz,host=server02 cpu=23.8,mem=0.63 1567158293000000000
> insert devops-idc-sz,host=server03 cpu=56.3,mem=0.78 1567158293000000000

0x05 data query through the SELECT statement, all the data look-up table devops-idc-sz in.

> select * from devops-idc-sz
name: devops-idc-sz
time                cpu  host     mem
----                ---  ----     ---
1567158293000000000 16.1 server01 0.43
1567158293000000000 56.3 server03 0.78
1567158293000000000 23.8 server02 0.63
>

0x06 data update since the time series data read write more features no less updated, InfluxDB, does not support data update, the author nor recommended to perform update operations on time series data record. If in some special scenarios, the index value must be updated to record the time series data, you can use the “time stamp (Timestamp) and time-series line (Series) exact chronological data recording, data recording timing is the same, the newly inserted index value of time series data, will cover the characteristics of the original time series data record “, the update timing of the data record. Update index value time-series data recorded on 2019/8/30 17:44:53’s server01.

> insert devops-idc-sz,host=server01 cpu=76.1,mem=0.83 1567158293000000000
>
> select * from devops-idc-sz
name: devops-idc-sz
time                cpu  host     mem
----                ---  ----     ---
1567158293000000000 76.1 server01 0.83
1567158293000000000 56.3 server03 0.78
1567158293000000000 23.8 server02 0.63
>

0x07 delete the same data, because less time-series data write and read no update batch delete records time-series data features, InfluxDB does not support removing a single time series data record. In addition to deleting the data recorded by the timing of the periodic timing retention policy, InfluxDB also further also supports conditional statement WHERE, deletion time sequential line, deleting the table, delete the database, delete the slice (Shard) directly to the mass delete, etc. timing specified data record . 1. Remove the conditional statement WHERE by the timing data from the specified table records from Table devops-idc-sz, remove the tag named host, Server01 label value, the timing of the time point at 2019/8/30 17:44:53 data record. (Note: WHERE conditional statements, does not support index (Field), only supports tags and timestamps.)

> delete from  devops-idc-sz where "host"='server01' and  time=1567158293s
>

2. Delete by deleting the time series data recorded sequential line, remove all of the timing of data recording label “host” = ‘server01’ line corresponding to the time series.

> drop series from devops-idc-sz where "host"='server01'
>

3. Delete by deleting the recording data timing of the specified table, delete a table of all the sequential data records devops-idc-sz corresponding.

> drop measurement devops-idc-sz
>

4. Delete by deleting the recording data timing of specifying the database, delete all records time series data corresponding to the database telegraf.

> drop database telegraf
>

The specified slice sequential data deleted by deleting records, deleting data records all of the timing slice corresponding to 6.

> select *  from  devops-idc-sz
name: devops-idc-sz
time                cpu  host     mem
----                ---  ----     ---
1567158293000000000 16.1 server01 0.43
1567158293000000000 56.3 server03 0.78
1567158293000000000 23.8 server02 0.63
>
> show shards
name: _internal
id database  retention_policy shard_group start_time           end_time             expiry_time          owners
-- --------  ---------------- ----------- ----------           --------             -----------          ------
1  _internal monitor          1           2019-08-27T00:00:00Z 2019-08-28T00:00:00Z 2019-09-04T00:00:00Z
4  _internal monitor          4           2019-08-30T00:00:00Z 2019-08-31T00:00:00Z 2019-09-07T00:00:00Z

name: monito
id database retention_policy shard_group start_time           end_time             expiry_time          owners
-- -------- ---------------- ----------- ----------           --------             -----------          ------
3  monitor  autogen          3           2019-04-29T00:00:00Z 2019-05-06T00:00:00Z 2019-05-06T00:00:00Z
2  monitor  autogen          2           2019-08-26T00:00:00Z 2019-09-02T00:00:00Z 2019-09-02T00:00:00Z

name: telegraf
id database retention_policy shard_group start_time           end_time             expiry_time          owners
-- -------- ---------------- ----------- ----------           --------             -----------          ------
6  telegraf autogen          6           2019-08-26T00:00:00Z 2019-09-02T00:00:00Z 2019-09-02T00:00:00Z
>
> drop shard 6
>
> select *  from  devops-idc-sz
> 

Postscript: Welcome exchanges and discussions: micro-channel public number: influxdb-dev. InfluxDB technical exchange group (QQ): 663274123.

Leave a Reply