Labels

Aws (32) MySQL (20) Aurora (12) Rds (7) DynamoDB (6) Cloudformation (4) Python3 (4) Unix (3) MongoDB (1)

Friday, 14 September 2018

TSDB Time series database

A Time Series Database (TSDB) is a database optimized for time-stamped or time series data. Time series are simply measurements or events that are tracked, monitored, downsampled, and aggregated over time.
A Time Series Database is built specifically for handling metrics and events or measurements that are time-stamped.

Installation server

Ubuntu
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.6.2_amd64.deb
sudo dpkg -i influxdb_1.6.2_amd64.deb
Most of the settings in the local configuration file (/etc/influxdb/influxdb.conf
There are two ways to launch InfluxDB with your configuration file:
Point the process to the correct configuration file by using the -config option:
influxd -config /etc/influxdb/influxdb.conf
Set the environment variable INFLUXDB_CONFIG_PATH to the path of your configuration file and start the process. For example:
echo $INFLUXDB_CONFIG_PATH
/etc/influxdb/influxdb.conf

influxd

Getting started

Creating a database
create database mydb show databases use mydb The _internal database is created and used by InfluxDB to store internal runtime metrics.
Writing and exploring data
  • Data in InfluxDB is organized by "time series", which contain a measured value, like "cpu_load" or "temperature".
  • Time series have zero to many points, one for each discrete sample of the metric.
  • Points consist of
    • time (a timestamp),
    • a measurement ("cpu_load", for example),
    • from zero to many key-value tags containing any metadata about the value (e.g. "host=server01", "region=EMEA", "dc=Frankfurt").
    • at least one key-value field (the measured value itself, e.g. "value=0.64", or "temperature=21.2"),
Conceptually you can think of a measurement as an SQL table, where the primary index is always time. tags and fields are effectively columns in the table.
  • tags are indexed
  • fields are not indexed
    The difference is that, with InfluxDB, you can have millions of measurements, you don’t have to define schemas up-front, and null values aren’t stored.
Line Protocol
[,=...] =[,=...] [unix-nano-timestamp]
  • tag opzionali
  • field at least ONE
Dirty hands
cpu,host=serverA,region=us_west value=0.64
> INSERT cpu,host=serverA,region=us_west value=0.64  ## host,region are tags - value is a field
> select * from cpu where value=0.64
name: cpu
time                          host    region  value
----                          ----    ------  -----
2018-08-16T16:17:28.03345666Z serverA us_west 0.64

- Host,Region are indexed
- Value doesn't have index
influx -precision rfc3339
Connected to http://localhost:8086 version 1.6.1
InfluxDB shell version: 1.6.1

>
> show databases;
name: databases
name
----
_internal
>
> create database mydb
> show databases;
name: databases
name
----
_internal
mydb
> use mydb
Using database mydb
> insert cpu,host=serverA,region=us_west value=0.64
> select * from cpu
name: cpu
time                          host    region  value
----                          ----    ------  -----
2018-08-16T16:17:28.03345666Z serverA us_west 0.64
> insert payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i 1434067467100293230
> select * from cpu where host=serverA
> select * from cpu where host='serverA'
name: cpu
time                          host    region  value
----                          ----    ------  -----
2018-08-16T16:17:28.03345666Z serverA us_west 0.64
> select * from cpu where value=0.64
name: cpu
time                          host    region  value
----                          ----    ------  -----
2018-08-16T16:17:28.03345666Z serverA us_west 0.64

Other point examples
payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i 1434067467100293230
stock,symbol=AAPL bid=127.46,ask=127.48
temperature,machine=unit42,type=assembly external=25,internal=37 1434067467000000000

InfluxDB key concepts

Sample data
name: census
-————————————
time            butterflies     honeybees     location     scientist
2015-08-18T00:00:00Z      12               23            1         langstroth
2015-08-18T00:00:00Z      1                30            1         perpetua
2015-08-18T00:06:00Z      11               28            1         langstroth
2015-08-18T00:06:00Z      3                28            1         perpetua
InfluxDB is a time series database so it makes sense to start with what is at the root of everything we do: time all data in InfluxDB have the column time stores timestamps, and the timestamp shows the date and time, in RFC3339 UTC
The next two columns, called butterflies and honeybees, are fields. They are Field keys the collection of field-key and field-value pairs make up a field set. Here are all eight field sets in the sample data:
  • butterflies = 12 honeybees = 23
  • butterflies = 1 honeybees = 30
  • butterflies = 11 honeybees = 28 Fields are a required piece of InfluxDB’s data structure - you cannot have data in InfluxDB without fields. It’s also important to note that fields are not indexed
The last two columns in the sample data, called location and scientist, are tags Both tag keys and tag values are stored as strings and record metadata. Tags are optional. You don’t need to have tags in your data structure tags are indexed. In the data above, the tag set is the different combinations of all the tag key-value pairs. The four tag sets in the sample data are:
  • location = 1, scientist = langstroth
  • location = 2, scientist = langstroth
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. The only measurement in the sample data is census.
A single measurement can belong to different retention policies. A retention policy describes how long InfluxDB keeps data (DURATION) and how many copies of those data are stored in the cluster (REPLICATION
In InfluxDB, a series is the collection of data that share a retention policy, measurement, and tag set. The data above consist of four series:
Arbitrary series number    Retention policy    Measurement    Tag set
series 1        autogen            census        location = 1,scientist = langstroth
series 2        autogen            census        location = 2,scientist = langstroth
series 3        autogen            census        location = 1,scientist = perpetua
Finally, a point is the field set in the same series with the same timestamp. For example, here’s a single point:
name: census
-----------------
time                    butterflies honeybees   location    scientist
2015-08-18T00:00:00Z    1           30          1           perpetua
The series in the example is defined by the retention policy (autogen), the measurement (census), and the tag set (location = 1, scientist = perpetua). The timestamp for the point is 2015-08-18T00:00:00Z.
InfluxDB is a schemaless database which means it’s easy to add new measurements, tags, and fields at any time

Sample data

we’ll use the database name NOAA_water_database
CREATE DATABASE NOAA_water_database
Download and write the data to InfluxDB
curl https://s3.amazonaws.com/noaa.water-database/NOAA_data.txt -o NOAA_data.txt
influx -import -path=NOAA_data.txt -precision=s -database=NOAA_water_database
Test queries
influx -precision rfc3339 -database NOAA_water_database
influx -precision rfc3339 -database NOAA_water_database

show tables;
ERR: error parsing query: found tables, expected CONTINUOUS, DATABASES, DIAGNOSTICS, FIELD, GRANTS, MEASUREMENT, MASUREMENTS, QUERIES, RETENTION, SERIES, SHARD, SHARDS, STATS, SUBSCRIPTIONS, TAG, USERS at line 1, char 6

> help;
ERR: error parsing query: found help, expected SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL at line 1, char 1


> SHOW measurements
name: measurements
------------------
name
average_temperature
h2o_feet
h2o_pH
h2o_quality
h2o_temperature


Count the number of non-null values of water_level in h2o_feet:
> SELECT COUNT("water_level") FROM h2o_feet

> show series;
key
---
average_temperature,location=coyote_creek
average_temperature,location=santa_monica
h2o_feet,location=coyote_creek
h2o_feet,location=santa_monica
h2o_pH,location=coyote_creek
h2o_pH,location=santa_monica
h2o_quality,location=coyote_creek,randtag=1
h2o_quality,location=coyote_creek,randtag=2
h2o_quality,location=coyote_creek,randtag=3
h2o_quality,location=santa_monica,randtag=1
h2o_quality,location=santa_monica,randtag=2
h2o_quality,location=santa_monica,randtag=3
h2o_temperature,location=coyote_creek
h2o_temperature,location=santa_monica

> show tag keys from h2o_temperature;
name: h2o_temperature
tagKey
------
location


> show queries;
qid query        database            duration status
--- -----        --------            -------- ------
16  SHOW QUERIES NOAA_water_database 42µs     running

> show field keys from h2o_temperature;
name: h2o_temperature
fieldKey fieldType
-------- ---------
degrees  float

help show
Usage:
    connect <host:port>   connects to another node specified by host:port
    auth                  prompts for username and password
    pretty                toggles pretty print for the json format
    chunked               turns on chunked responses from server
    chunk size <size>     sets the size of the chunked responses.  Set to 0 to reset to the default chunked size
    use <db_name>         sets current database
    format <format>       specifies the format of the server responses: json, csv, or column
    precision <format>    specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns
    consistency <level>   sets write consistency level: any, one, quorum, or all
    history               displays command history
    settings              outputs the current settings for the shell
    clear                 clears settings such as database or retention policy.  run 'clear' for help
    exit/quit/ctrl+d      quits the influx shell

    show databases        show database names
    show series           show series information
    show measurements     show measurement information
    show tag keys         show tag key information
    show field keys       show field key information

To read further

Questions

How do I understand which are tags and fields in a table? Is there an example my_database to download and test?