Indy Indy - 2 months ago 6
Java Question

Jedis HMSET map insertion order

Jedis has a hmset method which allows you to set a map of fields and their values at a specific key.

I use the method this way:

Map<String, String> map = new LinkedHashMap<String, String>();

// General player data
map.put("name", player.getName());
map.put("ip", player.getAddress().getAddress().getHostAddress());
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");

// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");

pipeline.hmset("uuid:" + uuid, map);

pipeline.sync();
core.redis.getJedisPool().returnResourceObject(jedis);


I decided to use a LinkedHashMap to retain the insertion order — however, when looking at the database, it still messes up the order.

Does anyone know how to insert it into the database without messing up the order?

Thank you.

Answer

The String-based Jedis client transcodes the strings using a temporary HashMap, so it kills your order. The BinaryClient iterates over the Map directly and retains the order.

The lettuce client keeps the order of your map in any case.

Alternatively, set the values one by one using HSET hash key value

HTH, Mark