为Aerospike编写聚合(groupBy, orderBy) Lua脚本的指导。
2015-10-19 16:5:16
收藏:0
阅读:78
评论:1
我有一个 Lua 脚本,它以 'sensorType' 为依据分组数据,并在每个 'sensorType' 组中打印 'clientId'。
function orderby(touples)
local function mapper(rec)
local element = map()
element["clientId"] = rec["clientId"];
element["sensorType"] = rec["sensorType"]
return element
end
local function accumulate(currentList, nextElement)
local sensorType = nextElement["sensorType"]
local clientId = nextElement["clientId"]
if currentList[sensorType] == nil then
currentList[sensorType] = list()
end
list.append(currentList[sensorType],clientId)
return currentList
end
local function mymerge(a, b)
return list.merge(a, b)
end
local function reducer(this, that)
return map.merge(this, that, mymerge)
end
return touples:map(mapper):aggregate(map{}, accumulate):reduce(reducer)
end
我还想按 'groupBy sensorType, clientId' 进行分组。请帮我准备一个可以接受任意组别子句列数并进行分组的脚本。
目前我的结果是-
{ BEACON: [ 'client2', 'client2', 'client2', 'client2', 'client2', 'client2' ],
SSID:
[ '100',
'100',
'100',
'100',
'100',
'100',
'100',
'102',
'100',
'100',
'101',
'100' ] }
我想要的结果是这个格式-
{ BEACON: [ 'client2' ],
SSID:
[ '100',
'102',
'101', ] }
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
在您的函数
accumulate中,clientId无条件添加到currentList中。如果您不想在currentList中有冗余数据,则需要检查clientId是否属于currentList。如果您使用的是列表而不是集合,则这有点棘手;您需要逐个测试每个元素:
local t = currentList[sensorType] local alreadyInList = false for i = 1, #t do if t[i] == clientId then alreadyInList = true end end if not alreadyInList then list.append(t, clientId) end这会比较慢——随着
currentList[sensorType]的增长,测试其是否已经包含您正在尝试添加的元素也将需要更长的时间。在许多情况下,这没有多大区别,但是使用 set 而不是列表将更快(也更容易)。在 Lua 中,集合非常容易,因为任何东西都可以作为 Lua 表的键,甚至是另一个表。这是您如何使用表作为集合而不是列表:--将一个空表初始化为集合 if currentSet[sensorType] == nil then currentSet[sensorType] = {} end --将数据添加到集合 `currentList[sensorType]` 中 currentSet[sensorType][clientId] = true --在适当的时候,将集合重新转换为列表 convertedList = {} i = 0 for key in pairs(currentSet[sensorType]) do i = i + 1 convertedList[i] = key end转换之前,
currentSet如下所示:{ [sensorType] = { ["100"] = true , ["101"] = true , ["102"] = true } }转换后,
convertedList如下所示:{ "100", "102", "101" }(请注意,
convertedList可以以任何顺序出现,因为 Lua 表内键的顺序是未定义的。)