Redis入門のredis対hashの操作

11525 ワード

    
	/**
	 *  {@link #test() test}
	 * jedis   hash     
	 * @author jackson
	 * @date 2015-12-17   2:48:30
	 * @return void
	 */
	@SuppressWarnings("unchecked")
	@Test
	public void TestJedisHash(){
		// hset  hget
		jedis.hset("hsetkey", "hashKey", "hashValue");//    key    field     value 。  key    ,             HSET   。   field          ,      。
		String hash = jedis.hget("hsetkey", "hashKey");//     key     field   
		System.out.println("   hset hget : hsetkey     :"+hash);
		
		//hsetnx       field    。  field(      )     ,     。 
		long n = jedis.hsetnx("hsetkeynx", "hashkeynx", "hashvaluenx");
		System.out.println(n!=0?"    ":"    ");
		n = jedis.hsetnx("hsetkeynx", "hashkey", "hashvaluenx");
		System.out.println(n!=0?"    ":"    ");
		n = jedis.hsetnx("hsetkeynx", "hashkey", "hashvaluenx");
		System.out.println(n!=0?"    ":"    ");
		
		//hmset hmget
		HashMap hashMap = new HashMap();
		hashMap.put("hashMap1", "hashValue1");
		hashMap.put("hashMap2", "hashValue2");
		hashMap.put("hashMap3", "hashValue3");
		hashMap.put("hashMap4", "hashValue4");
		String status  = jedis.hmset("hashMapkey", hashMap);//        ,  OK 。 key      (hash)    ,      。
		hash = jedis.hget("hashMapkey", "hashMap4");
		System.out.println("OK".equals(status)?"         :"+hash:"    ");
		//   :                ,                    
		List hashList = jedis.hmget("hashMapkey", "hashMap1 hashMap2 hashMap3 hashMap4".split(" "));
		for(String value : hashList){
			System.out.print("   value :  "+value+" ");//   :                ,                    
		}
		System.out.println();
		
		//hgetall      Map   key  file 
		Map hashMapKey = jedis.hgetAll("hashMapkey");
		
		// map         
		Set> entry = hashMapKey.entrySet();
		Iterator> it = entry.iterator();
		while(it.hasNext()){
			Map.Entry e  = it.next();
			System.out.println("key: "+e.getKey()+"  value: "+e.getValue());
		}
		
		// map        
		Set keySet = hashMapKey.keySet();// map    key set    ,      set       key
		Iterator iter = keySet.iterator();
		while(iter.hasNext()){
			String key = iter.next();
			String value = hashMapKey.get(key);
		}
		

		//hscan      scan      key             file-value  map    ;
		ScanResult> hscanResult = jedis.hscan("hashMapkey", "0");
		String cursor = hscanResult.getCursor(); //   0       
		System.out.println("  "+cursor);
		List> scanResult = hscanResult.getResult();
		for(int m = 0;m < scanResult.size();m++){
			Map.Entry mapentry  = scanResult.get(m);
			System.out.println("key: "+mapentry.getKey()+"  value: "+mapentry.getValue());
		}
		
		//hkeys
		Set setKey = jedis.hkeys("hashMapkey");// keys       key  ,hkeys    key         
		Iterator itset = setKey.iterator();
		String files = "";
		while(itset.hasNext()){
			files =files+" "+itset.next();
		}
		System.out.println("hashMapkey        :"+files);
		
		//hvals      key       。    : >= 2.0.0     : O(N),N        。   :             。 key     ,      。
		List list = jedis.hvals("hashMapkey");
		for(String s : list){
			System.out.println(s);
		}
		
		//          String           list
		Map> testMapList = new HashMap>();
		List testList = Arrays.asList("testList testList testList testList testList testList testList ");
		List testList1 = Arrays.asList("testList1 testList1 testList1 testList1 testList1 testList1 testList1 ");
		List testList2 = Arrays.asList("testList2 testList2 testList2 testList2 testList2 testList2 testList2 ");
		testMapList.put("testList", testList);
		testMapList.put("testList1", testList1);
		testMapList.put("testList2", testList2);
		String mapString  =  JSON.toJSONString(testMapList,true);// map   json 
		jedis.set("hashMapkey2", mapString);
		mapString = jedis.get("hashMapkey2");
//		System.out.println(mapString);  
		testMapList = (Map>)JSON.parse(mapString);
		Set>> mapListSet = testMapList.entrySet();
		Iterator>> maplistIter = mapListSet.iterator();
		while(maplistIter.hasNext()){
			Map.Entry> mapentryList = maplistIter.next();
			String key = mapentryList.getKey();
			List entryList = mapentryList.getValue();
			System.out.println("testMapList key: "+key+"testMapList value: "+entryList.toString());
		}
		// Map         
		Map testMapEntity = new HashMap();
		Bar bar = new Bar();bar.setColor("red");bar.setName("lvxiaojian");
		Bar bar1 = new Bar();bar.setColor("green");bar.setName("wagnbo");
		testMapEntity.put("bar", bar);
		testMapEntity.put("bar1", bar1);
		String entityString  =  JSON.toJSONString(testMapEntity,true);// map   json 
		jedis.set("hashMapkey3", entityString);
		entityString = jedis.get("hashMapkey3");
		testMapEntity = (Map)JSON.parse(entityString);
		Set entitySet = testMapEntity.keySet();
		Iterator iterentity = entitySet.iterator();
		while(iterentity.hasNext()){
			System.out.println("testMapEntity key: "+iterentity.next()+"testMapEntity value: "+testMapEntity.get(iterentity.next()));
		}
		
		
		//hlen     :        。 key     ,  0 。
		n = jedis.hlen("hashMapkey");
		System.out.println("hashMapkey       : "+n);
		
		//hdel     :           ,        
		n = jedis.hdel("hashMapkey","hashMap1 hashMap2 hashMap3 hashMap4".split(" "));
		System.out.println("          ,        : "+n);
		
		//hexists     :          ,  1 。           , key    ,  0 。
		boolean flag = jedis.hexists("hashMapkey", "hashMap1");
		System.out.println(flag?"        ":"         ");
		
		hashMap.clear();//   map
		hashMap.put("hashMap1", "1");
		hashMap.put("hashMap2", "2");
		hashMap.put("hashMap3", "3");
		hashMap.put("hashMap4", "4");
		hashMap.put("hashMap5", "5");
		hashMap.put("hashMap6", "6");
		jedis.hmset("hashMapkey", hashMap);
		flag = jedis.hexists("hashMapkey", "hashMap1");
		System.out.println(flag?"        ":"         ");
		
		//hincrBy  key                 :   HINCRBY     ,   key   field   
		System.out.println("  hash  key  hashMapkey   hashMap1         1      :"+jedis.hget("hashMapkey", "hashMap1"));//    :  hash  key  hashMapkey   hashMap1         1      :1
		n = jedis.hincrBy("hashMapkey", "hashMap1", -1); //   hash  key  hashMapkey   hashMap1        1
		System.out.println("  hash  key  hashMapkey   hashMap1        1    :"+n);//    :  hash  key  hashMapkey   hashMap1        1    :0
		
		System.out.println("  hash  key  hashMapkey   hashMap2        2      :"+jedis.hget("hashMapkey", "hashMap2"));//   :  hash  key  hashMapkey   hashMap2        2      :2
		n = jedis.hincrBy("hashMapkey", "hashMap2", 2); //   hash  key  hashMapkey   hashMap2        2
		System.out.println("  hash  key  hashMapkey   hashMap2        2    :"+n);//   :  hash  key  hashMapkey   hashMap2        2    :4
		
		// key                     :         ,        ,                     0        
		System.out.println("  hash  key  hashMapkey   hashMap7         1      :"+jedis.hget("hashMapkey", "hashMap7"));//   :  hash  key  hashMapkey   hashMap7         1      :null
		n = jedis.hincrBy("hashMapkey", "hashMap7", -1); //   hash  key  hashMapkey   hashMap1        1
		System.out.println("  hash  key  hashMapkey   hashMap7       1    :"+n);//   :  hash  key  hashMapkey   hashMap7       1    :-1
		
		System.out.println("  hash  key  hashMapkey   hashMap8        2      :"+jedis.hget("hashMapkey", "hashMap8"));//   :  hash  key  hashMapkey   hashMap8        2      :null
		n = jedis.hincrBy("hashMapkey", "hashMap8", 2); //   hash  key  hashMapkey   hashMap2        2
		System.out.println("  hash  key  hashMapkey   hashMap8        2    :"+n);//  hash  key  hashMapkey   hashMap8        2    :2
		
		//key                        0     set   ,    hincrby   
		System.out.println("  hash  key  hashMapkey1   hashMap7         1      :"+jedis.hget("hashMapkey1", "hashMap7"));//   :  hash  key  hashMapkey   hashMap7         1      :null
		n = jedis.hincrBy("hashMapkey1", "hashMap7", -1); //   hash  key  hashMapkey   hashMap1        1
		System.out.println("  hash  key  hashMapkey   hashMap7       1    :"+n);//   :  hash  key  hashMapkey   hashMap7       1    :-1
		
		System.out.println("  hash  key  hashMapkey   hashMap8        2      :"+jedis.hget("hashMapkey1", "hashMap8"));//   :  hash  key  hashMapkey   hashMap8        2      :null
		n = jedis.hincrBy("hashMapkey1", "hashMap8", 2); //   hash  key  hashMapkey   hashMap2        2
		System.out.println("  hash  key  hashMapkey1   hashMap8        2    :"+n);//  hash  key  hashMapkey   hashMap8        2    :2
		
		//incrbyfloat       incrby         double     
	}

   
package redis.clients.jedis.tests;


public class Bar {


	private String name;
	private String color;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}

fastJson Maven :

	com.alibaba
	fastjson
	1.2.4


 maven  :      :fastjson-1.2.6.jar