Abhijay Ghildyal Abhijay Ghildyal - 2 months ago 9
R Question

R library rredis during 'hset' appends spurious string

I am storing a hash to key value using rredis as follows :

library(rredis)

redishost<-c("127.0.0.1",6379)

redisConnect(host=redishost[1], port=as.numeric(redishost[2]))

toStore <- "asdf"
redisHSet(key='simulatorinput', field = "asdf", value = toStore, NX=F)
redisHGet(key = 'simulatorinput', field = "asdf")


When I check my input in redis I get the following output:

127.0.0.1:6379> HGET simulatorinput asdf
"X\n\x00\x00\x00\x02\x00\x03\x03\x00\x00\x02\x03\x00\x00\x00\x00\x10\x00\x00\x00\x01\x00\x04\x00\t\x00\x00\x00\x04asdf"


but, when I check my input to redis from R using
HGET


> redisHGet(key = 'simulatorinput', field = "asdf")
[1] "asdf"


Can somebody tell me the reason why this is happening? How can I avoid this from happening?

Answer

rredis pdf manual (p. 28) says the following:

In order to store strings that can easily be read by other clients, first convert the character object using the charToRaw function as shown in the examples.

toStore <- "asdf"
redisHSet(key='simulatorinput', field = "asdf", value = charToRaw(toStore), NX=F)
redisHGet(key = 'simulatorinput', field = "asdf")
[1] "asdf"
attr(,"redis string value")
[1] TRUE

From redis client:

redis 127.0.0.1:6379> HGETALL simulatorinput
1) "asdf"
2) "asdf"
Comments