Skip to content

Script Development / InfluxDB

The operation object of the InfluxDB connector is a wrapper for the third-party Python package influxdb (version 5.2.3), primarily providing methods for querying InfluxDB. This connector is compatible with the following databases:

  • Alibaba Cloud Time Series Database InfluxDB Edition

This connector is based on HTTP protocol connection

Parameters for DFF.CONN(...) are as follows:

Parameter Type Required / Default Value Description
connector_id str Required Connector ID
database str None Specify database

.query(...)

Executes an InfluxQL statement, parameters are as follows:

Parameter Type Required / Default Value Description
sql str Required InfluxQL statement, may include bound parameter placeholders in the form $var_name
bind_params dict None Bound parameters
database str None Specifies the database for this query
dict_output dict False Automatically converts returned data into {column name: value} format
Example
1
2
3
sql = 'SELECT * FROM demo WHERE city = $city LIMIT 5'
bind_params = {'city': 'hangzhou'}
result = db.query(sql, bind_params=bind_params, database='demo')
Output Example
1
# {'series': [{'columns': ['time', 'city', 'hostname', 'status', 'value'], 'name': 'demo', 'values': [['2018-12-31T16:00:10Z', 'hangzhou', 'webserver', 'UNKNOW', 90], ['2018-12-31T16:00:20Z', 'hangzhou', 'jira', 'running', 40], ['2018-12-31T16:00:50Z', 'hangzhou', 'database', 'running', 50], ['2018-12-31T16:01:00Z', 'hangzhou', 'jira', 'stopped', 40], ['2018-12-31T16:02:00Z', 'hangzhou', 'rancher', 'UNKNOW', 90]]}]}
Example 2 (Specify return result as dictionary format)
1
2
3
sql = 'SELECT * FROM demo WHERE city = $city LIMIT 5'
bind_params = {'city': 'hangzhou'}
result = db.query(sql, bind_params=bind_params, database='demo', dict_output=True)
Output Example
1
# {'series': [[{'city': 'hangzhou', 'hostname': 'webserver', 'status': 'UNKNOW', 'time': '2018-12-31T16:00:10Z', 'value': 90}, {'city': 'hangzhou', 'hostname': 'jira', 'status': 'running', 'time': '2018-12-31T16:00:20Z', 'value': 40}, {'city': 'hangzhou', 'hostname': 'database', 'status': 'running', 'time': '2018-12-31T16:00:50Z', 'value': 50}, {'city': 'hangzhou', 'hostname': 'jira', 'status': 'stopped', 'time': '2018-12-31T16:01:00Z', 'value': 40}, {'city': 'hangzhou', 'hostname': 'rancher', 'status': 'UNKNOW', 'time': '2018-12-31T16:02:00Z', 'value': 90}]]}

.query2(...)

The query2(...) method also executes InfluxQL statements but uses different parameter placeholders, using ? as a parameter placeholder. Parameters are as follows:

Parameter Type Required / Default Value Description
sql str Required InfluxQL statement, may include parameter placeholders.
? represents escaped parameters;
?? represents unescaped parameters
sql_params list None InfluxQL parameters
database str None Specifies the database for this query
dict_output dict False Automatically converts returned data into {"column name": "value"} format
Example
1
2
3
sql = 'SELECT * FROM ?? WHERE city = ? LIMIT 5'
sql_params = ['demo', 'hangzhou']
result = db.query2(sql, sql_params=sql_params, dict_output=True)

Dynamic SQL Statement

Internally, query2(...) uses DFF.SQL(...) to construct SQL statements and supports building complex dynamic SQL statements.

For example, WHERE IN (...) with an uncertain number of values, or INSERT INTO ... VALUES ... for batch inserting data.

For more details, please refer to Script Development / SQL Construction DFF.SQL

.write_point(...)

Added in version 1.1.13

Writes a single data point. Parameters are as follows:

Parameter Type Required / Default Value Description
measurement str Required Measurement
fields dict{str: str/int/float/bool} Required Fields
Key names must be str
Key values can be str/int/float/bool
tags dict{str: str} None Tags
Key names and key values must both be str
timestamp str/int None Time
ISO format, e.g., 2020-01-01T01:02:03Z
UNIX timestamp, e.g., 1577840523
database str None Specifies the database for this write
Example
1
2
3
fields = { 'cpu': 100, 'mem': 0.5 }
tags   = { 'host': 'web001' }
result = db.write_point(measurement='host_monitor', fields=fields, tags=tags)

.write_points(...)

Added in version 1.1.13

Batch writes multiple data points. Parameters are as follows:

Parameter Type Required / Default Value Description
points list Required Array of data points
points[#]['measurement'] str Required Measurement
points[#]['fields'] dict{str: str/int/float/bool} Required Fields
Key names must be str
Key values can be str/int/float/bool
points[#]['tags'] dict{str: str} None Tags
Key names and key values must both be str
points[#]['time'] str/int None Time
ISO format, e.g., 2020-01-01T01:02:03Z
UNIX timestamp, e.g., 1577840523
database str None Specifies the database for this write
Example
1
2
3
4
5
6
7
8
points = [
    {
        'measurement': 'host_monitor',
        'fields'     : { 'cpu': 100, 'mem': 0.5 },
        'tags'       : { 'host': 'web001' },
    }
]
result = db.write_points(points)