import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by user on 2015/12/8.
*/
public class RedisTestForHash {
private Jedis jedis = new Jedis("10.19.16.31",6379);
public void test(){
jedis.hset("redis","zj","a");
//hash集合設置 key sub-key value
jedis.hset("redis","zj","b");
jedis.hset("redis","zj","c");
jedis.hset("redis","ww","c");
jedis.hset("redis","ww","b");
jedis.hset("redis", "ww", "a");
//不存在的時候設置值
jedis.hsetnx("redis", "ls", "a");
jedis.hsetnx("redis", "ls", "b");
String a = jedis.hget("redis","ls");
System.out.println(a);
//獲取hash集合主鍵值爲redis的分鍵值
Set<String> strs = jedis.hkeys("redis");
System.out.println(strs);
//獲取hash集合主鍵值爲redis的分值
List<String> strs1 = jedis.hvals("redis");
//獲取全部主鍵redis的分鍵值和對應的值
Map<String,String> map = jedis.hgetAll("redis");
System.out.println(map);
}
public static void main(String[] args){
RedisTestForHash redisTestForHash = new RedisTestForHash();
redisTestForHash.test();
}
}
java