如何在redis hash中搜索关键模式? [英] How to search a key pattern in redis hash?

查看:50
本文介绍了如何在redis hash中搜索关键模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个哈希表,其键的模式为 USER_TEL,例如:

I have a hash table whose keys are of pattern USER_TEL like:

bob_123456  : Some address
mary_567894 : other address
john_123456 : third address

现在,我想获取所有密钥中具有相同电话号码的用户的地址.

Now, I'd like to get addresses of all uses who have the same TEL in their keys.

我想到的是:

tel = 123456
r.hmget('address_book', '*_%s' % tel)

我得到 [None] 而不是值.

推荐答案

你应该使用 HSCAN 命令.

例如:

redis> HMSET address_book bob_123456 Address1 mary_567894 Address2 john_123456 Address3
OK
redis> HSCAN address_book 0 match *_123456
1) "0"
2) 1) "bob_123456"
   2) "Address1"
   3) "john_123456"
   4) "Address3"

<小时>

更新

Python 实现:


Update

Python implementation:

r = Redis(....) #redis url
for address in r.hscan_iter('address_book', match='*_123456'):
  print(address)

这篇关于如何在redis hash中搜索关键模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆