In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to operate the data type of redis in java? in view of this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. Project must-have Jar package
Redis.clients jedis 2.9.0 commons-pool commons-pool 1.6 junit junit 4.12
II. Examples and structure
1) remote connection redis, clear database new Jedis (redis service connection IP, port)
Private static Jedis jedis; @ Before public void before () {jedis= JedisConnectUtil.getJedis (JedisTest.URL,63**); jedis.flushDB ();}
2) String type
@ Test public void testString () {/ / set key to get key get set jedis.set ("steve", "stevetao"); System.out.println ("set value:" + jedis.get ("steve")); / / add key append jedis.append ("steve", "Is Good Man"); System.out.println ("append value:" + jedis.get ("steve")) / / delete operation del jedis.del ("steve"); System.out.println ("value after deletion: + jedis.get (" steve ")); / / Save if it does not exist, setnx msetnx jedis.setnx (" steve "," stevetao "); System.out.println (" value after setting: "+ jedis.get (" steve ")) System.out.println ("set value again:" + jedis.setnx ("steve", "stevetao")); / / intercept the string substr System.out.println ("value after interception:" + jedis.substr ("steve", 0P4)); / / set multiple key-value pairs mset mget jedis.mset (new String [] {"zhangsan", "123"," lisi "," 1234 "})) System.out.println ("multiple settings:" + jedis.mget ("zhangsan", "lisi")); / / increasing and decreasing incr decr incrby decrby jedis.incr ("zhangsan"); jedis.decr ("lisi"); System.out.println ("increasing and decreasing value:" + jedis.mget ("zhangsan", "lisi"); jedis.incrBy ("zhangsan", 6) Jedis.decrBy ("lisi", 3); System.out.println ("increasing and decreasing value:" + jedis.mget ("zhangsan", "lisi");}
3) List type
@ Test public void testList () {/ / add rpush header to add lpush jedis.lpush ("books", "java", "C++", "Ruby", "Scala", "python"); jedis.rpush ("language", "java", "C++", "Ruby", "Scala", "python") / /-1 represents the last element of the list,-2 represents the penultimate element of the list, and so on. System.out.println ("books value after headers are added:" + jedis.lrange ("books", 0maxim 1)); System.out.println ("tail value after adding pages:" + jedis.lrange ("language", 0mailim 1)); / / tail deletion rpop header deletion lpop System.out.println ("deleted value is:" + jedis.lpop ("books")) System.out.println ("deleted value is:" + jedis.rpop ("language")); System.out.println ("books value after head deletion:" + jedis.lrange ("books", 0maire 1)); System.out.println ("language value after tail deletion:" + jedis.lrange ("language", 0meme 1)); / / tail deletion and add rpoplpush jedis.rpoplpush ("language", "books") System.out.println ("books value after tail deletion and header addition:" + jedis.lrange ("books", 0quotation 1); System.out.println ("page value after tail deletion and header addition:" + jedis.lrange ("language", 0fuge 1)); / / difference: only existing list can be added, not list jedis.lpushx ("books", "php") like lpush). Jedis.lpushx ("book", "php"); System.out.println ("books value after headers are added:" + jedis.lrange ("books", 0jure Lize1); System.out.println ("Books value after headers are added:" + jedis.lrange ("book", 0Marelle 1)) / / get the value of the llen specified index lindex retains the intercepted value ltrim System.out.println ("books collection length:" + jedis.llen ("books")); System.out.println ("second value of the books collection:" + jedis.lindex ("books", 1)); jedis.ltrim ("books", 0Magne2) System.out.println ("Books value after intercept:" + jedis.lrange ("books", 0maxim 1);}
4) Hash type
@ Test public void testHash () {/ / suitable field: set the value hset to hget (if value is a json string, similar to the saved object) jedis.hset ("student", "name", "zhangsan"); System.out.println ("the value of name in student is:" + jedis.hget ("student", "name")); / / suitable object: set the value hmset to hmget Map map = new HashMap () Map.put ("name", "lisi"); map.put ("age", "36"); jedis.hmset ("teacher", map); System.out.println ("values of name and age in teacher are:" + jedis.hmget ("teacher", "name", "age")) / / whether there is a key age hexists if (jedis.hexists ("teacher", "age")) {/ / add 4 hincrBy jedis.hincrBy ("teacher", "age", 4) to the specified value; System.out.println ("values of name and age in teacher are:" + jedis.hmget ("teacher", "name", "age")) } / / return the number of key hlen return key hvals return key hkeys value pair hgetAll jedis.hset ("student", "age", "13"); jedis.hset ("student", "qq", "2246920330"); jedis.hset ("student", "address", "beijing"); System.out.println ("number of keys in student:" + jedis.hlen ("student")) System.out.println ("all keys in student are:" + jedis.hkeys ("student"); System.out.println ("all values in student are:" + jedis.hvals ("student")); System.out.println ("all key-value pairs in student are:" + jedis.hgetAll ("student")); / / delete hdel jedis.hdel ("student", new String [] {"address", "qq", "age"})) System.out.println ("after deletion, all key-value pairs in student are:" + jedis.hgetAll ("student"));}
5) Set type
@ Test public void testSet () {/ / set add values sadd values smembers jedis.sadd ("student", "Jan", "John", "Steve", "jack", "lili", "peter", "Anna"); jedis.sadd ("girls", "Jan", "lili", "Alice", "Jeanne", "Anna"); System.out.println ("ranking in no particular order: + jedis.smembers (" student ")) / / number of set whether there is a value sismember System.out.println ("number of set sets:" + jedis.scard ("student")); System.out.println ("whether steve exists in student:" + jedis.sismember ("student", "Steve"); System.out.println ("whether stevetao exists in student:" + jedis.sismember ("student", "Stevetao")) / / System.out.println (jedis.sscan ("student", "0"). GetResult (); / / delete the specified value srem randomly delete and return spop System.out.println ("delete specified value Steve:" + jedis.srem ("student", "Steve")); System.out.println ("deleted value is:" + jedis.spop ("student")) System.out.println ("ranking again in no order: + jedis.smembers (" student ")); / / set operation System.out.println (" intersection of two set: "+ jedis.sinter (" student "," girls "); System.out.println (" union of two set: "+ jedis.sunion (" student "," girls ") System.out.println ("difference of student to girls:" + jedis.sdiff ("student", "girls")); System.out.println ("difference of girls to student:" + jedis.sdiff ("girls", "student"); / / set operation and save jedis.sinterstore ("jiaoji", "student", "girls"); jedis.sunionstore ("bingji", "student", "girls") Jedis.sdiffstore ("chaji", "student", "girls"); System.out.println ("intersection:" + jedis.smembers ("jiaoji")); System.out.println ("Union:" + jedis.smembers ("bingji")); System.out.println ("difference of student to girls:" + jedis.smembers ("chaji"));}
6) ZSet type
@ Test public void testZset () {jedis.zadd ("math", 75, "Jim"); jedis.zadd ("math", 86, "Lina"); jedis.zadd ("math", 52, "Dive"); jedis.zadd ("math", 91, "Bobber"); System.out.println ("number of members of ordered sets:" + jedis.zcard ("math")) System.out.println ("members of ordered collections:" + jedis.zrevrangeByScore ("math", 100d0)); / / returns set System.out.println ("members of ordered collections:" + jedis.zrangeWithScores ("math", 0100));}
This is the answer to the question about how to manipulate the data type of redis in java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.