Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改lua脚本中地图类型的aerospike db记录中的bin值_Lua_User Defined Functions_Lua Table_Aerospike - Fatal编程技术网

更改lua脚本中地图类型的aerospike db记录中的bin值

更改lua脚本中地图类型的aerospike db记录中的bin值,lua,user-defined-functions,lua-table,aerospike,Lua,User Defined Functions,Lua Table,Aerospike,假设aerospike数据库记录了如下数据 让名称空间为employee name age characteristics sachin 25 MAP('{"weight":70, "height":25}') 现在我想通过lua脚本更改employee名称空间中所有记录的map内部的height值 我已尝试更改以下正常数据类型的存储箱,即 尝试按以下方式更改年龄: function changeAgeOfEmployee(rec) if not aerospike:exists(

假设aerospike数据库记录了如下数据

让名称空间为employee

name age characteristics  
sachin 25 MAP('{"weight":70, "height":25}')  
现在我想通过lua脚本更改employee名称空间中所有记录的map内部的height值

我已尝试更改以下正常数据类型的存储箱,即 尝试按以下方式更改年龄:

function changeAgeOfEmployee(rec)
  if not aerospike:exists(rec) then
     error ("Invalid Record. Returning")
     return
  else
     age = 30
     rec['age'] = age
     aerospike:update(rec)
  end
end

但是我不知道如何在lua中更改map中的值,是否有人可以在这方面帮助我

您的
map
数据类型基本上是一个lua表。lua中的映射可以写成:

local m = map {"weight" => 70, "height" => 25}
要循环遍历所有键/值对,应使用以下命令:

for key, value in map.pairs(m) do
    m[key] = 30 --this changes all the values of your MAP to 30
end

如果要修改映射的键或列表的索引,则应将该bin强制转换为局部变量,然后在更新之前将其设置回记录

function changes(rec)
  rec['i'] = 99
  local m = rec['m']
  m['a'] = 66
  rec['m'] = m
  aerospike:update(rec)
end
在AQL中

$ aql
Aerospike Query Client
Version 3.15.1.2
C Client Version 4.3.0
Copyright 2012-2017 Aerospike. All rights reserved.
aql> register module './test.lua'
OK, 1 module added.
aql> select * from test.demo where PK='88'
+----+-------+--------------------------------------+------------------------------------------+
| i  | s     | m                                    | l                                        |
+----+-------+--------------------------------------+------------------------------------------+
| 88 | "xyz" | MAP('{"a":2, "b":4, "c":8, "d":16}') | LIST('[2, 4, 8, 16, 32, NIL, 128, 256]') |
+----+-------+--------------------------------------+------------------------------------------+
1 row in set (0.002 secs)

aql> execute test.changes() on test.demo where PK='88'
+---------+
| changes |
+---------+
|         |
+---------+
1 row in set (0.001 secs)

aql> select * from test.demo where PK='88'
+----+-------+---------------------------------------+------------------------------------------+
| i  | s     | m                                     | l                                        |
+----+-------+---------------------------------------+------------------------------------------+
| 99 | "xyz" | MAP('{"a":66, "b":4, "c":8, "d":16}') | LIST('[2, 4, 8, 16, 32, NIL, 128, 256]') |
+----+-------+---------------------------------------+------------------------------------------+
1 row in set (0.000 secs)

看Aerospike。谢谢@Ronen Botzer!谢谢你花宝贵的时间回答这个问题,同样的事情我也尝试过,它对我很有效,非常感谢,祝你玩得愉快