@@ -9,6 +9,7 @@ InfluxDB 2.0 python client library. TODO...
9
9
- [ Features] ( #how-to-use )
10
10
- [ Writing data] ( #writes )
11
11
- [ How to efficiently import large dataset] ( #how-to-efficiently-import-large-dataset )
12
+ - [ Efficiency write data from IOT sensor] ( #efficiency-write-data-from-iot-sensor )
12
13
13
14
## Requirements
14
15
@@ -101,6 +102,8 @@ The [WriteApiClient](https://github.com/bonitoo-io/influxdb-client-python/blob/m
101
102
102
103
#### How to efficiently import large dataset
103
104
105
+ - sources - [ import_data_set.py] ( https://github.com/bonitoo-io/influxdb-client-python/blob/master/influxdb2_test/import_data_set.py )
106
+
104
107
``` python
105
108
"""
106
109
Import VIX - CBOE Volatility Index - from "vix-daily.csv" file into InfluxDB 2.0
@@ -189,5 +192,89 @@ for table in result:
189
192
Close client
190
193
"""
191
194
client.__del__ ()
195
+ ```
196
+
197
+ #### Efficiency write data from IOT sensor
198
+
199
+ - sources - [ iot_sensor.py] ( https://github.com/bonitoo-io/influxdb-client-python/blob/master/influxdb2_test/iot_sensor.py )
200
+
201
+ ``` python
202
+ """
203
+ Efficiency write data from IOT sensor - write changed temperature every minute
204
+ """
205
+ import atexit
206
+ import platform
207
+ from datetime import timedelta
208
+
209
+ import psutil as psutil
210
+ import rx
211
+ from rx import operators as ops
212
+
213
+ from influxdb2.client.influxdb_client import InfluxDBClient
214
+ from influxdb2.client.write_api import WriteApi
215
+ from influxdb2.client.write_api import WriteOptions
216
+
217
+
218
+ def on_exit (db_client : InfluxDBClient, write_api : WriteApi):
219
+ """ Close clients after terminate a script.
220
+
221
+ :param db_client: InfluxDB client
222
+ :param write_api: WriteApi
223
+ :return: nothing
224
+ """
225
+ write_api.__del__ ()
226
+ db_client.__del__ ()
227
+
228
+
229
+ def sensor_temperature ():
230
+ """ Read a CPU temperature. The [psutil] doesn't support MacOS so we use [sysctl].
231
+
232
+ :return: actual CPU temperature
233
+ """
234
+ os_name = platform.system()
235
+ if os_name == ' Darwin' :
236
+ from subprocess import check_output
237
+ output = check_output([" sysctl" , " machdep.xcpm.cpu_thermal_level" ])
238
+ import re
239
+ return re.findall(r ' \d + ' , str (output))[0 ]
240
+ else :
241
+ return psutil.sensors_temperatures()[" coretemp" ][0 ]
242
+
243
+
244
+ def line_protocol (temperature ):
245
+ """ Create a InfluxDB line protocol with structure:
246
+
247
+ iot_sensor,hostname=mine_sensor_12,type=temperature value=68
248
+
249
+ :param temperature: the sensor temperature
250
+ :return: Line protocol to write into InfluxDB
251
+ """
252
+
253
+ import socket
254
+ return ' iot_sensor,hostname={} ,type=temperature value={} ' .format(socket.gethostname(), temperature)
255
+
256
+
257
+ """
258
+ Read temperature every minute; distinct_until_changed - produce only if temperature change
259
+ """
260
+ data = rx.interval(period = timedelta(seconds = 60 ))\
261
+ .pipe(ops.map(lambda t : sensor_temperature()),
262
+ ops.map(lambda temperature : line_protocol(temperature)),
263
+ ops.distinct_until_changed())
264
+
265
+ _db_client = InfluxDBClient(url = " http://localhost:9999/api/v2" , token = " my-token-123" , org = " my-org" , debug = True )
266
+
267
+ """
268
+ Create client that writes data into InfluxDB
269
+ """
270
+ _write_api = _db_client.write_api(write_options = WriteOptions(batch_size = 1 ))
271
+ _write_api.write(org = " my-org" , bucket = " my-bucket" , record = data)
272
+
273
+
274
+ """
275
+ Call after terminate a script
276
+ """
277
+ atexit.register(on_exit, _db_client, _write_api)
192
278
279
+ input ()
193
280
```
0 commit comments