Search code examples
javaredisjedis

using jedis hmset method for inserting multiple field element in redis


I am implementing a simple redis command using jedis in java. The redis-cli command is:

hmset myhash key "value1" field2 "value2" field3 "value3"

The problem is that jedis's hmset method requires two parameters:

  • String key
  • Map <String, String> hash

Possible solution:

String key;
String value2;
String value3;
while(!toVisit.isEmpty()) {
    key = someQueue.poll()
    value2 = getTitle(key)
    value3 = getSize(value2)
    jedis.hmset(key, value2Map)
    jedis.hmset(key, value3Map)
...

But it feels a bit counter-intuitive having to implement three Tree Map objects to get their last added object in order to add a tuple with three fields in the redis db.

Just hoping for some better ideas before going ahead and implementing this.


Solution

  • Not sure this is what you are looking for but I think you need to put all values into a hashmap and place the hashmap object in jedis.hmset() as a second param. See below

    Map<String, String> avalue = new 
    HashMap<String, String>();
    avalue.put(a, a1);
    avalue.put(b, b1);
    avalue.put(c, c1);
    avalue.put(d,d1)
    jedis.hmset(key, avalue);