Question

I tried adding some sample score-value pairs to redis sorted set using below code:

  String key = "set";
  redis.zadd(key, 5, "1034");
  redis.zadd(key, 2, "1030");
  redis.zadd(key, 1, "1089");

and tried retrieving it using byteArray and BitSet

  byte[] byteArr = redis.get(key.getBytes());
  BitSet bitSet = fromByteArrayReverse(byteArr);  
  System.out.println(bitset.toString()));

also i tried executing

  System.out.println(redis.get(key.getBytes()));

which is supposed to give me an address of the byte-array

But for both of these commands i get the error

" ERR Operation against a key holding the wrong kind of value"

So can anyone please tell me why does this error occur in the first place and also the correct redis command/code to retrieve values from a redis sorted-set??

Was it helpful?

Solution

What you want is calling ZSCORE key "1034"

Or in the case of wanting only elements between two particular scores ZRANGEBYSCORE key lower upper

Since you also have "rank" (position or index, as in a list) you can also ask for example for the first three elements in your set ZRANGEBYRANK key 0 2

The error you are getting is because once you assign a value to a key, that value defines the type of the internal structure on redis, and you can only use commands for that particular structure (or generic key commands such as DEL and so on). In your case you are trying to mix sorted sets with byte operations and it doesn't match.

To see all sorted set commands, please refer to http://redis.io/commands#sorted_set

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top