Java操作redis很简单。创建一个普通的Java工程。然后导入redis依赖的jar包。jar包我已经上传了。
1.入门级的demo
- package com.bjsxt.test;
- import redis.clients.jedis.Jedis;
- public class RedisDemo {
- public static Jedis getJedis(){
- Jedis jedis = null;
- jedis = new Jedis(FinalCollention.IP, 6379); //redis服务器的ip和端口
- jedis.auth(FinalCollention.PASSWORD); //连接redis的密码
- return jedis;
- }
- public void test(){
- Jedis jedis = null;
- try{
- jedis = getJedis();
- jedis.incr("test");
- long dbsize = jedis.dbSize();
- System.out.println("数据库大小:"+dbsize);
- System.out.println("测试test的值:"+jedis.get("test"));
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(jedis != null){
- jedis.disconnect();
- }
- }
- }
- public static void main(String[] args) {
- RedisDemo demo = new RedisDemo();
- demo.test();
- }
- }
如果出现Java无法连接redis的异常,主要有三个方面的问题:
1.Java运行的主机ping一下redis服务器所在主机,看能否ping通
2.关闭redis服务器所在主机的防火墙
3.打开redis安装目录下的redis.conf文件。注释掉:bind 127.0.0.1这句话
- # Accept connections on the specified port, default is 6379.
- # If port 0 is specified Redis will not listen on a TCP socket.
- port 6379
- # If you want you can bind a single interface, if the bind option is not
- # specified all the interfaces will listen for incoming connections.
- #
- # bind 127.0.0.1
- "redis.conf" 597L, 25667C written
特别说明:后面的实例,我就不使用try--catch了,但是自己写时一定要记着关闭连接
2.Java操作redis的各种数据结构
- package com.bjsxt.test;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import redis.clients.jedis.Jedis;
- public class RedisDataTypeDemo {
- public static Jedis getJedis(){
- Jedis jedis = null;
- jedis = new Jedis(FinalCollention.IP, 6379); //redis服务器的ip和端口
- jedis.auth(FinalCollention.PASSWORD); //连接redis的密码
- return jedis;
- }
- /**
- *
- * @title demoKey
- * @description 测试Key
- * @author hadoop
- */
- public void demoKey(){
- Jedis jedis = RedisDataTypeDemo.getJedis();
- System.out.println("查看键为aa的是否存在:"+jedis.exists("aa"));
- System.out.println("查看键为aa的类型:"+jedis.type("aa"));
- System.out.println("随机获得一个key:"+jedis.randomKey());
- System.out.println("将aa重命名为bb:"+jedis.rename("aa", "bb"));
- System.out.println("删除bb:"+jedis.del("bb"));
- //从上面的方法可以看出,redis提供的接口方法名和redis-cli客户端提供的方法名是一样的,所以,只要对redis客户端的命令熟悉,那么Java操作redis就不是问题
- }
- /**
- *
- * @title demoString
- * @description 测试String
- * @author hadoop
- */
- public void demoString(){
- Jedis jedis = RedisDataTypeDemo.getJedis();
- System.out.println("设置name:"+jedis.set("name", "小花"));
- System.out.println("设置name1:"+jedis.set("name1", "小花1"));
- System.out.println("设置那么2:"+jedis.set("name2", "小花2"));
- System.out.println("设置name,如果存在返回0:"+jedis.setnx("name", "小花哈哈"));
- System.out.println("获取key为name和name1的value值:"+jedis.mget("name","name1"));
- System.out.println("自增1:"+jedis.incr("index"));
- System.out.println("自增1:"+jedis.incr("index"));
- System.out.println("自增2:"+jedis.incrBy("count", 2));
- System.out.println("自增2:"+jedis.incrBy("count", 2));
- System.out.println("递减1:"+jedis.decr("count"));
- System.out.println("递减2:"+jedis.decrBy("index",2));
- System.out.println("在name后面添加String:"+jedis.append("name", ",我爱你"));
- System.out.println("获取key为name的值:"+jedis.get("name"));
- }
- /**
- *
- * @title demoList
- * @description 测试list数据类型
- * @author hadoop
- */
- public void demoList(){
- Jedis jedis = RedisDataTypeDemo.getJedis();
- //在头添加
- jedis.lpush("list", "hadoop","hbase","hive","pig","zookeeper");
- //在末尾添加
- jedis.rpush("list", "java","struts");
- //返回长度
- jedis.llen("list");
- //取值
- List<String> list = jedis.lrange("list", 0, -1);
- for (String string : list) {
- System.out.println(string);
- }
- }
- /**
- *
- * @title demoSet
- * @description 测试set数据类型
- * @author hadoop
- */
- public void demoSet(){
- Jedis jedis = RedisDataTypeDemo.getJedis();
- jedis.sadd("city", "北京","上海","重启","武汉");
- System.out.println("取出最上面的:"+jedis.spop("city"));
- System.out.println("随机取出一个值"+jedis.srandmember("city"));
- jedis.sadd("city2", "北京","武汉","河北","张家界","辽林");
- System.out.println("交集:"+jedis.sinter("city","city1"));
- System.out.println("并集:"+jedis.sunion("city","city1"));
- System.out.println("差集:"+jedis.sdiff("city","city1"));
- }
- /**
- *
- * @title demoMap
- * @description 测试map数据类型
- * @author hadoop
- */
- public void demoMap(){
- Jedis jedis = RedisDataTypeDemo.getJedis();
- jedis.hset("Mcity", "c1", "北京");
- System.out.println("取值:"+jedis.hget("city", "c1"));
- Map<String,String> mcity = new HashMap<String, String>();
- mcity.put("c1", "桂林");
- mcity.put("c2", "天津");
- mcity.put("c3", "合肥");
- jedis.hmset("Mcity2", mcity);
- List<String> list = jedis.hmget("Mcity2", "c1","c2","c3");
- for (String string : list) {
- System.out.println(string);
- }
- System.out.println(jedis.hlen("Mcity2"));
- }
- //测试
- public static void main(String[] args) {
- RedisDataTypeDemo rdr = new RedisDataTypeDemo();
- rdr.demoKey();
- rdr.demoList();
- rdr.demoMap();
- rdr.demoSet();
- rdr.demoString();
- }
- }
3.Java操作redis的管道
- package com.bjsxt.test;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.Pipeline;
- public class RedisPipelineDemo {
- public static Jedis getJedis(){
- Jedis jedis = null;
- jedis = new Jedis(FinalCollention.IP, 6379); //redis服务器的ip和端口
- jedis.auth(FinalCollention.PASSWORD); //连接redis的密码
- return jedis;
- }
- public void hasPipeline(){
- Jedis jedis = RedisPipelineDemo.getJedis();
- System.out.println("开始:"+System.currentTimeMillis());
- Pipeline pl = jedis.pipelined();
- for(int i = 0 ; i < 1000 ; i++){
- pl.incr("testPL");
- }
- pl.sync();
- System.out.println("开始:"+System.currentTimeMillis());
- }
- public void noPipeline(){
- Jedis jedis = RedisPipelineDemo.getJedis();
- System.out.println("开始:"+System.currentTimeMillis());
- for(int i = 0 ; i < 1000 ; i++){
- jedis.incr("testPL");
- }
- System.out.println("开始:"+System.currentTimeMillis());
- }
- }
4.redis事务
- package com.bjsxt.test;
- import java.util.List;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.Transaction;
- public class RedisTXDemo {
- public static Jedis getJedis(){
- Jedis jedis = null;
- jedis = new Jedis(FinalCollention.IP, 6379); //redis服务器的ip和端口
- jedis.auth(FinalCollention.PASSWORD); //连接redis的密码
- return jedis;
- }
- /**
- *
- * @title txDemo
- * @description 没有异常的事务
- * @author hadoop
- */
- public void txDemo(){
- Jedis jedis = getJedis();
- //开启事务
- Transaction tx = jedis.multi();
- tx.set("txcity", "beijin");
- tx.sadd("hi", "abc");
- tx.get("txcity");
- List<Object> objs = tx.exec();
- for (Object object : objs) {
- System.out.println(object.toString());
- }
- }
- /**
- *
- * @title txHasExDemo
- * @description 含有异常的事务
- * @author hadoop
- */
- public void txHasExDemo(){
- Jedis jedis = getJedis();
- //开启事务
- Transaction tx = jedis.multi();
- tx.set("txcity1", "beijin");
- tx.sadd("hi1", "abc");
- int i = 1/0;
- tx.get("txcity1");
- List<Object> objs = tx.exec();
- for (Object object : objs) {
- System.out.println(object.toString());
- }
- }
- public static void main(String[] args) {
- RedisTXDemo redisTXDemo = new RedisTXDemo();
- redisTXDemo.txDemo();
- redisTXDemo.txHasExDemo();
- }
- }