In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
How does redis implement conditional query? This problem may be often seen in our daily work. Through this question, I hope you can gain more. Let's take a look at the solution with the editor today.
First, import jar package
Second, realize simple conditional query
Create a User entity class
Public class User {private String id; private String name; private String sex; private int age; public String getId () {return id;} public User () {super ();} public void setId (String id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name } public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public User (String id, String name, String sex, int age) {super (); this.id = id This.name = name; this.sex = sex; this.age = age;} @ Override public String toString () {return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";}}
Create 5 objects and store them in the cache so that we can test
/ / Connect redis Jedis jedis = new Jedis ("127.0.0.1", 6379); Map map = new HashMap (); final String USER_TABLE = "USER_TABLE"; / / map String uuid1 = UUID.randomUUID (). ToString (); User user1 = new User (uuid1, "y1", "m", 15) / / convert the object to json map.put (uuid1, JSONObject.fromObject (user1). ToString ()); String uuid2 = UUID.randomUUID (). ToString (); User user2 = new User (uuid2, "y2", "m", 18); map.put (uuid2, JSONObject.fromObject (user2). ToString ()); String uuid3 = UUID.randomUUID (). ToString () User user3 = new User (uuid3, "Y3", "n", 25); map.put (uuid3, JSONObject.fromObject (user3). ToString ()); String uuid4 = UUID.randomUUID (). ToString (); User user4 = new User (uuid4, "Y4", "n", 15); map.put (uuid4, JSONObject.fromObject (user4). ToString ()) String uuid5 = UUID.randomUUID (). ToString (); User user5 = new User (uuid5, "Y5", "m", 25); map.put (uuid5, JSONObject.fromObject (user5). ToString ()); / / jedis.hmset map in cache ("USER_TABLE", map)
After querying in redis, you can see that five user objects have been stored in the cache
Next, first implement a single-condition query, such as querying user with age 15 and user with gender m
Because Redis is a nosql, it is impossible to directly use where for conditional query as mysql does, so Redis can only use a stupid method to implement conditional query: store all eligible user in a set.
Jedis jedis = new Jedis ("127.0.0.1", 6379); Map map = new HashMap (); final String USER_TABLE = "USER_TABLE"; / / query age 15, gender n final String USER_TABLE_AGE_15 = "USER_TABLE_AGE_15"; final String USER_TABLE_SEX_m = "USER_TABLE_SEX_m" Final String USER_TABLE_SEX_n = "USER_TABLE_SEX_n"; / / map String uuid1 = UUID.randomUUID () .toString (); User user1 = new User (uuid1, "y1", "m", 15); / / convert the object to json map.put (uuid1, JSONObject.fromObject (user1). ToString ()) / save the Id of qualified user in set jedis.sadd (USER_TABLE_AGE_15,uuid1); jedis.sadd (USER_TABLE_SEX_m,uuid1); String uuid2 = UUID.randomUUID (). ToString (); User user2 = new User (uuid2, "y2", "m", 18); map.put (uuid2, JSONObject.fromObject (user2). ToString ()) Jedis.sadd (USER_TABLE_SEX_m,uuid2); String uuid3 = UUID.randomUUID (). ToString (); User user3 = new User (uuid3, "Y3", "n", 25); map.put (uuid3, JSONObject.fromObject (user3). ToString ()); String uuid4 = UUID.randomUUID (). ToString () User user4 = new User (uuid4, "Y4", "n", 15); map.put (uuid4, JSONObject.fromObject (user4). ToString ()); jedis.sadd (USER_TABLE_AGE_15,uuid4); String uuid5 = UUID.randomUUID (). ToString (); User user5 = new User (uuid5, "Y5", "m", 25) Map.put (uuid5, JSONObject.fromObject (user5). ToString ()); jedis.sadd (USER_TABLE_SEX_m,uuid5); / / put map in the cache jedis.hmset ("USER_TABLE", map)
So, if you want to query the user at the age of 15, you need to extract all the uuid from the USER_TABLE_AGE_15 and then the user from the USER_TABLE
/ / get uuid Set age = jedis.smembers (USER_TABLE_AGE_15) of user aged 15; / / get user List userJson = new ArrayList () according to uuid; for (Iterator iterator = age.iterator (); iterator.hasNext ();) {String string = (String) iterator.next (); String jsonStr = jedis.hget (USER_TABLE, string) JSONObject json = JSONObject.fromObject (jsonStr); User user = (User) JSONObject.toBean (json, User.class); userJson.add (user); System.out.println (user);}
The results are as follows:
User [id=63a970ec-e997-43e0-8ed9-14c5eb87de8b, name=y1, sex=m, age=15] User [id=aa074a2a-88d9-4b50-a99f-1375539164f7, name=y4, sex=n, age=15]
So if you need user with age 15 and sex m now, it's very simple to get
The union of USER_TABLE_AGE_15 and USER_TABLE_SEX_m, and then obtained from USER_TABLE.
/ / get userSet userSet = jedis.sinter (USER_TABLE_AGE_15,USER_TABLE_SEX_m) of age 15 and gender m; List users = new ArrayList (); for (Iterator iterator = userSet.iterator (); iterator.hasNext ();) {String string = (String) iterator.next (); String jsonStr = jedis.hget (USER_TABLE, string); JSONObject json = JSONObject.fromObject (jsonStr) User user = (User) JSONObject.toBean (json, User.class); users.add (user); System.out.println (user);} User [id=63a970ec-e997-43e0-8ed9-14c5eb87de8b, name=y1, sex=m, age=15]
The above is the introduction of the method of redis to achieve conditional query, the code example is simple and clear, if you encounter this problem in your daily work. Through this article, I hope you can get something. For more details, please follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.