您的位置:首页 > 编程语言 > Python开发

python访问InfluxDB

2020-02-02 17:12 141 查看

封装了python方位InfluxDB的类,代码如下:

# coding=utf-8
import time
import datetime
from influxdb import InfluxDBClient

class InfluxDBVisitor():

def __init__(self):
self.host = "XXX.XXX.XXX.XXX"
self.port = 8086
self.user = "influx"
self.password = "influx"
self.database = "test"
self.beginTime = datetime.datetime.now()

def Init(self):
self.client = InfluxDBClient(self.host, self.port, self.user, self.password, self.database)  # 初始化

def InsertInput(self, dictInput):
# 插入数据
#data_list = [{'measurement': 'cpu',
#              'tags': {'host': '127.0.0.1'},
#              'fields': {'load': 0.5,
#                         'usage': 0.8}}]
#rlt = self.client.write_points(data_list)
tmpTime = self.beginTime + datetime.timedelta(microseconds=dictInput['time'])

dictTmp = {}
dictTmp['id'] = dictInput['id']
dictTmp['age'] = dictInput['age']
dictTmp['height'] = dictInput['height']
dictTmp['inp'] = dictInput['inp']
dictTmp['do'] = dictInput['do']
dictTmp['pan'] = dictInput['pan']
dictRecord = {}
dictRecord['fields'] = dictTmp
dictRecord['measurement'] = 'haha'
dictRecord['time'] = tmpTime.isoformat()

json_body = []
json_body.append(dictRecord)
rlt = self.client.write_points(json_body)

def SaveInputs(self, arrayPulses):
json_body = []
for dictPulse in arrayPulses:
toaTime = self.beginTime + datetime.timedelta(microseconds=dictPulse['time'])

dictTmp = {}
dictTmp['id'] = dictInput['id']
dictTmp['age'] = dictInput['age']
dictTmp['height'] = dictInput['height']
dictTmp['inp'] = dictInput['inp']
dictTmp['do'] = dictInput['do']
dictTmp['pan'] = dictInput['pan']
dictRecord = {}
dictRecord['fields'] = dictTmp
dictRecord['measurement'] = 'haha'
dictRecord['time'] = tmpTime.isoformat()
json_body.append(dictRecord)
rlt = self.client.write_points(json_body)

def GetDatas(self):
# 获取结果
#self.cur.execute('SELECT * FROM conditions ORDER BY time DESC LIMIT 100;')
#results = self.cur.fetchall()
#print(results)
result = self.client.query('select * from pdw where age > 20;')
pass

def Close(self):
# 关闭连接
#self.conn.commit()
#self.cur.close()
#self.conn.close()
pass

def read_info():
data_list = [{'measurement': 'cpu',
'tags': {'host': '127.0.0.1'},
'fields': {'load': 0.5,
'usage': 0.8}}]
return data_list

host = "XXX.XXX.XXX.XXX"
port = 8086
user = "influx"
password = "influx"
database = "test"
if __name__ == '__main__':
client = InfluxDBClient(host, port, user, password, database) # 初始化
counts = 0 #计数,也就是数据上传20次
while counts <= 20:#
counts += 1
rlt = client.write_points(read_info())
time.sleep(1)
  • 点赞 1
  • 收藏
  • 分享
  • 文章举报
常思大妹子 发布了11 篇原创文章 · 获赞 7 · 访问量 214 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: