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

5 years ago Lalit Bhagtani 0

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

Hash Value

Hash is a map of field-value pair, which can be used to represent object type like java object. In Redis, hash can be store as a value and various redis commands are used to store, manage and retrieved a hash value stored in redis database. You can find here more information about Hash 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 & Set Field’s Value

There are three methods for setting a value associated with the field contained inside the hash value. They are as as follows :-

  1. hset :- It set the specified value to its respective field in the hash value. If field already exists, then it’s value is overwritten. If key does not exist, then a new key holding a hash value is created before performing the set operation.
    /* Insert name field in <hash-1> */
    jedis.hset("hash-1", "name", "john");
    		
    /* Insert name field in <hash-2> */
    jedis.hset("hash-2".getBytes(), "name".getBytes(), "john".getBytes());
  2. hsetnx :- It set the specified value to its respective field in the hash value, only if field does not exist. If field already exists, then this operation has no effect. If key does not exist, then a new key holding a hash value is created before performing the set operation.
    /* Insert name field in <hash-1> */
    jedis.hsetnx("hash-1", "name", "john");
    		
    /* Insert name field in <hash-2> */
    jedis.hsetnx("hash-2".getBytes(), "age".getBytes(), "23".getBytes());
  3. hmset :- It sets the specified values to their respective fields ( multiple field/value pair ) in the hash value. If any of the specified fields already exists, then it’s value will be overwritten. If key does not exist, then a new key holding a hash value is created before performing the set operation.
    /* Insert name and age field in <hash-1> */
    Map<String,String> map = new HashMap<String, String>();
    map.put("name","john");
    map.put("age","23");
    jedis.hmset("hash-1", map);

Get Field’s Value

There are two methods for getting a value associated with the field contained inside the hash value. They are as as follows :-

  1. hget :- It returns the value associated with a single field contained inside the hash value.
    /* Get value of name field in <hash-1> */
    jedis.hget("hash-1", "name");
  2. hmget :- It returns the value associated with one or more fields contained inside the hash value.
    /* Get value of name and age field in <hash-1> */
    jedis.hmget("hash-1", "name", "age");

Size 

hlen method is used to get the number of fields contained in the hash value stored at a key.

/* Returns number of fields in <hash-1> */
jedis.hlen("hash-1");

Delete

hdel method is used to delete one or more fields from the hash value stored at a key.

/* Remove name and age field from <hash-1> */
jedis.hdel("hash-1", "name", "age");
		
/* Remove name and age field from <hash-2> */
jedis.hdel("hash-2".getBytes(),"name".getBytes(),"age".getBytes());

Check Field Existence 

hexists method is used to check, if the specified field is a member of the hash or not.

/* Check if name is a member of <hash-1> or not */
jedis.hexists("hash-1", "name");

/* Check if name is a member of <hash-2> or not */
jedis.hexists("hash-2".getBytes(),"name".getBytes());

Get All

hgetall method is used to get all the fields and its associated values contained in the hash value stored at a key. 

/* Returns all fields and its values from <hash-1> */
jedis.hgetAll("hash-1");

/* Returns all fields and its values from <hash-2> */
jedis.hgetAll("hash-2".getBytes());

Get All Keys

hkeys method is used to get the names of all fields contained in the hash value stored at a key. 

/* Returns all fields names from <hash-1> */
jedis.hkeys("hash-1");

/* Returns all fields names from <hash-2> */
jedis.hkeys("hash-2".getBytes());

Get All Values

hvals method is used to get all the field’s values contained in the hash value stored at a key. 

/* Returns all values from <hash-1> */
jedis.hvals("hash-1");

/* Returns all values from <hash-2> */
jedis.hvals("hash-2".getBytes());

References :-

  1. Hash Command Docs

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