Part 2: How-to Create an IoT Project with the TICK Stack on the Google Cloud Platform

Navigate to:

Part 2: Using the InfluxDB API

InfluxDB comes with an in-built HTTP API that you can use to perform all the basic operations. The API is powerful enough to allow you both administrative operations like creating databases and data management operations along with querying.

At this point in time, if we recap from Part 1 – we have installed InfluxDB on Google Cloud Platform powered via a single Compute Engine instance. Remember that there are 2 ports that InfluxDB uses as we saw in the previous part, 8083 (Admin Web Application) and 8086 (API). And we had exposed both these ports to allow traffic by configuring the Firewall rules in Google Compute Engine.

The InfluxDB API starting page is available here. It exposes 3 endpoints that are self-explanatory:

  • /ping
  • /query
  • /write

Since the HTTP Server API is exposed on port 8086, the HTTP endpoint will be in the following format (for e.g. /ping endpoint):

http://:8086/ping

InfluxDB also has a set of client libraries that you can use from your client programming language. That would definitely make things easier and we will take a look at that too but for now we will deal with the basic HTTP calls, so that we understand it better.

What we are going to do is execute some HTTP POST calls to the InfluxDB API Server to insert some records. Remember that in the previous section, we created the following:

  • A database named temperature_db
  • Inserted a few records with the following characteristics:
    • A measurement named temperature
    • Each measurement has a single tag named station and its value is something like S1, S2 and so on.
    • Each measurement has a value field which is the temperature reading in degrees celsius.

The Writing Data API guide for InfluxDB API comes in handy here.

Enter Google Cloud Shell

One of the most interesting and useful features available in Google Cloud Platform in my book is Google Cloud Shell. It gives you a small Compute Engine instance with most tools / language SDKs installed so that you can use that immediately for several tasks that could include:

  • Working with your Google Cloud Platform resources.
  • Using tools like curl, Docker, etc. For a full list of all the goodies that are installed for you, check this out
  • Web preview functionality

While you could run curl utility from most machines including your laptop, let us get a bit familiar with Google Cloud Shell since you would definitely find that handy for many other tasks.

To get started with the Cloud Shell, go to the Google Developers Console and login with your account, the same account in which you have created your Google Cloud Platform project.

On successful login, look at the top right near your project name and you should see the Activate Google Cloud Shell icon as shown below:

shell Click that and wait a while for the VM to be provisioned the first time and activated. On successful activation, you will see a terminal slide up as shown below and there is your development machine in the cloud:

shell2

You can now go ahead and use several of the tools / developer SDKs that have been pre-installed for you.

One of the things that you can try out is to use the standard gcloud SDK at the terminal. To see a list of your Compute Engine instances, including the one that you have created to host the InfluxDB Server, you can give the following command:

$ gcloud compute instances list

This should list out for you the instances and for your particular instances, you should see one column named EXTERNAL_IP. This is the same public IP of the Compute Engine instance that we had seen in the earlier part of this series also.

Now, let us get going with inserting the records into the InfluxDB database. We are going to insert a few records here, so go ahead and use the following curl commands:

We need to do an HTTP POST with the following details:

  • The endpoint is `http://PublicIPofInfluxDBHost:8086/write`
  • Provide a request parameter named db that will have the database name as its value. In our case, the database that we created is named `temperature_db`.
  • We need to provide the Line Protocol format data for the record. To recreate the same scenario as the earlier part, it will be of the following format: `,= value=`
  • For e.g. a sample Line Protocol will be `temperature,Station=S1 value=29.8`

I fire the following curl commands at the prompt and get back a 204 Response Code, which means that the server has successfully fulfilled the request. Please note that you should substitute the field PublicIP below with your Compute Engine public IP Address.

romin_irani@mindstormclouddemo:~$ curl -i -XPOST 'http://<PublicIP>:8086/write?db=temperature_db' --data-binary 'temperature,Station=S1 value=29.8'
HTTP/1.1 204 No Content
Request-Id: f512ef7a-d307-11e5-8067-000000000000
X-Influxdb-Version: 0.10.0
Date: Sun, 14 Feb 2016 10:44:43 GMT

romin_irani@mindstormclouddemo:~$ curl -i -XPOST 'http://<PublicIP>:8086/write?db=temperature_db' --data-binary 'temperature,Station=S1 value=29.85'
HTTP/1.1 204 No Content
Request-Id: 230bed8e-d308-11e5-806f-000000000000
X-Influxdb-Version: 0.10.0
Date: Sun, 14 Feb 2016 10:46:00 GMT

romin_irani@mindstormclouddemo:~$ curl -i -XPOST 'http://<PublicIP>:8086/write?db=temperature_db' --data-binary 'temperature,Station=S1 value=29.9'
HTTP/1.1 204 No Content
Request-Id: 305bd26a-d308-11e5-8073-000000000000
X-Influxdb-Version: 0.10.0
Date: Sun, 14 Feb 2016 10:46:22 GMT

Note that the above method that I demonstrated required one HTTP request to be made for each measurement. You can also provide multiple (bulk) measurements to be sent in one single request. InfluxDB Write API allows for that. It also allows for the Line Protocol data to be present in a file and you can then replay that file via curl. Check out the Write API for more details.

We can query if our records have gotten created via the Web UI or even via the HTTP API.

temp2

Query API

You can use the Query API, which is a HTTP API to query for InfluxDB records.

The HTTP Endpoint that you will have to invoke will be as follows:

http://PublicIPofInfluxDBHost:8086/query/db=<dbname>&q=<query>

For e.g. let us query for all temperature measurements for Station S2 (we have only 1 record with Station = S2) in Google Cloud Shell via curl. The output from the curl command is shown below:

romin_irani@mindstormclouddemo:~$ curl -G 'http://<PublicIP>:8086/query?pretty=true' --data-urlencode "db=temperature_db" 
--data-urlencode "q=SELECT * FROM temperature WHERE Station='S2'"
{
   "results": [
       {
           "series": [
               {
                   "name": "temperature",
                   "columns": [
                       "time",
                       "Station",
                       "value"
                   ],
                   "values": [
                       [
                           "2016-01-05T09:27:06.269925241Z",
                           "S2",
                           29.9
                       ]
                   ]
               }
           ]
       }
   ]
}
romin_irani@mindstormclouddemo:~$

Try other queries like COUNT(value) or MEAN(value) to get the total number of records or average temperature reading in degrees celsius.

Some sample queries are given below:

SELECT COUNT(value) from temperature

SELECT COUNT(value) from temperature GROUP BY Station

SELECT MEAN(value) from temperature GROUP BY Station

Refer to InfluxQL for more information.

In this part we covered how you could manage your InfluxDB data with the HTTP API that is exposed by the server. To recap, it exposes 3 endpoints /ping, /query and /write. In the next part, we are going to look at how to use a client library to perform the same operation.

What's next?

  • This concludes part two of this tutorial. In Part 3 we'll look at using the InfluxDB client libraries. Follow us on Twitter @influxdb to catch the next blog in this series.
  • Looking to level up your InfluxDB knowledge? Check out our economically priced virtual and public trainings.