Query InfluxDB using SQLAlchemy-style syntax

amancevice, updated 🕥 2023-01-12 15:00:17

InfluxAlchemy

pypi python pytest coverage maintainability

Query InfluxDB using SQLAlchemy-style syntax

Installation

bash pip install influxalchemy

Usage

python import influxdb import influxalchemy

Define InfluxAlchemy Measurements

```python class Widgets(influxalchemy.Measurement): measurement = 'widgets'

class Wombats(influxalchemy.Measurement): measurement = 'wombats' ```

The class-attribute __measurement__ can be omitted and will default to the class name if absent.

Open InfluxAlchemy Connection

python db = influxdb.DataFrameClient(database="example") flux = influxalchemy.InfluxAlchemy(db)

Query InfluxDB

Query Single Measurement

```python

SELECT * FROM widgets;

flux.query(Widgets) ```

Query Ad Hoc Measurement

```python

SELECT * from /.*/;

flux.query(influxalchemy.Measurement.new("/.*/")) ```

Select Fields of Measurement

```python

SELECT tag1, field2 FROM widgets;

flux.query(Widgets.tag1, Widgets.field2) ```

Query Across Measurements

```python

SELECT * FROM /widgets|wombats/;

flux.query(Widgets | Wombats) ```

Filter Tags

```python

SELECT * FROM widgets WHERE tag1 = 'fizz';

flux.query(Widgets).filter(Widgets.tag1 == "fizz") ```

Filter Tags with 'like'

```python

SELECT * FROM widgets WHERE tag1 =~ /z$/;

flux.query(Widgets).filter(Widgets.tag1.like("/z$/")) ```

Chain Filters

```python clause1 = Widgets.tag1 == "fizz" clause2 = Widgets.tag2 == "buzz"

SELECT * FROM widgets WHERE tag1 = 'fizz' AND tag2 = 'buzz';

flux.query(Widgets).filter(clause1 & clause2)

SELECT * FROM widgets WHERE tag1 = 'fizz' OR tag2 = 'buzz';

flux.query(Widgets).filter(clause1 | clause2) ```

Group By

```python

SELECT * FROM widgets GROUP BY time(1d);

flux.query(Widgets).group_by("time(1d)")

SELECT * FROM widgets GROUP BY tag1;

flux.query(Widgets).group_by(Widgets.tag1) ```

Time

```python

SELECT * FROM widgets WHERE (time > now() - 7d);

flux.query(Widgets).filter(Widgets.time > "now() - 7d")

SELECT * FROM widgets WHERE time >= '2016-01-01' AND time <= now() - 7d;

d = date(2016, 1, 1) flux.query(Widgets).filter(Widgets.time.between(d, "now() - 7d")) ```

Note that naive datetime object will be assumed in UTC timezone.

Issues

Aggregator functions

opened on 2018-07-26 21:34:12 by saulofr

How add aggregator functions like mean, sum, integral...?

Like this: flux.query(ElectricalMeasurement.power.integral.as.energy).filter(clause1 & clause2).group_by(group_by)

Releases

0.2.7 2022-11-30 00:30:49

migrate to pyproject.toml drop support for Python 3.6 and earlier

0.2.6 2020-12-26 21:40:41

Python 3.9 support

0.2.1 2018-10-31 16:43:35

Security updates

0.2.0 2018-04-02 21:32:18

  • Bump dependencies
  • Added docker-compose.yml for playing with test data.

0.1.1 2018-02-19 15:33:04

  • Added LIMIT functionality (https://github.com/amancevice/influxalchemy/pull/4)
  • Brought test suite up to date with my current standards
  • Added six dependency to help with Python 2/3 compatibility re Metaclass'ing
  • Some other minor tweaks

0.1.0 2017-08-21 12:33:23

Alexander Mancevice

senior cloud infrastructrician

GitHub Repository

influxdb sqlalchemy python