Getting Started with Python InfluxDB

Monitoring a complex network is made that much simpler using python with influxDB. While there are sophisticated plugins and add-ons available under the InfluxDB ecosystem, Python comes in helpful when data needs to be queried.

Data continues to evolve, as there are more primary input devices present in the ecosystem. From IoT devices to smart wearables, more information needs to be processed the right way. That’s where Python comes in and provides a robust framework to manage the data.

Developers can also perform second-tier functions within the architecture, especially when leveraging Python to its fullest extent. As time series data may need to be further analyzed, it’s important to get started with Python the right way.

Depending on the scale and complexity of the project, Python can be integrated to produce streamlined results. It can also help in sending out automated alerts when a certain threshold has been cleared, or the user has accomplished a task.

Installing the python library

The most effective way to install the Python library for your project would be to use install it using pip. Running pip using the -m argument to the Python command is the best way to ensure maximum compatibility.

$ python3 -m pip install influxdb

There would be a message showing successful installation. This is an indication of the installation moving in the right direction. The next step would be to use REPL to ensure immediate output as and when code is entered. The start should be with importing the InfluxDBClient from the python-influxdb library base.

If there are errors in the output message, then there could be issues with the command entered or the version of Python being used. There have been instances of compatibility issues arising from older Python versions, with different code versions of the project. It’s ideal to use the latest version of Python or use a consistent Python version when working with older code base or projects.

Creating initial connection

The next step in the protocol is to establish a connection by creating a new instance of the InfluxDBClient. Developers will also have to share the server information as they’re creating a secure connection. Starting with a local default port is recommended when creating an initial connection.

>>> client = InfluxDBClient(host=’localhost’, port=8086)

SSL utilization, timeout and UDP parameters will also have to be shared when making the connection. Developers can also connect to a remote host at a certain domain by incorporating the appropriate port and SSL directly.

Using key commands can help in initializing the data management protocol effectively.

>>> client.create_database(‘pyexample’) – The command can help create a new database


>>> client.get_list_database()

[{‘name’: ‘telegraf’}, {‘name’: ‘_internal’}, {‘name’: ‘pyexample’}] Checks the presence of the referenced database

Writing and querying data

When the connection is properly configured and the database is active, it’s time to start writing directly into the database. Using the write_points() method can help in adding information directly onto the backend.

Using JSON, developers can add vital information about the data being tracked and introduce metadata around the key inputs. Time series data can then be reviewed agnostically, while reviewing key parameters within the code.

A reference code written could be as follows:

“measurement”: “RunEvents”,

    “tags”: {

        “user”: “Adam”,

        “RunId”: “2244CCFS”

The code is designed to track the runner’s ID while going on a specific run on a specific day.

Developers can also query data from the system to generate valuable insights. By querying on InfluxDB, results can be obtained using the client’s query() function. The function then returns a ResultSet object, which contains all the key data housed inside it.

The scale and flexibility provided by Python can help manage complex time series data points seamlessly. It’s recommended to visit the documentation and libraries available on each query and function.

About Author