【问题标题】:changing the value of bin in a record of aerospike db which is of map type in lua script更改 lua 脚本中地图类型的 aerospike db 记录中 bin 的值
【发布时间】:2018-01-22 10:21:12
【问题描述】:

说 aerospike 数据库正在记录如下数据

让命名空间为员工

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

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

我已经尝试改变正常数据类型的 bin 如下,i,e i 尝试将年龄更改如下:

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 中更改地图中的值,有人可以帮我解决这个问题

【问题讨论】:

    标签: lua user-defined-functions lua-table aerospike


    【解决方案1】:

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

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

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

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

    【讨论】:

    【解决方案2】:

    如果您要修改地图的键或列表的索引,则应将该 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)
    

    【讨论】:

    • 感谢您花费宝贵的时间回复此问题,我尝试过同样的方法,它对我有用,非常感谢,玩得开心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多