Redis的Lua脚本,它总结了键的值

时间:2022-10-06 09:47:57

I am building out my first Redis server side script (for debugging) and my lack of Lua experience has me quite stuck.

我正在构建我的第一个Redis服务器端脚本(用于调试),而我缺乏Lua经验让我非常困难。

Essentially have a dataset of K/V pairs (containing ~1000 values) from which I want to list all the KEYS that match a pattern. For example in redis-cli:

基本上有一个K / V对的数据集(包含〜1000个值),我想从中列出所有匹配模式的KEYS。例如在redis-cli中:

> KEYS "carlos:*"
1) "carlos:1"
2) "carlos:2"
3) "carlos:3"
4) "carlos:4"

Based on the above output I want to return the sum of those keys by executing a Lua script. Currently I have the following on my sum.lua

根据上面的输出,我想通过执行Lua脚本返回这些键的总和。目前我在sum.lua上有以下内容

local sum = 0
local matches = redis.call('KEYS', 'carlos:*')

for unpack(matches)
   sum = sum + redis.call('GET', matches)
end

return sum

While the above script is likely incorrect, trying even redis.call('KEYS', 'carlos:*') by itself produces the following error

虽然上面的脚本可能不正确,但尝试redis.call('KEYS','carlos:*')本身会产生以下错误

root@carlos:~# redis-cli EVAL "$(cat sum.lua)"

root @ carlos:〜#redis-cli EVAL“$(cat sum.lua)”

(error) ERR wrong number of arguments for 'eval' command

(错误)ERR'eval'命令的参数数量错误

I have tried a number of iterations of my syntax to no avail. Any ideas?

我尝试过多次迭代我的语法无济于事。有任何想法吗?

Thanks

谢谢

1 个解决方案

#1


20  

  1. EVAL requires a minimum of two arguments; the script and the number of keys you are passing to the script. In this case, you are passing zero keys, meaning the script can be invoked as follows:

    EVAL至少需要两个参数;脚本和传递给脚本的键数。在这种情况下,您传递的是零键,这意味着可以按如下方式调用脚本:

    redis-cli EVAL "$(cat sum.lua)" 0
    

    or:

    要么:

    redis-cli --eval sum.lua
    
  2. Your loop structure for iterating over the values returned from KEYS was incorrect; I have fixed it for you.

    用于迭代从KEYS返回的值的循环结构不正确;我已经为你修好了。

  3. You need to convert the value returned from GET from a string to a number using Lua's tonumber function.

    您需要使用Lua的tonumber函数将从GET返回的值从字符串转换为数字。

With the above changes made, the following script should work for you:

完成上述更改后,以下脚本应该适用于您:

local sum = 0
local matches = redis.call('KEYS', 'carlos:*')

for _,key in ipairs(matches) do
    local val = redis.call('GET', key)
    sum = sum + tonumber(val)
end

return sum

#1


20  

  1. EVAL requires a minimum of two arguments; the script and the number of keys you are passing to the script. In this case, you are passing zero keys, meaning the script can be invoked as follows:

    EVAL至少需要两个参数;脚本和传递给脚本的键数。在这种情况下,您传递的是零键,这意味着可以按如下方式调用脚本:

    redis-cli EVAL "$(cat sum.lua)" 0
    

    or:

    要么:

    redis-cli --eval sum.lua
    
  2. Your loop structure for iterating over the values returned from KEYS was incorrect; I have fixed it for you.

    用于迭代从KEYS返回的值的循环结构不正确;我已经为你修好了。

  3. You need to convert the value returned from GET from a string to a number using Lua's tonumber function.

    您需要使用Lua的tonumber函数将从GET返回的值从字符串转换为数字。

With the above changes made, the following script should work for you:

完成上述更改后,以下脚本应该适用于您:

local sum = 0
local matches = redis.call('KEYS', 'carlos:*')

for _,key in ipairs(matches) do
    local val = redis.call('GET', key)
    sum = sum + tonumber(val)
end

return sum