Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to operate Redis database

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Java to operate Redis database. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Redis is a memory-based database, which can greatly improve the running speed by interacting with Redis.

First, let's create a normal Maven project and add the corresponding dependencies

Redis.clients jedis 3.3.0 com.alibaba fastjson 1.2.72

Then we can use it.

Test the connection (I am using a local reids service here)

/ / define host number and port number HostAndPort hostAndPort = new HostAndPort ("127.0.0.1", 6379); / / connect redis service Jedis jedis=new Jedis (hostAndPort) / / ping System.out.println ("Service is running:" + jedis.ping ()) / / close jedis.close (); console output PONG indicates a successful connection

Next, let's look at the basic operations of the five big data types of Redis.

String Typ

System.out.println ("Save one data:" + jedis.set ("age", "20"); System.out.println ("Save multiple data:" + jedis.mset ("name", "zhangsan", "sex", "male"); System.out.println ("get one data:" + jedis.get ("age")) System.out.println ("get multiple data:" + jedis.mget ("name", "sex"); System.out.println ("concatenate the specified string after the specified data: + jedis.append (" name "," Hello ") System.out.println ("View the length of a data:" + jedis.strlen ("name"); System.out.println ("modify the value of a data and return the value before modification:" + jedis.getSet ("name", "lisi")) System.out.println ("determine whether a data exists:" + jedis.exists ("name"); System.out.println ("set expiration time for a data (unit / s):" + jedis.expire ("name", 20)) System.out.println ("View the remaining lifetime of a data (s):" + jedis.ttl ("name"); System.out.println ("delete one or more data:" + jedis.del ("name", "sex"))

View print results

Set Typ

System.out.println ("add one or more elements to the collection:" + jedis.sadd ("key1", "v1", "v2", "v3"); System.out.println ("get the number of elements in the collection:" + jedis.scard ("key1")) System.out.println ("return all elements in the collection:" + jedis.smembers ("key1"); System.out.println ("determine whether the specified element exists in the collection:" + jedis.sismember ("key1", "v1")) System.out.println ("remove elements specified in the collection:" + jedis.srem ("key1", "v3")); / / here we are creating a collection System.out.println ("adding one or more elements to the collection:" + jedis.sadd ("key2", "v2", "v3", "v4") System.out.println ("return the difference between key1 and key2:" + jedis.sdiff ("key1", "key2"); System.out.println ("return the intersection of key1 and key2:" + jedis.sinter ("key1", "key2"); System.out.println ("return the union of key1 and key2:" + jedis.sunion ("key1", "key2"))

View the result

Hash Typ

Map map=new HashMap (); map.put ("name", "zhangsan"); map.put ("age", "20"); map.put ("sex", "male"); System.out.println ("create a hash table to store a user object:" + jedis.hmset ("user", map)) System.out.println ("get the name of the user in the hash table:" + jedis.hget ("user", "name"); System.out.println ("check whether the specified field exists in the hash table:" + jedis.hexists ("user", "name")) System.out.println ("get the number of fields in the hash table:" + jedis.hlen ("user"); System.out.println ("get all fields in the hash table:" + jedis.hkeys ("user")) System.out.println ("get the values of all fields in the hash table:" + jedis.hvals ("user"); System.out.println ("get all fields and values in the hash table:" + jedis.hgetAll ("user")) System.out.println ("Delete one or more hash table fields:" + jedis.hdel ("user", "name", "age", "sex"))

View the result

List Typ

System.out.println ("insert a value into the head of the list (multiple values): + jedis.lpush (" city "," Beijing "," Shanghai "); System.out.println (" insert a value at the end of the list (multiple values): "+ jedis.rpush (" city "," Jinan "," Nanjing ")) System.out.println ("get elements within the specified range of the list:" + jedis.lrange ("city", 0,-1); System.out.println ("get list length:" + jedis.llen ("city")) System.out.println ("remove the first element of the list and output the value: + jedis.lpop (" city "); System.out.println (" remove the last element of the list and output the value: "+ jedis.rpop (" city ")) System.out.println ("modify the value of the element in the specified index location in the list:" + jedis.lset ("city", 0, "Xizang")) System.out.println ("Let the list keep only the elements within the specified range, and all elements that are not within the specified range will be deleted:" + jedis.ltrim ("city", 1,2))

View the result

Sorted Set Typ

Map scoreMembers=new HashMap (); scoreMembers.put ("member1", 1D); scoreMembers.put ("member2", 2D); scoreMembers.put ("member3", 3D); System.out.println ("add one or more elements to an ordered set, or update the score of existing elements:" + jedis.zadd ("member", scoreMembers)) System.out.println ("get the number of elements in the ordered set:" + jedis.zcard ("member")); System.out.println ("calculate the number of members in the specified interval ([socre1,socre2]) score in the ordered set:" + jedis.zcount ("member", 0D, 3D)) System.out.println ("return the elements in the specified interval of the ordered set through the index interval, from low to high: + jedis.zrange (" member ", 0LRMI 1)); System.out.println (" return the index interval returns the elements in the specified interval of the ordered set, from high to low: "+ jedis.zrevrange (" member ", 0LMMI 1)) System.out.println ("returns the score value of the specified element in the ordered set:" + jedis.zscore ("member", "member1"); System.out.println ("remove one or more elements from the ordered set:" + jedis.zrem ("member", "member1", "member2")) System.out.println ("returns the index of the specified element in the ordered collection:" + jedis.zrank ("member", "member3"))

View the result

Finally, let's take a brief look at how to operate the database.

System.out.println ("clear current database:" + jedis.flushDB ()); System.out.println ("clear all databases:" + jedis.flushAll ()); System.out.println ("View how much data is stored in the current database:" + jedis.dbSize ()) Set keys = jedis.keys ("*"); System.out.println ("View all key values stored in the current database:" + keys); System.out.println ("Select a database:" + jedis.select (0))

View the result

In fact, Redis also has three special storage types.

Geospatial

Mainly used to store geographic location information, and operate on the stored information, based on Sorts Set ordered collection

HyperLogLog

It's used to do cardinality statistics.

Bitmap

Record information with only two states by manipulating binary (0Pol 1).

This is the end of this article on "how to use Java to operate Redis database". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report