读取series 时间,插入时,time设置为相同时间即可,但要注意时间戳精确度
# -*- coding:utf-8 -*-
from influxdb import InfluxDBClient
import time
import pytz,datetime
def foo(start,end):
res = client.query("select field1 from table1 where dev ='sss1' and time>='{}' and time <'{}' GROUP BY sss1".format(start,end))
# print(res.raw.get('series')[0]['values'])
influx_list = []
if res:
for key in res.raw.get('series')[0]['values']:
try:
tim = utc_to_local(key[0])
except ValueError:
tim = utc_to_local(key[0],utc_format='%Y-%m-%dT%H:%M:%SZ')
json_body = {
'measurement': 'table1',
'tags': {
'sn': 'thermofisher-001',
},
'time': tim, # 时间一定要正确
'fields': {
'sss2': float(key[1]),
}
}
influx_list.append(json_body) #插入即替换原来
# print(influx_list)
# client.write_points(influx_list)
def utc_to_local(utc_time_str, utc_format='%Y-%m-%dT%H:%M:%S.%fZ'):
local_tz = pytz.timezone('Asia/Chongqing')
local_format = "%Y-%m-%d %H:%M:%S"
utc_dt = datetime.datetime.strptime(utc_time_str, utc_format)
local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
time_str = local_dt.strftime(local_format)
timestamp = int(time.mktime(time.strptime(time_str, local_format)))*1000000000 + \int(float('0'+utc_time_str[19:-1])*1000000000)
return timestamp
if __name__ == '__main__':
client = InfluxDBClient('192.168.1.172', 8086, '', '', 'novadb')
start = datetime.datetime(2018,7,31,0,0,0)
# step = datetime.timedelta(days=1)
end = '2018-08-01 17:04:44'
foo(start, end)
python 读取influxdb,修改数据(其实是覆盖)