Python操作InfluxDB

influxdb包:influxdb

通过Python使用InfluxDBClient类操作数据库,操作如下:

from influxdb import InfluxDBClient

client = InfluxDBClient('localhost', 8086, 'username', 'password', 'dbname')

# 显示已存在的数据库
print(client.get_list_database())
# 创建数据库
client.create_database('py_db1')
print(client.get_list_database())
# 删除数据库
client.drop_database('py_db1')
print(client.get_list_database())

写入:

# 待写入数据库的点组成的列表
points = [
    {
        'measurement': 'table1',
        'tags': {
            'host': 'server01',
            'region': 'us-west'
        },
        'time': '2021-04-16T12:00:00Z',
        'fields': {
            'value': 0.64
        }
    }
]

# 将这些点写入指定database
client.write_points(points, database='py_db1')

# 查询刚刚写入的点
result = client.query('select value from table1;', database='py_db1')

print(result)
[{'name': '_internal'}, {'name': 'db0415'}, {'name': 'mydb'}]
ResultSet({'('table1', None)': [{'time': '2021-04-16T12:00:00Z', 'value': 0.64}]})
原文地址:https://www.cnblogs.com/wangzhilong/p/14675393.html