前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >redis 读取数据,并且对里面的时间进行排序读取。[第N次优化]

redis 读取数据,并且对里面的时间进行排序读取。[第N次优化]

作者头像
简单、
发布2018-07-17 17:44:58
9410
发布2018-07-17 17:44:58
举报
代码语言:javascript
复制
def redis(start_time=0,end_time=0,fields=None):
    import redis
    import json

    client = redis.StrictRedis(host='192.168.31.49')
    addres =  fields.split(",")

    data = []

    for dd in addres:
        for key in client.keys(str("*")+str(dd)+str("*")):
            d = json.loads(key)
            at = int(d['timeStamp'])
            if int(start_time) < at <= int(end_time):
                tmp = client.get(key)
                if tmp is not None:
                    val = json.loads(tmp)
                    d['value'] = str(val['val'])
                    data.append(d)
        data.sort(key=lambda d: d["timeStamp"])

    result = dict()
    if addres is not None:
        try:
            for field in addres:
                da = {}
                da['val'] = []
                da['timeStamp'] = []
                da['device'] = ''
                da['description'] = None
                result[int(field)] = da
        except Exception,e:
            print e
    for d in data:
        # if d['address'] not in result:
        #     print d['address'],result
        #     continue
        try:

            addr = int(d['address'])
            value = d['value']
            timeStamp = int(d['timeStamp'])
            device = int(d['device'])
            description = response_redis_code(addr)
            result[addr]['val'].append(str(value))
            result[addr]['timeStamp'].append(str(timeStamp))
            result[addr]['device'] = device
            result[addr]['description'] = description
        except Exception,e:
            print e

    # 拼接数据,或者可以在 result 里直接填写 device 和 address 字段
    def join(item):
        item[1]['address'] = item[0]
        item[1]['device'] = device
        return item[1]


    # map 函数,或者直接使用列表推导式
    # return [join(item) for item in result.items()]
    return list(map(join, result.items()))

代码语言:javascript
复制
def redis(start_time=0,end_time=0,fields=None):
    import redis
    import json

    client = redis.StrictRedis(host='192.168.31.49')
    addres =  fields.split(",")

    data = []
    for dd in addres:
        for key in client.keys("*"+dd+"*"):
            d = json.loads(key)
            at = int(d['timeStamp'])
            if int(start_time) < at <= int(end_time):
                continue
            val = json.loads(client.get(key))
            d['value'] = val['val']
            data.append(d)
        data.sort(key=lambda d: d["timeStamp"])

    result = dict()
    if addres is not None:
        for field in addres:
            da = {}
            da['val'] = []
            da['timeStamp'] = []
            da['device'] = 0
            da['description'] = None
            result[int(field)] = da

    for d in data:
        if d['address'] not in result:
            print d['address'],result
            continue
        value = int(d['value'])
        timeStamp = int(d['timeStamp'])
        device = int(d['device'])
        description = response_redis_code(d['address'])
        result[d['address']]['val'].append(value)
        result[d['address']]['timeStamp'].append(timeStamp)
        result[d['address']]['device'] = device
        result[d['address']]['description'] = description


    # 拼接数据,或者可以在 result 里直接填写 device 和 address 字段
    def join(item):
        item[1]['address'] = item[0]
        item[1]['device'] = device
        return item[1]

    # map 函数,或者直接使用列表推导式
    # return [join(item) for item in result.items()]
    return list(map(join, result.items()))

代码语言:javascript
复制
import redis
import json

def redis(start_time=0,end_time=0):
    device = '1'
    client = redis.StrictRedis(host='192.168.31.49')

data = []
for key in client.keys():
    d = json.loads(key)
    at = int(d['timeStamp'])
    if at < start_time or at > end_time:
        continue
    val = json.loads(client.get(key))
    d['value'] = val['val']
    data.append(d)

data.sort(key=lambda d: d["timeStamp"])

result = {
   16407: {
       'description': u'UA相电压',
       'val': [],
       'timeStamp': [],
   },
   16410: {
       'description': u'UAB相电压',
       'val': [],
       'timeStamp': [],
   },
   16432: {
       'description': u'FR频率',
       'val': [],
       'timeStamp': [],
   }
}

for d in data:
    if d['address'] not in result or d['device'] != device:
        continue
    result[d['address']]['val'].append(d['val'])
    result[d['address']]['timeStamp'].append(d['timeStamp'])

# 拼接数据,或者可以在 result 里直接填写 device 和 address 字段
def join(item):
    item[1]['address'] = item[0]
    item[1]['device'] = device
    return item[1]

# map 函数,或者直接使用列表推导式
# return [join(item) for item in result.items()]
return list(map(join, result.items()))

下面的弃用

代码语言:javascript
复制
def redis(start_time=0,end_time=0):
    import redis
    import json
    pool = redis.ConnectionPool(host='192.168.31.49', port=6379, db=0)
    r = redis.Redis(connection_pool=pool)

    pipe = r.pipeline()
    pipe_size = 100000

    len = 0
    key_list = []
    arr_ts_16407 = []
    arr_val_16407 = []
    address_16407 = None
    address_16410 = None
    address_16432 = None
    arr_ts_16410 = []
    arr_val_16410 = []
    arr_ts_16432 = []
    arr_val_16432 = []
    description = []
    data = []
    for key in r.keys():
        tmp = json.loads(key)
        val = json.loads(r.get(key))
        tmp['value'] = val['val']
        data.append(tmp)

    for d in data:
        key_list.append(d)
    key_list.sort(key=lambda d: d["timeStamp"])
    for key in key_list:
        dec = key["address"]
        ts = key["timeStamp"]
        val = key["value"]
        if dec in [16407, 16410, 16432] and ts > start_time and ts <= end_time:
            if dec == 16407:
                arr_ts_16407.append(ts)
                arr_val_16407.append(val)
                address_16407 = 16407
            if dec == 16410:
                arr_ts_16410.append(ts)
                arr_val_16410.append(val)
                address_16410 = 16410
            if dec == 16432:
                arr_ts_16432.append(ts)
                arr_val_16432.append(val)
                address_16432 = 16432
    return [{"address": 16407, "timeStamp": arr_ts_16407,"val": arr_val_16407,'description':u"UA相电压","device":1},{"address": 16410, "timeStamp": arr_ts_16410,"val": arr_val_16410,'description':u"UAB相电压","device":1},{"address": 16432, "timeStamp": arr_ts_16432,"val": arr_val_16432,"description":u"FR频率","device":1}]
redis-读取数据
redis-读取数据

Tags: python, Redis

Archives QR Code

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档