InfluxAlchemy


In [1]:
import influxdb
import influxalchemy

Define InfluxAlchemy Measurements


In [2]:
class Widgets(influxalchemy.Measurement):
    __measurement__ = 'widgets'


class Wombats(influxalchemy.Measurement):
    __measurement__ = 'wombats'

Open InfluxAlchemy Connection


In [3]:
db = influxdb.DataFrameClient(database="example")
flux = influxalchemy.InfluxAlchemy(db)

Query InfluxDB

Query Single Measurement


In [4]:
flux.query(Widgets)


Out[4]:
SELECT * FROM widgets;

Query Ad Hoc Measurement


In [5]:
flux.query(influxalchemy.Measurement.new("/.*/"))


Out[5]:
SELECT * FROM /.*/;

Select Fields of Measurement


In [6]:
flux.query(Widgets.tag1, Widgets.field2)


Out[6]:
SELECT tag1, field2 FROM widgets;

Query Across Measurements


In [7]:
flux.query(Widgets | Wombats)


Out[7]:
SELECT * FROM /widgets|wombats/;

Filter Tags


In [8]:
flux.query(Widgets).filter(Widgets.tag1 == "fizz")


Out[8]:
SELECT * FROM widgets WHERE (tag1 = 'fizz');

Filter Tags with 'like'


In [9]:
flux.query(Widgets).filter(Widgets.tag1.like("/z$/"))


Out[9]:
SELECT * FROM widgets WHERE (tag1 =~ /z$/);

Chain Filters


In [10]:
clause1 = Widgets.tag1 == "fizz"
clause2 = Widgets.tag2 == "buzz"
flux.query(Widgets).filter(clause1 & clause2)


Out[10]:
SELECT * FROM widgets WHERE (tag1 = 'fizz' AND tag2 = 'buzz');

In [11]:
flux.query(Widgets).filter(clause1 | clause2)


Out[11]:
SELECT * FROM widgets WHERE (tag1 = 'fizz' OR tag2 = 'buzz');

Group Bys


In [12]:
flux.query(Widgets).group_by("time(1d)")


Out[12]:
SELECT * FROM widgets GROUP BY time(1d);

In [13]:
flux.query(Widgets).group_by(Widgets.tag1)


Out[13]:
SELECT * FROM widgets GROUP BY tag1;

Time


In [14]:
flux.query(Widgets).filter(Widgets.time > "now() - 7d")


Out[14]:
SELECT * FROM widgets WHERE (time > now() - 7d);

In [15]:
flux.query(Widgets).filter(Widgets.time.between("'2016-01-01'", "now() - 7d"))


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

In [ ]: