Redisはsqlの条件クエリーを簡単に実現する
4880 ワード
redisのデータ型String、list、hashmap、set、sortedsetなどは、一般的にリレーショナル・データベースの柔軟な条件クエリーを実現するのは容易ではありません.簡単な紹介:すべてのuserをmapに入れてredisに保存し、条件に基づいてuserをクエリーする方法.User:ユーザーエンティティクラスJsonUtil:jsonとエンティティクラス間の変換TestRedis:redis条件クエリーテスト
package com.test.redis;
public class User {
private String name;
private int age;
private String sex;
private String id;
public User(){
super();
}
public User(String id, String name, int age, String sex){
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
// set get
}
import com.alibaba.fastjson.JSON;
/**
* fastjson json
* @author zhb
*
*/
public class JsonUtil {
/**
* json
* @param object
* @return
*/
public static String toJSONString(Object object){
if(object!=null){
return JSON.toJSONString(object);
}
return null;
}
/**
* json T
* @param
* @param jsonStr
* @return
*/
public static Object parseObject(String jsonStr, Class c){
if(jsonStr!=null){
return JSON.parseObject(jsonStr, c);
}
return null;
}
}
import redis.clients.jedis.Jedis;
/**
* redis (String、list、hashmap、set、sortedset) ,
*
* @author zhb
*
*/
public class TestRedis {
public static void main(String[] args) {
// redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
// user map,user id key user json value
Map map = new HashMap();
String id1 = UUID.randomUUID().toString();
User user1 = new User(id1, " ", 22 ,"m");
map.put(id1, JsonUtil.toJSONString(user1));
String id2 = UUID.randomUUID().toString();
User user2 = new User(id2, " ", 23 ,"m");
map.put(id2, JsonUtil.toJSONString(user2));
String id3 = UUID.randomUUID().toString();
User user3 = new User(id3, " ", 26 ,"w");
map.put(id3, JsonUtil.toJSONString(user3));
String id4 = UUID.randomUUID().toString();
User user4 = new User(id4, " ", 25 ,"w");
map.put(id4, JsonUtil.toJSONString(user4));
// user map redis key user
jedis.hmset("user", map);
// id3 user
String u = jedis.hget("user", id3);
// key user map, map
Map map1 = jedis.hgetAll("user");
// for (Entry entry : map1.entrySet()) {
// System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
// }
// user user id
// redis ,
// select * from user where age between 20 and 25 and sex m; 20 25
// redis , hashmap、set sortedset
//
// user , id sortedset , w m set
// key = user_age sortedset key , score; id member
String user_age = "user_age";
jedis.zadd(user_age, 22, id1);
jedis.zadd(user_age, 23, id2);
jedis.zadd(user_age, 26, id3);
jedis.zadd(user_age, 25, id4);
// 20 25 id
Set ageSet = jedis.zrangeByScore(user_age, 20, 25);
// 20 25 user
List hmget = jedis.hmget("user", (String[])ageSet.toArray(new String[ageSet.size()]));
for (String string : hmget) {
User user = (User) JsonUtil.parseObject(string, User.class);
System.out.println(" 20 25 user :"+user.getName());
}
// user_sex_m set key ,id member
String user_sex_m = "user_sex_m";
jedis.sadd(user_sex_m, id1, id2);
// user_sex_w set key ,id member
String user_sex_w = "user_sex_w";
jedis.sadd(user_sex_w, id3);
jedis.sadd(user_sex_w, id4);
// id
Set user_sex_wStr = jedis.smembers(user_sex_w);
// user
List hh = jedis.hmget("user", (String[])user_sex_wStr.toArray(new String[user_sex_wStr.size()]));
for (String string : hh) {
User user = (User) JsonUtil.parseObject(string, User.class);
System.out.println(" :"+user.getName());
}
}
}