您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

redis实现简单的条件查询

2024/4/8 21:38:07发布13次查看
一、导入jar包
二、实现简单的条件查询
创建一个user实体类
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 + "]"; }}
创建5个对象并将其存入缓存中,以便我们进行测试
//连接redis jedis jedis = new jedis("127.0.0.1",6379); map<string, string> map = new hashmap<string,string>(); final string user_table = "user_table"; //向缓存中存入5条数据组成的map string uuid1 = uuid.randomuuid().tostring(); user user1 = new user(uuid1, "y1", "m", 15); //将对象转为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()); //把map存到缓存中 jedis.hmset("user_table", map);
在redis中查询,可以看到已经将5个user对象存到缓存中
接下来,首先实现单条件的查询,比如说查询年龄为15的user和性别为m的user
由于redis是nosql,无法直接像mysql那样利用where进行条件查询,所以redis想实现条件查询,只能用一个笨方法:将所有符合条件的user存到一个set中。
jedis jedis = new jedis("127.0.0.1",6379); map<string, string> map = new hashmap<string,string>(); final string user_table = "user_table"; //查询年龄为15,性别为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"; //向缓存中存入5条数据组成的map string uuid1 = uuid.randomuuid().tostring(); user user1 = new user(uuid1, "y1", "m", 15); //将对象转为json map.put(uuid1, jsonobject.fromobject(user1).tostring()); //将符合条件的user的id存到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); //把map存到缓存中 jedis.hmset("user_table", map);
那么,如果想要查询年龄为15的user,就需要先从user_table_age_15中取出所有的uuid,然后再从user_table中取出user
//获取年龄为15的user的uuid set<string> age = jedis.smembers(user_table_age_15); //根据uuid获取user list<user> userjson = new arraylist<user>(); 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); }
结果如下:
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]
那么如果现在需要年龄为15,性别为m的user呢,很简单,获取
user_table_age_15 和 user_table_sex_m的并集,再从user_table中获取.
//获取年龄为15并性别为m的user set<string> userset = jedis.sinter(user_table_age_15,user_table_sex_m); list<user> users = new arraylist<user>(); 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]
更多redis知识请关注redis入门教程栏目。
以上就是redis实现简单的条件查询的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product