-1

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.

Stelios
  • 1,294
  • 3
  • 12
  • 28
  • I have update my question to be a bit more clear. I have not created the maps yet - i ' m in the process of doing so now. But they will be NavigableMap in order to be able to get the last entry while performing the loop. – Stelios Aug 02 '17 at 12:05
  • 1
    What is the issue you're facing in using hmset? I think you can simply create k map of your key values and store that map in hmset with a key for identification of that map. Rest, if you'll state the issue then better I can help you. – Mayank Jain Aug 24 '17 at 04:57

2 Answers2

4

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);
Kreecha_pu
  • 56
  • 1
  • 3
0

You could use Redisson if you looking Java API compatible solution:

Map<String, String> redisMap = redisson.getMap("myMap");

Map<String, String> newEntries = new HashMap<>();
newEntries.put("1", "a");
newEntries.put("2", "b");
newEntries.put("3", "c");

redisMap.putAll(newEntries);
Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71