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

changing the value of bin in a record of aerospike db which is of map type in lua script

假设 aerospike 数据库正在记录如下数据

命名空间设为 employee

name age characteristics  
sachin 25 MAP('{"weight":70, "height":25}')  

现在我想通过 lua 脚本更改员工命名空间中所有记录的地图内部的高度值。

我试过如下更改正常数据类型的 bin,我,我 尝试更改年龄如下:

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 数据类型基本上是 lua table。 lua中的MAP可以写成:

local m = map {"weight" => 70, "height" => 25}

要遍历所有 key/value 对,您应该像这样使用 pairs iterator

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)