skip to Main Content

My requirement is that in redis db, keys should be stored as strings and values as byte array.
And I am able to achieve the same in java using lettuce’s RedisCodec. But when I view the value through reds-cli it appears to be in original string itself rather than the byte format. Following is the simple code I am trying with.

RedisURI redisURI = RedisURI.builder().withHost("localhost")
        .withPort(6379)
        .build();

RedisClient redisClient = RedisClient.create(redisURI);
RedisCommands<String, byte[]> redisCommands = redisClient.connect(RedisCodec.of(new StringCodec(), new ByteArrayCodec())).sync();

redisCommands.set("key", "value".getBytes(StandardCharsets.UTF_8));
System.out.println(redisCommands.get("key"));

Console O/p : [B@3bd7f8dc

redis-cli o/p:

127.0.0.1:6379> get key

"value"

I am using lettuce as redis client. I am unable to understand why the value I obtained from redis-cli is in string itself rather than the byte format.

Any help is appreciated. Thanks in advance.

3

Answers


  1. I believe that below answer helps you to understand reading byte value from redis-cli.

    How to read Redis binary values in redis console

    Here is also detail explaniton of redis-cli. I hope it makes sense.
    https://redis.io/docs/manual/cli/

    Login or Signup to reply.
  2. The second argument from RedisCodec is how you will transmit the value data to Redis Server, which in your case is through ByteArrayCodec, once it is retrieved using get is serialized back to the original form.

    Login or Signup to reply.
  3. What I use to read byte arrays from redis is something like this:

    byte[] value = redisSyncConnection.get("key");
    try (ByteArrayInputStream bis = new ByteArrayInputStream(value);
         ObjectInputStream ois = new ObjectInputStream(bis)) {
         String valueString = (String) ois.readObject();
         System.out.println(valueString);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search