Redis Jedis – How to perform CRUD operations on set value using jedis library

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to perform CRUD operation on a set value using Jedis library.

Jedis Library

Jedis is a Java client library for redis datastore. It is small and very easy to use, and fully compatible with redis 2.8.x, 3.x.x and above datastore. You can find here more information about jedis library.

Set Value

Set is an unordered collection of unique elements, In Redis, sets can be store as a value and various redis commands can be used to store, manage and retrieved a set value stored in redis database. You can find here more information about Set commands.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>3.0.1</version>
</dependency>

For latest version of jedis library, check this page.

Redis Installation

You will need to install latest version of Redis. Check this page for more information about installing redis.

Jedis Connection

Create an object of Jedis ( redis.clients.jedis.Jedis ) class for connecting your java code to redis.

Jedis jedis = new Jedis();

If you have started one redis service in your local machine and on default port (6379) then default constructor will just work fine. Otherwise you have to pass correct host url and port no. as an argument into the constructor.

Create & Add 

sadd method insert one or more elements in the set value. If set value does not exist, then it first creates a key holding empty set value before performing the insert operation. 

/* Creating a new set <set-1> and inserting string values a, b, c, d */
jedis.sadd("set-1", "a", "b", "c", "d");

/* Creating a new set <set-2> and inserting string values 1, 2 */
jedis.sadd("set-2".getBytes(),"1".getBytes(),"2".getBytes());

Check Member Existence 

sismember method is used to check, if the specified element is a member of the set or not. 

/* Check if ab is a member of <set-1> or not */
jedis.sismember("set-1", "ab");

/* Check if 1 is a member of <set-2> or not */
jedis.sismember("set-2".getBytes(), "1".getBytes());

Remove & Return 

spop method is used to remove and return one or more random elements from a set value stored at a key. 

/* Removes and return single random element from the <set-1> */
jedis.spop("set-1");

/* Removes and return 3 random element from the <set-1> */
jedis.spop("set-1", 3);

Size 

scard method is used to get the size of a set value stored at a key. 

/* Returns size of <set-1> */
jedis.scard("set-1");

Delete 

srem method is used to delete one or more elements from the set value.  

/* Remove a, b, c elements from <set-1> */
jedis.srem("set-1", "a", "b", "c");
		
/* Remove 2, 3 elements from <set-2> */
jedis.srem("set-1".getBytes(), "1".getBytes(), "b".getBytes());

Get All

smembers method is used to get all the elements of a set value.  

/* Returns all elements from <set-1> */
jedis.smembers("set-1");

Union

There are two methods for performing union operation on a set value. They are as as follows :-

  1. sunion :- It performs union operation on two or more sets and returns the result as an array.
    /* Get Union of <set-1>, <set-2> */
    jedis.sunion("set-1", "set-2");
  2. sunionstore :- It performs union operation on two or more sets and returns the result in a new set value.
    /* Get Union of <set-1>, <set-2> in set destination set <set-3> */
    jedis.sunionstore("set-3", "set-1", "set-2");

Intersection

There are two methods for performing intersection operation on a set value. They are as as follows :-

  1. sinter :- It performs intersection operation on two or more sets and returns the result as an array.
    /* Get Intersection of <set-1>, <set-2> */
    jedis.sinter("set-1", "set-2");
  2. sinterstore :- It performs intersection on two or more sets and returns the result in a new set value.
    /* Get Intersection of <set-1>, <set-2> in set destination set <set-3> */
    jedis.sinterstore("set-3", "set-1", "set-2");

Difference

There are two methods for performing difference operation on a set value. They are as as follows :-

  1. sdiff :- It performs difference on two or more sets and returns the result as an array.
    /* Get Difference of <set-1>, <set-2> */
    jedis.sdiff("set-1", "set-2");
  2. sdiffstore :- It performs difference on two or more sets and returns the result in a new set value.
    /* Get Difference of <set-1>, <set-2> in set destination set <set-3> */
    jedis.sdiffstore("set-3", "set-1", "set-2");

References :-

  1. Set Command Docs

That’s all for how to perform CRUD operation on a set value using Jedis library. If you liked it, please share your thoughts in comments section and share it with others too.