Java鏈接Redis測試

用eclipse新建一個Maven工程,在pom.xml文件裏面,引入redis和junit的依賴。java

<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。mysql

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"));
    }
}
相關文章
相關標籤/搜索