Here is a python ORM/Driver for InfluxDB : Influxable

javidjms

Javid Mougamadou

Posted on December 11, 2020

Here is a python ORM/Driver for InfluxDB : Influxable

Introduction

Influxable is a lightweight Python ORM / ODM / Client for InfluxDB .

This repository allows users to deal with InfluxDB in a smooth way and to manipulate Python object.

Link

Link : https://github.com/Javidjms/influxable

Link Release : https://github.com/Javidjms/influxable/releases/tag/1.3.0

Getting Started

The package is available in pypi. You can install it via pip :

pip install influxable
Enter fullscreen mode Exit fullscreen mode

You can set your environment variable for the connection of InfluxDB in order to override the default values :

INFLUXDB_URL=http://localhost:8086
INFLUXDB_DATABASE_NAME=default

#Optional
INFLUXDB_USER=admin
INFLUXDB_PASSWORD=changme
Enter fullscreen mode Exit fullscreen mode

Then you just have to import the influxable package and create an instance of Influxable :

from influxable import Influxable

client = Influxable()
Enter fullscreen mode Exit fullscreen mode

You can also set connection variable in Influxable constructor :

# Without authentication

client = Influxable(
    base_url='http://localhost:8086',
    database_name='default',
)

# With authentication

client = Influxable(
    base_url='http://localhost:8086',
    database_name='default',
    user='admin',
    password='changeme',
)
Enter fullscreen mode Exit fullscreen mode

Create a Measurement Class

There is a built-in Measurement mapper class that you can extend it :

from influxable import attributes, serializers
from influxable.measurement import Measurement

class TemperatureMeasurement(Measurement):
    parser_class = serializers.MeasurementPointSerializer # Default
    measurement_name = 'temperature'

    time = attributes.TimestampFieldAttribute()
    phase = attributes.TagFieldAttribute()
    value = attributes.FloatFieldAttribute()
Enter fullscreen mode Exit fullscreen mode

You can also create a measurement mapper class quickly with the SimpleMeasurement class :

from influxable.measurement import SimpleMeasurement

TemperatureMeasurement = SimpleMeasurement('temperature', ['value'], ['phase'])
Enter fullscreen mode Exit fullscreen mode

Create Queries

You can query with Measurement.get_query() :

from influxable.db import Field

points = TemperatureMeasurement\
  .get_query()\
  .select('phase', 'value')\
  .where(
     Field('value') > 15.2,
     Field('value') < 30.5,
  )\
  .limit(100)
  .evaluate()
Enter fullscreen mode Exit fullscreen mode

You can also query with Query :

from influxable.db import Query, Field

points = Query()\
  .select('phase', 'value')\
  .from_measurements('temperature')\
  .where(
     Field('value') > 15.2,
     Field('value') < 30.5,
  )\
  .limit(100)
  .execute()
Enter fullscreen mode Exit fullscreen mode

Saving Data

You can create data by using Measurement.bulk_save()

points = [
    TemperatureMeasurement(phase="HOT",value=10,time=1463289075),
    TemperatureMeasurement(phase="COLD",value=10,time=1463289076),
]
TemperatureMeasurement.bulk_save(points)
Enter fullscreen mode Exit fullscreen mode

You can also create data with BulkInsertQuery

str_query = '''
temperature,phase=HOT value=10 1463289075000000000
temperature,phase=COLD value=10 1463289076000000000
'''

raw_query = BulkInsertQuery(str_query)
res = raw_query.execute()
Enter fullscreen mode Exit fullscreen mode

Other Features

  • group_by()

  • range_by

  • populate command

  • autogenerate measurements command

  • multiple serializer like PandasSerializer

  • etc

💖 💪 🙅 🚩
javidjms
Javid Mougamadou

Posted on December 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related