2015-09-04 41 views

回答

1

User-Defined Functions Lua写的扩展Aerospike的核心功能。您将创建一个stream UDF并将其附加到query

Aerospike中的流UDF的一个最佳实践是在将结果传递给UDF之前消除尽可能多的记录,因此在这种情况下,我将创建另一个bin来保存前缀(第一个字母或子字符串,在你的用例上),并建立一个二级索引。这个想法是,查询部分应该尽可能地返回尽可能小的子集。对于您的示例,前缀可以是单个字符,您可以向集合中的记录添加新的bin'firstchar',然后在其上创建一个辅助索引build

流UDF模块看起来是这样的:

local function range_filter(bin_name, substr_from, substr_to) 
    return function(record) 
     local val = record[bin_name] 
     if type(val) ~= 'string' then 
      return false 
     end 
     if val >= substr_from and val <= substr_to then 
      return true 
     else 
      return false 
     end 
    end 
end 

local function rec_to_map(record) 
    local xrec = map() 
    for i, bin_name in ipairs(record.bin_names(record)) do 
     xrec[bin_name] = xrec[bin_name] 
    end 
    return xrec 
end 


function str_between(stream, bin_name, substr_from, substr_to) 
    return stream : filter(range_filter(bin_name, substr_from, substr_to)) : map(rec_to_map) 
end 

在Python客户端你如下调用它:

import aerospike 
from aerospike import predicates as p 

# instantiate the client and connect to the cluster, then: 
query = client.query('test', 'this') 
query.where(p.equals('firstchar', 'a')) 
query.apply('strrangemod', 'str_between', ['a','az']) 
相关问题