Example of using Java to connect to Redis
Redis is an open source Key-value storage tool, and Redis is typically used to store structured data because the Redis key can contain string, hash, listset, and sorted lists.
Redisserver currently the most stable version is 2.8.9, can be downloaded to the official website Http://redis.io/download. Depending on the type and number of bits of the machine, download the corresponding version to install, Reids support Linux and the Windows operating system.
Redisclient supports a variety of languages, including: C, C + +, C #, PHP, Java, Python, go and other languages, according to their own development language, select the appropriate Redis client version type. I developed it in the Java language, and for the Java language, the Redis client also offers a variety of support for clients, followed by the recommended types: Jedis, Redisson, Jredis, Jdbc-redis, RJC, Redis-protocol, Aredis, lettuce. The first two types are more recommended, and we use the Redisson type version as the redisclient.
Redisson Source project location: Https://github.com/mrniko/redisson. Here are examples of use and some introductions, which are no longer described in detail here.
Redis Java Client Jedis tool class
Import Java.util.arraylist;import java.util.hashmap;import Java.util.hashset;import Java.util.iterator;import Java.util.linkedhashset;import Java.util.list;import Java.util.map;import Java.util.set;import Redis.clients.jedis.builder;import Redis.clients.jedis.shardedjedis;import Redis.clients.jedis.shardedjedispipeline;import Redis.clients.jedis.shardedjedispool;import redis.clients.util.safeencoder;/** * The Redisutil represents * @version $Id $ * @author FENGJC */public class Redisutil {/ * * Data source */private shardedjedispool shardedjedispool;/** ======================================strings============== ======================== *//** * Set The string value as value of the key. The string can ' t be longer than * 1073741824 bytes (1 GB). * Time Complexity:o (1) * @param key * @param value * @return Status code reply */public string setString (string key, Stri Ng value) {Shardedjedis Jedis = This.shardedJedisPool.getResource (); String status = Jedis.set (key, value); This.shardedJedisPool.returnResource (Jedis); return status;} /** * Get The value of the specified key. If The key does not exist the special * value ' nil ' is returned. If the value stored at key was not a string an * error was returned because GET can only handle string values. * Time Complexity:o (1) * @param key * @return Bulk reply */public string getString (String key) {Shardedjedis Jedis = this . Shardedjedispool.getresource (); String value = Jedis.get (key); This.shardedJedisPool.returnResource (Jedis); return value;} /** * This stirng batch update * @param pairs */public list<object> batchsetstring (final list<pair<string, string> > Pairs) {final Shardedjedis Jedis = This.shardedJedisPool.getResource (); list<object> status = jedis.pipelined (new Shardedjedispipeline () {@Overridepublic void execute () {for (pair< String, string> pair:pairs) {set (Pair.getkey (), Pair.getvalue ());}}); This.shardedJedisPool.returnResource (Jedis); return status;} /** * This string is available in bulk * @param keys * @return */public list< String> batchgetstring (final list<string> keys) {final Shardedjedis Jedis = This.shardedJedisPool.getResource (); list<object> dataList = jedis.pipelined (new Shardedjedispipeline () {@Overridepublic void execute () {for (String Key:keys) {get (key);}}); This.shardedJedisPool.returnResource (Jedis); list<string> rtndatalist = new arraylist<string> (); for (Object data:datalist) {Rtndatalist.add ( String.build (data));} return rtndatalist;} /** ======================================hashes====================================== *//** * Set the specified hash field to the specified value. * If key does not exist, a new key holding a hash is created. * Time Complexity:o (1) * @param key * @param field * @param value * @return If the field already exists, and the hset jus T produced an update * of the value, 0 are returned, otherwise if a new field is created * 1 is returned. */public long HashSet (string key, String field, String value) {Shardedjedis Jedis = thIs.shardedJedisPool.getResource (); Long Count = Jedis.hset (key, field, value); This.shardedJedisPool.returnResource ( Jedis); return count;} /** * If Key holds a hash, retrieve the value associated to the specified * field. * If The field is not found or the key does not exist, a special ' nil ' * value is returned. * Time Complexity:o (1) * @param key * @param field * @return Bulk reply */public string Hashget (String key, String field) {Shardedjedis Jedis = This.shardedJedisPool.getResource (); String value = Jedis.hget (key, field); This.shardedJedisPool.returnResource (Jedis); return value;} /** * Set the respective fields to the respective values. Hmset replaces old * values with new values. * If key does not exist, a new key holding a hash is created. * Time Complexity:o (n) (with n being the number of fields) * @param key * @param hash * @return return OK or Exception if Hash is empty */public string Hashmultipleset (String key, map<string, string> hash) {Shardedjedis Jedis = This.shar DedjeDispool.getresource (); String status = Jedis.hmset (key, hash); This.shardedJedisPool.returnResource (Jedis); return status;} /** * Retrieve The values associated to the specified fields. * If Some of the specified fields does not exist, nil values is returned. * Non Existing keys is considered like empty hashes. * Time Complexity:o (n) (with n being the number of fields) * @param key * @param fields * @return Multi Bulk Reply specif Ically a list of all the values associated * with the specified fields, in the same order of the request. */public list<string> hashmultipleget (string key, String ... fields) {Shardedjedis Jedis = This.shardedJedisPool.getResource (); list<string> dataList = Jedis.hmget (key, fields); This.shardedJedisPool.returnResource (Jedis); return dataList;} /** * This batch of Hashmultipleset * @param pairs * @return */public list<object> batchhashmultipleset (Final List<pair <string, map<string, string>>> pairs) {final Shardedjedis Jedis = this. Shardedjedispool.getresource (); list<object> status = jedis.pipelined (new Shardedjedispipeline () {@Overridepublic void execute () {for (pair< String, map<string, string>> pair:pairs) {Hmset (Pair.getkey (), Pair.getvalue ());}}); This.shardedJedisPool.returnResource (Jedis); return status;} /** * This batch of Hashmultipleget * @param pairs * @return */public list<list<string>> batchhashmultipleget (final List<pair<string, string[]>> pairs) {final Shardedjedis Jedis = This.shardedJedisPool.getResource (); list<object> dataList = jedis.pipelined (new Shardedjedispipeline () {@Overridepublic void execute () {for (pair< String, string[]> pair:pairs) {hmget (Pair.getkey (), Pair.getvalue ());}}); This.shardedJedisPool.returnResource (Jedis); list<list<string>> rtndatalist = new arraylist<list<string>> (); for (Object data:datalist) { Rtndatalist.add (String_list.build (data));} return rtndatalist;} /** * Return all the fields and associateD values in a hash. * Time Complexity:o (n), where N is the total number of entries * @param key * @return all the fields and values contained into a hash. */public map<string, string> hashgetall (String key) {Shardedjedis Jedis = This.shardedJedisPool.getResource (); Map<string, string> hash = Jedis.hgetall (key); This.shardedJedisPool.returnResource (Jedis); return hash;} /** * This batch of Hashmultipleget * @param keys * @return */public list<map<string, string>> batchhashgetall (final list<string> keys) {final Shardedjedis Jedis = This.shardedJedisPool.getResource (); list<object> dataList = jedis.pipelined (new Shardedjedispipeline () {@Overridepublic void execute () {for (String Key:keys) {Hgetall (key);}}); This.shardedJedisPool.returnResource (Jedis); list<map<string, string>> rtndatalist = new arraylist<map<string, string>> (); for (Object data: DataList) {rtndatalist.add (data) (String_map.build);} return rtndatalist;} /** ======================================builder====================================== */public static Final builder<double> Double = new builder<double> () {@Overridepublic double build (Object data) {return double.valueof (String.build ( data));} @Overridepublic String toString () {return ' Double ';}}; public static Final Builder<boolean> Boolean = new builder<boolean> () {@Overridepublic Boolean build (Object D ATA) {return ((Long) data) = = 1;} @Overridepublic String toString () {return ' Boolean ';}}; public static final builder<long> long = new Builder<long> () {@Overridepublic long build (Object data) {return (Long) data;} @Overridepublic String toString () {return ' Long ';}}; public static final Builder<string> string = new Builder<string> () {@Overridepublic string build (Object data) {return Safeencoder.encode ((byte[]) data);} @Overridepublic string toString () {return ' string ';}}; public static final builder<list<string>> string_list = new builder<list<string>> () {@Override @suppresswarnings ("unchecked") public list<string> Build (Object data) {if (null = = data) { return null;} list<byte[]> L = (list<byte[]>) data;final arraylist<string> result = new Arraylist<string> ( L.size ()); for (final byte[] barray:l) {if (Barray = = null) {Result.add (null);} else {Result.add (Safeencoder.encode (Barra y));}} return result;} @Overridepublic String toString () {return "list<string>";}}; public static final builder<map<string, string>> string_map = new builder<map<string, string>> ( {@Override @suppresswarnings ("unchecked") public map<string, string> build (Object data) {final list<byte[] > Flathash = (list<byte[]>) data;final map<string, string> hash = new hashmap<string, String> (); FINA L iterator<byte[]> Iterator = Flathash.iterator (); while (Iterator.hasnext ()) {Hash.put (Safeencoder.encode ( Iterator.next ()), Safeencoder.encode (Iterator.next ()));} return hash;} @OverridepUblic String toString () {return "map<string, string>";}}; public static final builder<set<string>> String_set = new builder<set<string>> () {@ Override@suppresswarnings ("unchecked") public set<string> Build (Object data) {if (null = = data) {return null;} list<byte[]> L = (list<byte[]>) data;final set<string> result = new Hashset<string> (L.size ()); For (final byte[] barray:l) {if (Barray = = null) {Result.add (null);} else {Result.add (Safeencoder.encode));}} return result;} @Overridepublic String toString () {return "set<string>";}}; public static final builder<set<string>> String_zset = new builder<set<string>> () {@ Override@suppresswarnings ("unchecked") public set<string> Build (Object data) {if (null = = data) {return null;} list<byte[]> L = (list<byte[]>) data;final set<string> result = new Linkedhashset<string> ( L.size ()); for (final byte[] barray:l) {if (Barray = = null) {Result.add (null);} else {Result.add (Safeencoder.encode (Barray));}} return result;} @Overridepublic String toString () {return "zset<string>";}};/ * * ======================================other====================================== */public void Setshardedjedispool (Shardedjedispool shardedjedispool) {this.shardedjedispool = Shardedjedispool;} /** * This constructs Pair * @param key * @param value * @return */public <k, v> pair<k, v> makepair (K key, V value) {R Eturn New pair<k, v> (key, value);} /** * The pair represents key value pair * @version $Id $ * @author FENGJC * @param <K> * @param <V> */public class PAIR&L T K, v> {private k key;private v value;public Pair (K key, V value) {This.key = Key;this.value = value;} Public K GetKey () {return this.key;} public void Setkey (K key) {This.key = key;} Public V GetValue () {return this.value;} public void SetValue (V value) {this.value = value;}}}
Original reference: http://blog.csdn.net/wgw335363240/article/details/24471311
Example of using Java to connect to Redis