使用Python操作InfluxDB时序数据库
安装python包
influxdb,这里我安装的是5.3.0版本
pip install influxdb==5.3.0
使用
-
from influxdb import InfluxDBClient
-
conn_db=InfluxDBClient( '127.0.0.1', '8086', 'u_wyk13195', 'p_wyk13195', 'my_monitor')
-
1 #数据库操作
-
conn_db.create_database( 'testdb') #创建数据库
-
print(conn_db.get_list_database()) #显示所有数据库名称
-
conn_db.drop_database( 'testdb') #删除数据库
-
print(conn_db.get_list_database()) #显示所有数据库名称
-
-
2 #表操作
-
#influxDB没有提供单独的建表语句,可以通过并添加数据的方式建表,示例如下:
-
json_body = [
-
{
-
"measurement": "students",
-
"tags": {
-
"stuid": "s123"
-
},
-
#"time": "2017-03-12T22:00:00Z",
-
"fields": {
-
"score": 89
-
}
-
}
-
]
-
conn_db.write_points(json_body) #写入数据,同时创建表
-
result = conn_db.query( "show measurements") #显示数据库中的表
-
print( "Result: {}".format(result))
-
3 #查询:可以通过influxql语句实现,示例如下:
-
result = conn_db.query( 'select * from students;')
-
print( "Result: {0}".format(result))
-
4 #通过influxql语句实现
-
conn_db.query( 'drop measurement students') #删除表
-
result = conn_db.query( "show measurements") #显示数据库中的表
-
print( "Result: {}".format(result))
-
# 5 更新
-
# tags和timestamp相同时数据会覆盖操作,相当于influxDB的更新操作
-
-
# 6 删除
-
# 使用influxql语句实现,delete语法,示例如下"
-
# client.query('delete from students;') #删除数据