Trying InfluxDB using Docker

July 05, 2019 in #influxdb #docker #testing #trial #microservices #insight #analytics #data


I wanted to test out InfluxDB for a project so I though "whats the easiest way I can play around with it in 30 mintues"

I chose to use Docker as I had it installed on my machine and it just makes things easy...

So here is what I found

The Docker Part

Here we are going to run the InfluxDB container and then attach to the running container in order to use the InfluxDB CLI.

Continer Info can be found here -

One caviat before we begin, yes you will need to have docker installed or alternatively you can install InfluxDB on a machine.

Downloading and Running the Container

Run docker run -p 8086:8086 influxdb in your terminal, if you don't have the latest image downloaded then you'll see that docker pulls it from docker hub.

laptop:adam.jarvis$ docker run -p 8086:8086 influxdb
Using default tag: latest
latest: Pulling from library/influxdb
6f2f362378c5: Pull complete 
494c27a8a6b8: Pull complete 
7596bb83081b: Pull complete 
058194e87993: Pull complete 
e59ecfe2e49e: Pull complete 
0e92dec1b17d: Pull complete 
f53096159982: Pull complete 
0cf0c73a3724: Pull complete 
Digest: sha256:81270b10e58ce1aa448b3d4ab2ba9d55b6b1fa35e7815ae38d5f346206f17c57
Status: Downloaded newer image for influxdb:latest

Once the container is downloaded, the container will start - I am using InfluxDB version: 1.7.7.

The -p 8086:8086 command isn't necessary for this but if you want to connect an external service such as Grafana you need this port open.

Conecting to the Container

Once the container has started run to docker ps command which should give you this output.

laptop:adam.jarvis$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
10733ba6d014        influxdb            "/ infl…"   8 seconds ago       Up 7 seconds        8086/tcp            agitated_colden

From here we need to connect to the container so that we can access it's CLI.

Run the docker exec -it <CONTAINER ID> /bin/bash command, this attachs us to the container and delivers us to the bash promt

laptop:adam.jarvis$ docker exec -it  10733ba6d014 /bin/bash

We are now able to access the InfluxDB containers CLI, the Docker part is complete...

InfluxDB Part

Creating a Database

To enter the InfluxDB CLI simply type influx into the the container terminal that we just attached too.

root@10733ba6d014:/# influx
Connected to http://localhost:8086 version 1.7.7
InfluxDB shell version: 1.7.7

Now we are in the InfluxDB CLI the first thing to do is to create a database, this is done by running the create database <database name> statement.

> create database myFirstInfluxDb

To make sure it's created run show databases

> show databases
name: databases

Inserting Data into the Database

Before we can insert data into the DB we first need to swtich to it, this is done using the use <database name> command.

> use myFirstInfluxDb
Using database myFirstInfluxDb

Now that we are using the database we want to create a table and add data but first we must understand the InfluxDB Line Protocol

Source -

The InfluxDB line protocol is a text based format for writing points to the database.

A single line of text in line protocol format represents one data point in InfluxDB. It informs InfluxDB of the point’s measurement, tag set, field set, and timestamp. The following code block shows a sample of line protocol and breaks it into its individual components:

weather,location=us-midwest temperature=82 1465839830100400200
  |    -------------------- --------------  |
  |             |             |             |
  |             |             |             |
|measurement|,tag_set| |field_set| |timestamp|

The way I see it

  • measurement = The name of the table
  • tag_set = Some meta data about the information such as location or server host name (Can have multiple tag sets)
  • field_set = The values that you actually use such as the temprature in this example (Can have multiple field sets)
    • Having multiple field sets will effectivly create multiple columns. In the case of our garage sensor below it takes a reading of the temprature and the humidiy.
  • timestamp = the time the data was inserted - you can chose to add a timestamp or leave it blank. If you leave it blank the server will use the time the data was inserted as the timestamp
    • Unix timestamp

The insert statements look like insert <measurement>,<tag_set>=<tag_set value> <field_set>=<field_set value>. Insert the values below so that we can manipulate these after.

insert tempratureRecord,location="Garden" temp=32i
insert tempratureRecord,location="Garden" temp=12i
insert tempratureRecord,location="Garden" temp=19i

insert tempratureRecord,location=“Bedroom” temp=18i
insert tempratureRecord,location=“Bedroom” temp=17i
insert tempratureRecord,location=“Bedroom” temp=21i

insert tempratureRecord,location=“Garage” temp=20i,humidity=65i
insert tempratureRecord,location=“Garage” temp=30i,humidity=53i
insert tempratureRecord,location=“Garage” temp=40i,humidity=47i

Notice the letter i after each number - this is used to signal that the value is an integer. Normally with a relational SQL database you would define the data type in advance but not with InfluxDB this is done when we insert data.

Quering the Database

Basic 'Select' Statements

Now we have data in the database we can begin to manipulate it. The query syntax has a strong resembilance to SQL so to get all the data from the the tempratureRecord table we run select * from tempratureRecord.

> select * from tempratureRecord
name: tempratureRecord
time                humidity location temp
----                -------- -------- ----
1562158793803208700          Garden   40
1562158793814101500          Garden   12
1562158793817141000          Garden   19
1562158793820141300          Bedroom  18
1562158793823133500          Bedroom  17
1562158793825951200          Bedroom  21
1562158793829004000 65       Garage   20
1562158793836015300 53       Garage   30
1562158794478890100 47       Garage   40

This, like SQL, selects all the data in the measurement (table) tempratureRecord including both the tag and field sets.

From here we can select specific columns from the measurement (table) by specifiying the field/tag set. In this example we are selecting the all the records that have the humidity filed set. This is done by typing select humidity from tempratureRecord.

> select humidity from tempratureRecord
name: tempratureRecord
time                humidity
----                --------
1562158793829004000 65
1562158793836015300 53
1562158794478890100 47

As you can see only three entries are shown as when we inserted the data only three records had the humidity field attribute.

And finally as you'd expect you can select multiple fields at once. Here we are selecting both the temp and humidity field and not the location tag set attribute

> select temp, humidity from tempratureRecord
name: tempratureRecord
time                temp humidity
----                ---- --------
1562158793803208700 40   
1562158793814101500 12   
1562158793817141000 19   
1562158793820141300 18   
1562158793823133500 17   
1562158793825951200 21   
1562158793829004000 20   65
1562158793836015300 30   53
1562158794478890100 40   47

'Where' Statements

The 'where' statements are important when we want to minipulate the data that we have.

You can do where clauses on both the tag sets and field sets. Firstly we are going to select all the data that came from the garden sensor using select temp from tempratureRecord where location='Garden'

Notice because the tag set is a string we need to put the word in quotes - location='Garden'.

> select temp from tempratureRecord where location='Garden'
name: tempratureRecord
time                temp
----                ----
1562158793803208700 40
1562158793814101500 12
1562158793817141000 19

We then get all the data from just the garden sensor as expected and now to get data from a field set. Good news this is exactly the same!

This time we are going to return all the tempratures that were over 19 Degrees (Celsius).

> select temp from tempratureRecord where temp > 19
name: tempratureRecord
time                temp
----                ----
1562158793803208700 40
1562158793825951200 21
1562158793829004000 20
1562158793836015300 30
1562158794478890100 40

So that's it for now! Next I'll try this with Grafana so that we can visualise the data we've inserted into this database.

Share on Google+
No Newer Posts