发布时间:2019-09-26 10:00:45编辑:auto阅读(2411)
之前已经在自己的MQTT服务器上安装好了InfluxDB时序数据库,接下来写个Python程序测试下.
API详情参阅这里
打开powershell,执行pip install influxdb
安装模块
程序API详细参考这里
# coding=utf-8
import time
from influxdb import InfluxDBClient
def read_info():
data_list = [{'measurement': 'win',
'tags': {'cpu': 'i7-7700HQ'},
'fields': {'cpu_info_user': cpu_time_info[0],
'cpu_info_system': cpu_time_info[1],
'cpu_info_idle': cpu_time_info[2],
'cpu_info_interrupt': cpu_time_info[3],
'cpu_info_dpc': cpu_time_info[4]}}]
return data_list
if __name__ == '__main__':
client = InfluxDBClient(host, port, user, password, database) # 初始化
counts = 0 #计数,也就是数据上传20次
while counts <= 20:#
counts += 1
client.write_points(read_info())
time.sleep(5)
程序功能很简单,每5秒上传一次CPU使用状况,连续上传20次.
其中核心知识是数据格式,这里会自动打包字典到JSON,一开始在这打包成JSON上传报错.而且需要把这个字典放在列表中,因为一次可以添加多个字典格式上传.其中必须有'measurement','tags','fields'三个Key.
在 InfluxDB 中,我们可以粗略的将要存入的一条数据看作一个虚拟的 key 和其对应的 value(field value),格式如下:
cpu_usage,host=server01,region=us-west value=0.64 1434055562000000000
虚拟的 key 包括以下几个部分: database, retention policy, measurement, tag sets, field name, timestamp。 database 和 retention policy 在上面的数据中并没有体现,通常在插入数据时在 http 请求的相应字段中指定。
上一篇: Windows系统下Eclipse上搭建
下一篇: 本地安装python包的方法
48447
47332
38211
35460
29917
26622
25594
20545
20234
18663
47°
54°
158°
109°
138°
242°
327°
328°
305°
394°