时序数据库InfluxDB的基本语法

一 了解InfluxDB的必要性 时序数据库主要存放的数据

Time series data is a series of data points each associated with a specific time. Examples include:

Server performance metrics

Financial averages over time

Sensor data, such as temperature, barometric pressure, wind speeds, etc.

时序数据库和关系数据库的区别

Relational databases can be used to store and analyze time series data, but depending on the precision of your data, a query can involve potentially millions of rows. InfluxDB is purpose-built to store and query data by time, providing out-of-the-box functionality that optionally downsamples data after a specific age and a query engine optimized for time-based data.

二 基本概念 2.1 database & duration

database

A logical container for users, retention policies, continuous queries, and time series data.

duration

The attribute of the retention policy that determines how long InfluxDB stores data. Data older than the duration are automatically dropped from the database. 

2.2 field

The key-value pair in an InfluxDB data structure that records metadata and the actual data value. Fields are required in InfluxDB data structures and they are not indexed - queries on field values scan all points that match the specified time range and, as a result, are not performant relative to tags.

Field keys are strings and they store metadata.Field values are the actual data; they can be strings, floats, integers, or booleans. A field value is always associated with a timestamp.

2.3 Tags

Tags are optional. The key-value pair in the InfluxDB data structure that records metadata.You don’t need to have tags in your data structure, but it’s generally a good idea to make use of them because, unlike fields, tags are indexed. This means that queries on tags are faster and that tags are ideal for storing commonly-queried metadata.

Tags 与  fields 的区别

Tags are indexed and fields are not indexed. This means that queries on tags are more performant than those on fields.

Tags 与  fields 的使用场景

(1)Store commonly-queried meta data in tags

(2)Store data in tags if you plan to use them with the InfluxQL GROUP BY clause

(3)Store data in fields if you plan to use them with an InfluxQL function

(4)Store numeric values as fields (tag values only support string values)

2.4 measurement 

The measurement acts as a container for tags, fields, and the time column, and the measurement name is the description of the data that are stored in the associated fields. Measurement names are strings, and, for any SQL users out there, a measurement is conceptually similar to a table.

2.5 point

In InfluxDB, a point represents a single data record, similar to a row in a SQL database table. Each point:

has a measurement, a tag set, a field key, a field value, and a timestamp;

is uniquely identified by its series and timestamp.

You cannot store more than one point with the same timestamp in a series. If you write a point to a series with a timestamp that matches an existing point, the field set becomes a union of the old and new field set, and any ties go to the new field set.

2.6 series

In InfluxDB, a series is a collection of points that share a measurement, tag set, and field key. A point represents a single data record that has four components: a measurement, tag set, field set, and a timestamp. A point is uniquely identified by its series and timestamp.

series key

A series key identifies a particular series by measurement, tag set, and field key.

三 查询 3.1 正则模糊查询

1.实现查询以给定字段开始的数据

select fieldName from measurementName where fieldName=~/^给定字段/

2.实现查询以给定字段结束的数据

select fieldName from measurementName where fieldName=~/给定字段$/

3.实现查询包含给定字段数据

select fieldName from measurementName where fieldName=~/给定字段/

3.2 Select 注意事项:

必须包含field key

A query requires at least one field key in the SELECT clause to return data. If the SELECT clause only includes a single tag key or several tag keys, the query returns an empty response. This behavior is a result of how the system stores data.

3.3 Where 限定

使用单引号,否则无数据返回或报错

(1)Single quote string field values in the WHERE clause. Queries with unquoted string field values or double quoted string field values will not return any data and, in most cases,will not return an error.

(2)Single quote tag values in the WHERE clause. Queries with unquoted tag values or double quoted tag values will not return any data and, in most cases, will not return an error.

3.4 Group By 

(1)Note that the GROUP BY clause must come after the WHERE clause.

(2)The GROUP BY clause groups query results by:  one or more specified tags ;specified time interval。

(3)You cannot use GROUP BY to group fields.

(4)fill() changes the value reported for time intervals that have no data.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzgfyp.html