Redis数据库提供了多种数据结构,其中最常见的数据结构有String(字符串)、List(列表)、Set(集合)、Hash(散列)、Sorted Sets(有序集合)。
redis-cli操作
java操作
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestKeyOperate {
public static void main(String[] args) {
// 连接Redis数据库
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
Jedis jedis = jedisPool.getResource();
jedis.select(1);
/*1.操作列表*/
//operationList(jedis);
/*2.操作集合*/
//operationGather(jedis);
/*3.操作散列*/
//operationHash(jedis);
/*4.操作有序集合*/
operationSortedSets(jedis);
}
/*操作有序集合*/
private static void operationSortedSets(Jedis jedis) {
// ZADD:为有序集合添加一个或多个键值对
jedis.zadd("scores", 85, "Peter");
jedis.zadd("scores", 90, "Tom");
jedis.zadd("scores", 73, "Mary");
// ZCARD:获取有序集合中元素的个数
Long num = jedis.zcard("scores");
System.out.println("集合中元素个数:" + num);
// ZCOUNT:统计有序集合中指定分值范围内的元素个数
Long count = jedis.zcount("scores", 90, 100);
System.out.println("90~100元素个数:" + count);
// ZRANGE:获取有序集合中指定索引范围内的元素
Set<String> stringSet = jedis.zrange("scores", 0, 2);
for(String str : stringSet){
System.out.println(str);
}
// ZSCORE:获取有序集合中指定元素的分值
Double score = jedis.zscore("scores", "Tom");
System.out.println(score);
// ZREM:移除有序集合中的指定元素
Long result = jedis.zrem("scores", "Tom");
System.out.println(result);
Long num_new = jedis.zcard("scores");
System.out.println(num_new);
}
/*操作散列*/
private static void operationHash(Jedis jedis) {
// HSET:为散列中的指定键设置值
//jedis.hset("person", "name", "wwj");
// HMSET:为散列中多个键设置值
/*Map<String, String> hash = new HashMap<String, String>();
hash.put("age", "22");
hash.put("sex", "girl");
jedis.hmset("person",hash);*/
// HGET:获取散列中指定键的值
String name = jedis.hget("person", "name");
System.out.println(name);
// HMGET:获取散列中多个键的值
List<String> ageAndSex = jedis.hmget("person", "age", "sex");
System.out.println(ageAndSex.toString());
// HGETALL:获取散列中的所有键值对
Map<String, String> person = jedis.hgetAll("person");
for (Map.Entry<String, String> entry : person.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
// HKEYS:获取散列中的所有键
Set<String> keys = jedis.hkeys("person");
for(String str : keys){
System.out.println(str);
}
// HVALS:获取散列中的所有键的值
List<String> vals = jedis.hvals("person");
for(String str : vals){
System.out.println(str);
}
// HDEL:删除散列中指定键及其相对应的值
Long result = jedis.hdel("person", "age");
System.out.println(result);
}
/*操作集合*/
private static void operationGather(Jedis jedis) {
// SADD:将一个或多个元素添加到集合中
jedis.sadd("vehicles", "car", "bus", "airplane", "boat");
// SCARD:获取集合中的元素数量
Long num = jedis.scard("vehicles");
System.out.println(num);
// SMEMBERS:获取集合中的所有元素
Set<String> vehicles = jedis.smembers("vehicles");
System.out.println(vehicles.toString());
// SISMEMBER:检查指定元素是否存在于集合中
Boolean flag = jedis.sismember("vehicles", "car");
System.out.println(flag);
// SREM:移除集合中的一个或多个已存在的元素
jedis.srem("vehicle", "bus");
jedis.sadd("vehiclesNew", "subway");
// SMOVE:将元素从一个集合移动到另一个集合
jedis.smove("vehicle", "vehiclesNew", "airplane");
}
/*操作列表*/
private static void operationList(Jedis jedis) {
// LPUSH:将一个或多个元素推入到列表的右端
// LRANGE:获取列表指定索引范围内的元素
/*jedis.lpush("fruits", "apple", "pear", "orange");
List<String> fruits = jedis.lrange("fruits", 0, -1);
for(String str : fruits){
System.out.println(str);
}*/
// LINDEX:获取列表指定索引位置上的元素
String fruits = jedis.lindex("fruits", 2);
System.out.println(fruits);
// RPOP:移除列表最右端的元素
jedis.lpop("fruits");
// LPOP:移除列表最左端的元素
jedis.rpop("fruits");
// LLEN:获取列表中值的长度,即元素个数
jedis.llen("fruits");
// LREM:移除列表中的指定元素
jedis.lrem("fruits", 1, "apple");
}
}