怎么统计redis的hash中的某个字段个数,在线等
迷茫
迷茫 2017-04-24 16:00:39
0
5
913

存储的hash这样的:

user:1 => id:1, name: 'a', age:30, sex:1
user:2 => id:2, name: 'b', age:18, sex:0
user:3 => id:3, name: 'c', age:22, sex:1
user:4 => id:4, name: 'd', age:10, sex:0

我想统计sex为1的用户有多少个?

另外再复杂一点,以age分组,分成三组[age<15, 15<=age<25, age>=25],分别统计用用户的个数

刚入redis,找遍了资料,没有发现相关的文档

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(5)
仅有的幸福

The characteristics of Redis determine that it is not suitable for statistical operations. Because there is no structured query language like SQL. Here is a solution for you, but you need to modify your data organization:

In redis, if you really have statistical needs, you can use SortedSet. Each key in the ordered set corresponds to a score, and the values ​​can be sorted according to the score.

For example:

redis> ZADD age 10 jack
(integer) 0
redis> ZADD age 15 tom
(integer) 0
redis> ZADD age 25 peter
(integer) 0

The above code inserts three pieces of data into the age sorted set, using the age of the three people as the score. If you want to count the age range between 10-20, as follows:

ZRANGEBYSCORE age 10 20

If the data you want to store is not a simple string and there is no way to store it directly as a key, there are two solutions:

  1. Convert the data to a string in json format as the key, and then restore the json string to a format supported by the code when taking it out. For example, you can use json.dumps 将字典转为字符串,从 redis 取出时再使用json.loadsrestore in python.

  2. Use id as the key, and then use another hash or other data structure to store actual user information. In this way, you need to first find out the id based on the statistical field, and then check the complete information based on the id, which requires two queries.

But the above solution only applies if you only have one field for statistics, such as the age you mentioned. If you have multiple statistical needs, there must be a solution in theory, but I think you should consider whether you really need to use redis to complete this work. After all, the usage scenarios of redis limit its ability to perform statistics as complex and diverse as SQL. You can consider other NoSQL, such as mongodb.

It is wisest to choose tools based on your usage scenarios, rather than just looking at everything as a nail if you have a hammer in your hand.

伊谢尔伦

Does it have to be handled in redis-cli? To access redis in other languages, you can take out user:* and then traverse the count.
I just started using redis =.=

世界只因有你

Redis query should only be through the key. The key can be matched according to the rules. You can do something on the key.

phpcn_u1582

Don’t force it, just switch to a relational database.

Or add a key specifically for statistics every time you insert data.
INCR('sex:1')
INCR('sex:1')

INCR('age:0')
INCR('age:1')
INCR('age:2')
0/1/2 represents age group

仅有的幸福

If you don’t make an application and just know how many values ​​a certain hash key has, it’s very simple. Hkey keyname will list all fields, and then redis will automatically give you a sort number, which is a count, right? !

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!