非关系型数据库redis-java基本操作
概述
redis 是一个 key-value 的 nosql 数据库 (非关系型数据库)。支持存储的 value 类型包括 string(字符串)、list(链表)、set(集合)、zset(sorted set -- 有序集合) 和 hash(哈希类型)。这些数据类型都支持 push/pop、add/remove 及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。为了保证效率,数据都是缓存在内存中。
下载 Windows 版 Redis
Redis 官网不支持 Windows,可以去 Github 上下载,可以直接下载 zip 解压即可。
Redis 客户端软件:https://redisdesktop.com/
指定配置启动,可以在 conf 文件中配置密码等选项。
./redis-server.exe redis.windows.conf
启动后可以看到端口和 PID,当然也可以在后台启动。
用 redis-cli.exe -h 127.0.0.1 -p 6379 连接 redis 服务。
redis 执行语句
redis 的命令:各种命令在这里
包下载
关于 java 操作 redis 的 jar 包可以使用Jedis, 当然下载这个是要 FQ 的啦,FQ 的话同志们可以去买各种 vpn, 也可以自己搭,自己搭建 VPN 服务器的话看这里, 都嫌麻烦可以用 lantern. 还嫌麻烦的话度娘那也有.
代码
redis 因为是非关系型数据库, 所以没有表的概念, 而且也没有 sql 语句.
public class RedisJava { Jedis jedis = new Jedis("222.222.221.137",6379); //连接本地的 Redis 服务 public static void main(String[] args) { RedisJava redisJava = new RedisJava(); System.out.println("服务正在运行:"+redisJava.jedis.ping()); redisJava.save("sk1", "-value1"); redisJava.update("sk1", "sk111"); redisJava.findByKey("sk111"); redisJava.del("sk111"); redisJava.findAll(); redisJava.list(); redisJava.map(); redisJava.set();
redisJava.jedis.disconnect();}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加数据(key/value)</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(String key,String value){ String result </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.set(key, value); System.out.println(</span>"添加{"+key+":"+value+"},结果为"+<span style="color: rgba(0, 0, 0, 1)">result); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据Key查询数据</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String findByKey(String key){ String value </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.get(key); System.out.println(</span>"get到"+key+"的值为"+<span style="color: rgba(0, 0, 0, 1)">value); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> value; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改key</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> update(String oldKey,String newKey){ System.out.println(</span>"检测是否有key:"+oldKey+",结果为:"+<span style="color: rgba(0, 0, 0, 1)">jedis.exists(oldKey)); jedis.rename(oldKey, newKey); System.out.println(</span>"将key:"+oldKey+"重置为"+<span style="color: rgba(0, 0, 0, 1)">newKey); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据Key删除数据</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> del(String key) { Long result </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.del(key); System.out.println(</span>"删除的结果为:"+<span style="color: rgba(0, 0, 0, 1)">result); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询所有</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> findAll(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取keys数据并输出</span> Set<String> keys = jedis.keys("*"<span style="color: rgba(0, 0, 0, 1)">); Iterator</span><String> it=<span style="color: rgba(0, 0, 0, 1)">keys.iterator(); System.out.println(</span>"所有key:"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){ String key </span>=<span style="color: rgba(0, 0, 0, 1)"> it.next(); System.out.println(key); jedis.del(key); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除所有key</span>
}
}List<String> list = jedis.hmget("Mcity", "c1", "c2", "c3"); //一次获取多个 map-value for (String string : list) {System.out.println(string); } System.out.println("list 长度:"+jedis.hlen("Mcity"));}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作List</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> list() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存储数据到列表中,一条一条存</span> jedis.lpush("list", "FireFox"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"list", "Google"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"list", "IE"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从头添加,一次添加多条</span> jedis.lpush("list", "flash", "cisco", "catalin", "Google"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从尾添加</span> jedis.rpush("list", "walley", "jon"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回List长度</span> System.out.println("list长度为:"+jedis.llen("list"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 取值</span> System.out.println("所有list:"<span style="color: rgba(0, 0, 0, 1)">); List</span><String> list = jedis.lrange("list", 0, -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String string : list) { System.out.println(string); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作Map</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> map() { jedis.hset(</span>"Mcity", "c1", "上海"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">key,map-key,map-value</span> System.out.println("取值:" + jedis.hget("Mcity", "c1")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据key,map-key,获取mapvalue</span> Map<String, String> map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String, String><span style="color: rgba(0, 0, 0, 1)">(); map.put(</span>"c1", "徐州"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">相同的map-key会被覆盖</span> map.put("c2", "扬州"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"c3", "常州"<span style="color: rgba(0, 0, 0, 1)">); jedis.hmset(</span>"Mcity", map); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一次取多个map值</span>
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作Set</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> set() { jedis.sadd(</span>"city", "扬州", "上海", "南京", "苏州", "徐州", "常州","北京"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"取最上面的值:" + jedis.spop("city"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"随机取值:" + jedis.srandmember("city"<span style="color: rgba(0, 0, 0, 1)">)); jedis.sadd(</span>"city1", "北京", "上海", "河北", "辽林", "云南", "黑龙江"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"交集:" + jedis.sinter("city", "city1"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"并集:" + jedis.sunion("city", "city1"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"差集:" + jedis.sdiff("city", "city1"<span style="color: rgba(0, 0, 0, 1)">)); }
}