2017-09-13 66 views
0

现在,我的mongodb有一张表,它是关键字和被关键字抓取的次数被存储的,现在如何将关键字插入到redis列表中并由抓取的次数优先水平? thks!非常 这是我的代码:如何将mongo数据插入到redis列表中

def init_mongo_to_redis(mongo_db, redis_pool): 
    r = redis.Redis(connection_pool = redis_pool) 
    mongo_handle = mongo_db.keywords_tbl.find({}, {'keyword': 1, 'keyword_type': 1, \ 
      'ignore_station': 1}, no_cursor_timeout=True) 
    redis_len = r.llen('fetch_keywords') 
    if redis_len != 0: 
     logging.info('redis fetch_keywords size is %d', redis_len) 
     return 

    logging.info('init redis fetch_keywords start') 

    r_pipe = r.pipeline() 
    pbar = tqdm(mongo_handle) 
    for keyword in pbar: 
     item = { 
      'keyword_type': keyword['keyword_type'], 
      'ignore_station': keyword['ignore_station'], 
      'keyword': keyword['keyword'] 
     } 
     r_pipe.lpush('fetch_keywords', json.dumps(item)) 
     pbar.set_description('Processing %s' % keyword) 
    r_pipe.execute() 

    logging.info('init redis fetch_keywords end :%d', r.llen('fetch_keywords')) 
+0

请发表您已经拿出代码,并询问具体问题 –

回答

0

任何时候你有一个数字订购(整数或浮点数)在Redis的价值,这是更好地考虑首先使用Sorted Sets。您可以通过一个分数顺序值,你的情况我想是

数的争夺优先级