import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.ScanOptions; import org.springframework.data.redis.core.ScanOptions.ScanOptionsBuilder; public class TestRedisHash { private static JedisConnectionFactory getJedisConnectionFactory() { JedisConnectionFactory jcf = new JedisConnectionFactory(); jcf.setHostName("127.0.0.1"); jcf.setPort(6379); jcf.setPassword("123"); jcf.afterPropertiesSet(); return jcf; } private static RedisConnection getRedisConnection() { return getJedisConnectionFactory().getConnection(); } public static void main(String[] args) { RedisConnection rc = getRedisConnection(); //HSET boolean hSet = rc.hSet("set1".getBytes(), "a".getBytes(), "b".getBytes()); System.out.println(hSet); //HEXISTS boolean hExists = rc.hExists("set".getBytes(), "a".getBytes()); System.out.println(hExists); //HGET byte[] hGet = rc.hGet("set".getBytes(), "a".getBytes()); System.out.println(new String(hGet)); //HGETALL Map<byte[], byte[]> hGetAll = rc.hGetAll("set".getBytes()); Set<Entry<byte[], byte[]>> set = hGetAll.entrySet(); Iterator<Entry<byte[], byte[]>> ite = set.iterator(); while (ite.hasNext()) { Entry<byte[], byte[]> entry = ite.next(); System.out.println(new String(entry.getKey()) + " " + new String(entry.getKey())); } Set<byte[]> keySet = hGetAll.keySet(); Iterator<byte[]> iter = keySet.iterator(); while (iter.hasNext()) { byte[] key = iter.next(); System.out.println(new String(key) + " " + new String(hGetAll.get(key))); } //HKEYS Set<byte[]> hKeys = rc.hKeys("books".getBytes()); Iterator<byte[]> itrKeys = hKeys.iterator(); while (itrKeys.hasNext()) { System.out.println(new String(itrKeys.next())); } //HVALS List<byte[]> hVals = rc.hVals("books".getBytes()); for (byte[] value : hVals) { System.out.println(new String(value)); } //HDEL long hDel = rc.hDel("testdel".getBytes(), "a".getBytes()); System.out.println(hDel); //HINCRBY long hIncrBy = rc.hIncrBy("number".getBytes(), "b".getBytes(), 2); System.out.println(hIncrBy); //HINCRBYFLOAT double hIncrByFloat = rc.hIncrBy("dounum".getBytes(), "b".getBytes(), 2.0e2); System.out.println(hIncrByFloat); //HLEN long hLen = rc.hLen("seft".getBytes()); System.out.println(hLen); //HMSET Map<byte[], byte[]> books = new HashMap<byte[], byte[]>(); books.put("001".getBytes(), "Redis".getBytes()); books.put("002".getBytes(), "Mongodb".getBytes()); books.put("003".getBytes(), "Mysql".getBytes()); rc.hMSet("books".getBytes(), books); //HMGET List<byte[]> hMget = rc.hMGet("books".getBytes(), "003".getBytes(), "002".getBytes(), "aa".getBytes()); for (byte[] value : hMget) { if (value != null) { System.out.println(new String(value)); } } //HSETNX boolean hSetNX = rc.hSetNX("setnx".getBytes(), "hello".getBytes(), "world".getBytes()); System.out.println(hSetNX); //HSCAN ScanOptionsBuilder sob = ScanOptions.scanOptions().count(1).match("*1"); ScanOptions options = sob.build(); options.getCount(); Cursor<Entry<byte[], byte[]>> hScan = rc.hScan("books".getBytes(), options); while (hScan.hasNext()) { Entry<byte[], byte[]> entry = hScan.next(); System.out.println(new String(entry.getKey()) + " " + new String(entry.getValue())); } } }
学习 Redis Hash(哈希表)
猜你喜欢
转载自injavawetrust.iteye.com/blog/2296040
今日推荐
周排行