1
votes

I have a database on Redis with the following previous registred values:

person:1 {city city:1 }
city:1 {name Brasilia}

And I have a Lua script to get the city name of a given person:

redis.call("SELECT", 5)
local attribute = redis.call("HMGET", KEYS[1], ARGV[1])
local value = redis.call("HMGET", attribute, "name")
return value

Where the KEYS[1] should have the person key, and ARGV[1] the attribute of that person, in that specific case I assume that it is the city.

But when I execut the script with the comand:

$ redis-cli --eval redis_script.lua person:1 , city

I receive the following error:

(error) ERR Error running script (call to f_8b79864f056e62eadd87b14b98e43c3457971911): @user_script:3: @user_script: 3: Lua redis() command arguments must be strings or integers

Meaning an error on the 3rd line of my script on redis.call, which I assume that is because "attribute" value is invalid. So I changed my script to find out the value of "attribute":

redis.call("SELECT", 5)
local attribute = redis.call("HMGET", KEYS[1], ARGV[1])
return attribute

Which returns:

redis-cli --eval redis_script.lua person:1 , city
1) "city:1"

And changed again to:

redis.call("SELECT", 5)
local attribute = redis.call("HMGET", KEYS[1], ARGV[1])
local value = redis.call("HMGET", "city:1", "name")
return value

Which gives me:

redis-cli --eval another_test.lua person:1 , city
1) "Brasilia"

That is what I want. But I don't get why my first code gives error on redis.call, since the "attribute" value is correct, that is, "city:1".

1

1 Answers

1
votes

I think I have just found out the error. Making some tests here I get one interesting error on the following code:

redis.call("SELECT", 5)
local attribute = redis.call("HMGET", KEYS[1], ARGV[1])
local message = "City: ".. " " ..attribute
return message

Executing him:

redis-cli --eval test.lua person:1 , city
(error) ERR Error running script (call to f_03b059b49498769b0110c9f44765d1b769cb6976): @user_script:3: user_script:3: attempt to concatenate local 'attribute' (a table value) 

What caught my attention was the end of the error "...(a table value)". So I changed my first code to try to acess attribute as a table:

redis.call("SELECT", 5)
local attribute = redis.call("HMGET", KEYS[1], ARGV[1])
local value = redis.call("HMGET", attribute[1], "name")
return value

Which gives me:

redis-cli --eval redis_script.lua person:1 , city
1) "Brasilia"

So it just happens that I still didn't properly understand how Lua work with Redis. At first it looks like the redis.call returns a value as a string or an integer, but looks like it's not that way. I still don't know if it's because I am using a Hash, since I set person:1 and city:1 with HSET, or if it's that's just how Lua with redis works.

I hope this still help someone that goes with the same trouble as me.