Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Multimaps With Redis

DZone's Guide to

Java Multimaps With Redis

Redis server provides many popular data structures like map, list, set... In practice, however, you may face more complicated requirements, such as storing several values per key, for example. The best practice for that would be implementing the Multimap.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

Redis server provides many popular data structures like map, list, set...

In practice, however, you may face more complicated requirements, such as storing several values per key, for example. The best practice for that would be implementing the Multimap.

You may need to spend some time to code such structure by yourself. It may look like this:

Map<MyKey, Set<MyObject>> setMultimap = new HashMap<>();

void put(MyKey key, MyObject obj) {
   Set<MyObject> list = setMultimap.get(key);
   if (set == null) {
       set = new HashSet<>();
       setMultimap.put(key, obj);
   }
   set.add(obj);
}

void remove(MyObject obj) {
   set<MyObject> set = setMultimap.get(key);
   if (set != null) {
      set.remove(obj);
   }
}

Getting this to work on top of Redis requires much more extra work: create Redis connection, execute Redis command, establish one-way relation between Redis map key and Redis list name, and so on ...

Or no work at all if you are using the Redisson framework. It takes care of all the tedious jobs for you. With implementation of a set of standard java objects: Map, List, Set, Lock ... backed by Redis, it offers Multimap object too.

There are two flavors of Multimap objects available:

  1. RListMultimap - List based multimap, values bounded to key stored in List structure.

  2. RSetMultimap - Set-based multimap, values bounded to key stored in Set structure.

Let's take a look at the following snippet of using the RSetMultimap:

Config config = new Config();
config.useClusterServers()
    // redis cluster nodes
    .addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001");

RedissonClient redisson = Redisson.create(config);

RSetMultimap<String, String> setMultimap = redisson.getSetMultimap("myFish");

// Adding items

setMultimap.put("favoriteFish", "Flagfin");
setMultimap.put("favoriteFish", "Shiner");
setMultimap.put("favoriteFish", "Ladyfish");
setMultimap.put("oceanFish", "Shark");
setMultimap.put("oceanFish", "Ocean sunfish");

// Removing item

setMultimap.remove("oceanFish", "Shark");
setMultimap.remove("favoriteFish", "Flagfin");

// Getting all items

Set<String> favoriteFish = setMultimap.get("favoriteFish");

// Getting size

// total entries amount
setMultimap.size(); // 5
// total values amount by key
setMultimap.get("favoriteFish").size(); // 3

// check entry existence
setMultimap.containsEntry("favoriteFish", "Ladyfish");

// and so on ...

It's important to note that in Redisson object type can be anything, not limited to only String.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
java ,redis

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}