Java连接Redis测试

用eclipse新建一个Maven工程,在pom.xml文件里面,引入redis和junit的依赖。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.comtop.cn</groupId>
  <artifactId>JavaRedis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>JavaRedis</name>
  <description>java连接redis例子</description>
  
   <dependencies>
      <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
	  </dependency>
      <dependency>
	    <groupId>redis.clients</groupId>
	    <artifactId>jedis</artifactId>
	    <version>2.9.0</version>
	  </dependency>
   </dependencies>
</project>

新建一个测试类,测试redis字符串、List列表、哈希(hash)、set、sorted set、HyperLogLog。

public class JavaRedisTest {
    
    Jedis jedis;
    @Before
    public void SetUp() throws Exception {
        jedis=new Jedis("localhost");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("运行结束");
    }
    /**
     * 连接redis服务器
     */
    @Test
    public void pingTest(){
        System.out.println("连接成功");
        System.out.println("服务正在运行:"+jedis.ping());
    }
    /**
     * 字符串实例
     */
    @Test
    public void setKeyTest(){
        jedis.set("cz", "cz");
        System.out.println("redis 存储的字符串为:"+jedis.get("cz"));
    }
    
    /**
     * List列表实例
     */
    @Test
    public void listTest(){
        jedis.lpush("url", "www.baidu.com");
        jedis.lpush("url", "www.2345.com");
        jedis.lpush("url", "www.sina.com.cn");
        jedis.lpush("url", "www.qq.com");
        List<String> urlList=jedis.lrange("url", 0, 10);
        for(int i=0;i<urlList.size();i++){
            System.out.println("列表项为:"+urlList.get(i));
        }
    }
    
    /**
     * 测试hash
     */
    @Test
    public void hashTest(){
        Map<String,String> redisMap=new HashMap<String,String>();
        redisMap.put("userName", "张三");
        redisMap.put("age", "25");
        redisMap.put("sex", "男");
        jedis.hmset("redisMap", redisMap);
        Map<String,String> resultMap=jedis.hgetAll("redisMap");
        for(String key:resultMap.keySet()){
            System.out.println("Key="+key+",Value="+resultMap.get(key));
        }
    }
    
    /**
     * 测试Set
     * 无序Set集合
     */
    @Test
    public void setTest(){
        jedis.sadd("db", "redis");
        jedis.sadd("db", "mongdb");
        jedis.sadd("db", "oracle");
        jedis.sadd("db", "mysql");
        jedis.sadd("db", "greenplum");
        Set<String> dbSet=jedis.smembers("db");
        for(String db:dbSet ){
            System.out.println("set 成员有:"+db);
        }
    }
    
    /**
     * 测试sorted set
     * 有序set集合
     */
    @Test
    public void zSetTest(){
        jedis.zadd("dbs", 0, "redis");
        jedis.zadd("dbs", 1, "mongdb");
        jedis.zadd("dbs", 2, "oracle");
        jedis.zadd("dbs", 3, "mysql");
        jedis.zadd("dbs", 4, "greenplum");
        Set<String> dbSet=jedis.zrange("dbs", 0, 5);
        for(String db:dbSet ){
            System.out.println("有序set 成员有:"+db);
        }
    }
    
    /**
     * 测试HyperLogLog
     * Redis HyperLogLog 是用来做基数统计的算法,
     * HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定 的、并且是很小的。
     */
    @Test
    public void hyperLogLogTest(){
        jedis.pfadd("count", "one");
        jedis.pfadd("count", "two");
        jedis.pfadd("count", "three");
        jedis.pfadd("count", "four");
        jedis.pfadd("count", "five");
        System.out.println("HyperLogLog 的基数估算值为:"+jedis.pfcount("count"));
    }
}

 

转载于:https://my.oschina.net/u/3822522/blog/1786230

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值