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

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to perform CRUD operation on a list 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.

List Value

List is a sequence of strings sorted by insertion order. In Redis, list can be store as a value and various redis commands can be used to store, manage and retrieved a list value stored in redis database. You can find here more information about List 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

Jedis library provides several methods for creating and inserting elements in a list value. Some of the important methods are as follows :-

  1. lpush :- It insert one or more elements at the head of the list value. If list value does not exist, then it first creates a key holding empty list value before performing the insert operation.
    /* Creating a new list and inserting string values a, b, c at head */
    jedis.lpush("list-1", "a", "b", "c");
    		
    /* Creating a new list and inserting byte array of string value 1, 2 at head */
    jedis.lpush("list-2".getBytes(),new BigInteger("1").toByteArray(),new BigInteger("2").toByteArray());
  2. rpush :- It insert one or more elements at the tail of the list value. If list value does not exist, then it first creates a key holding empty list value before performing the insert operation.
    /* Creating a new list and inserting string values a, b, c at tail */
    jedis.rpush("list-3", "a", "b", "c");
    		
    /* Creating a new list and inserting byte array of string value 1, 2 at tail */
    jedis.rpush("list-4".getBytes(),new BigIntege r("1").toByteArray(),new BigInteger("2").toByteArray());
  3. lpushx :- It insert one or more elements at the head of the list value only if list value already exist.
    /* Inserting string values d, e at head of the list <list-1> */
    jedis.lpushx("list-1", "d", "e");
    		
    /* Inserting string values 3, 4 at head of the list <list-2> */
    jedis.lpushx("list-2".getBytes(),new BigInteger("3").toByteArray(),new BigInteger("4").toByteArray());
  4. rpushx :- It insert one or more elements at the tail of the list value only if list value already exist.
    /* Inserting string values d, e at tail of the list <list-3> */
    jedis.rpush("list-3", "d", "e");
    		
    /* Inserting string values 3, 4 at tail of the list <list-4> */
    jedis.rpush("list-4".getBytes(),new BigInteger("3").toByteArray(),new BigInteger("4").toByteArray());

Remove & Return

There are two methods for performing stack like pop operation on a list value. They are as as follows :-

  1. lpop :- It remove and return first element of the list value.
    /* Removes and return single element from the head of <list-1> */
    jedis.lpop("list-1");
    		
    /* Removes and return single element from the head of <list-2> */
    jedis.lpop("list-2".getBytes());
  2. rpop :- It remove and return last element of the list value.
    /* Removes and return single element from the tail of <list-3> */
    jedis.rpop("list-3");
    		
    /* Removes and return single element from the tail of <list-4> */
    jedis.rpop("list-4".getBytes());

Length 

llen method is used to get the length of the list value stored at a key. Code example :- 

/* Returns size of <list-1> */
jedis.llen("list-1");
		
/* Returns size of <list-2> */
jedis.llen("list-2".getBytes());

Delete 

lrem method is used to delete all the occurrences of an element from the list value.  

/* Remove one occurrence of element a from <list-1> */
jedis.lrem("list-1", 1, "a");
		
/* Remove two occurrence of element 1 from <list-2> */
jedis.lrem("list-2".getBytes(), 2, "1".getBytes());

Get By Index 

lindex method is used to get an element by its index argument.  

/* Get an element at index 3 from <list-1> */
jedis.lindex("list-1", 3);
		
/* Get an element at index 2 from <list-2> */
jedis.lindex("list-2".getBytes(), 2);

Update By Index 

lset method is used to update an element by its index argument.  

/* Set an element g at index 3 from <list-1> */
jedis.lset("list-1", 3, "g");
		
/* Set an element 6 at index 2 from <list-2> */
jedis.lset("list-2".getBytes(), 2, "6".getBytes());

Get Multiple Elements

lrange method is used to get one or elements from the list value, defined by the offsets argument.  

/* Returns elements from index 1 to 5 from <list-1> */
jedis.lrange("list-1", 1, 5);
		
/* Returns all elements from <list-2> */
jedis.lrange("list-2".getBytes(), 0, -1);

References :-

  1. List Command Docs

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