Writing Measurements¶
Examples¶
You can write measurements in a few different ways, but writing a single “temperature” measurement is as simple as:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename')
client.write('temperature', value=21.3)
No time is specified in the above example, so inflow automatically set’s the
measurement’s time to the current time. Also, no tags are provided to the
write
method, so no tags are attached to the measurement.
A more complex example of writing a single measurement:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename')
client.write(
'temperature',
tags={
'location': 'groningen',
'sensor_type': 'ni1000'
},
value=21.3,
timestamp=1475845863
)
Writing multiple measurements is also possible:
#!/usr/bin/env python
from inflow import Client, Measurement
client = Client('http://username:pass@localhost:8086/databasename')
client.write([
Measurement(
name='temperature',
tags={
'location': 'groningen',
'sensor_type': 'ni1000'
},
value=21.3,
timestamp=1475845863
),
Measurement(
name='temperature',
tags={
'location': 'groningen',
'sensor_type': 'ni1000'
},
value=20.1,
timestamp=1475848864
)
])
However, this is a bit verbose. That’s why you can also do this:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename')
temperature = dict(
name='temperature',
tags={
'location': 'groningen',
'sensor_type': 'ni1000'
}
)
client.write(temperature, [
{'value': 21.3, 'timestamp': 1475845863},
{'value': 20.1, 'timestamp': 1475846182}
])
In the above examples, every write
call will issue a direct call to the
InfluxDB API. You can accumulate measurements and write them all at once using
Sessions.
Note
In every example, we use timestamp ints (in seconds) to specify the time for each measurement. You can also set the timestamp to a datetime. Inflow will automatically convert both to the right precision when writing to InfluxDB.
Multiple Values¶
In all the examples above, we assume there is only one actual value
for
the given measurements. However, InfluxDB supports having an arbitrary amount
of values for every measurements. This is also possible in Inflow:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename')
client.write(
'temperature',
timestamp=1475846182,
lower_sensor=20.9,
upper_sensor=23.2
)
This will create a measurement with the lower_sensor
and upper_sensor
values. This method also works when manually writing Measurement
instances, and when writing lists of dicts.
Precision¶
By default, Inflow assumes the timestamps that are written to InfluxDB are in seconds. However, you can specify a custom precision when creating the client:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename',
precision='ms')
client.write('temperature', value=21.3, timestamp=1476191999000)
The precision needs to be one of: h, m, s, ms, u or ns.
Retention Policies¶
By default, Inflow will write to the database’s default retention policy. However, you can explicitly specify which retention policy your measurements should be written to:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename',
retention_policy='rp_four_weeks')
client.write('temperature', value=21.3)
You can also specify the retention policy when calling into write
:
#!/usr/bin/env python
from inflow import Client
client = Client('http://username:pass@localhost:8086/databasename')
client.write('temperature', value=21.3, retention_policy='rp_four_weeks')