从 Python 创建并插入 Aerospike 有序地图

Create, and insert into, an Aerospike ordered map from Python

我看到 documentation 用于附加到 Aerospike 中的列表,来自 Python,即:

key = ('test', 'demo', 1)
rec = {'coutry': 'India', 'city': ['Pune', 'Dehli']}

client.put(key, rec)

client.list_append(key, 'city', 'Mumbai')

但是我不知道如何在 Python 中向 Aerospike 中的地图添加元素,而且我也不知道如何将所述地图定义为 sorted

基本上我正在尝试按如下方式对时间序列建模:

  ticker1: {intepochtime1: some_number, intepochtime2: some_other_number,...}
  ticker2: {intepochtime1: some_number, intepochtime2: some_other_number,...}
  ........

其中代码是记录键,因此显然被索引,而且 intepochtimes 也是整数 JS 样式整数时间戳并且 也被索引 由于存储在升序或降序,因此很容易进行范围查询。 Python 如何做到这一点?

查看 Python 客户端文档。

必须是版本 3.8.4+ 创建地图策略: 定义键排序或键值排序策略之一 http://www.aerospike.com/apidocs/python/client.html#map-policies 对于 map_order

放置地图类型 bin 但首先定义地图策略。 http://www.aerospike.com/apidocs/python/client.html#id1 见 map_set_policy(key, bin, map_policy) 然后 map_put()

排序地图只是常规地图,但具有 map_order 政策。

这里有一些示例代码可以帮助您入门: 同样在 github 上:https://github.com/pygupta/aerospike-discuss/tree/master/stkovrflo_Py_SortedMaps

import aerospike
from aerospike import predicates as p

def print_result((key, metadata, record)):
   print(record)

config = { 'hosts': [ ("localhost", 3000), ] }
client = aerospike.client(config).connect()

map_policy={'map_order':aerospike.MAP_KEY_VALUE_ORDERED}

# Insert the records
key = ("test", "demo", 'km1')
client.map_set_policy(key, "mymap", map_policy)
client.map_put(key,  "mymap", '0', 13)
client.map_put(key,  "mymap", '1', 3)
client.map_put(key,  "mymap", '2', 7)
client.map_put(key,  "mymap", '3', 2)
client.map_put(key,  "mymap", '4', 12)
client.map_put(key,  "mymap", '5', 33)
client.map_put(key,  "mymap", '6', 1)
client.map_put(key,  "mymap", '7', 12)
client.map_put(key,  "mymap", '8', 22)


# Query for sorted value
print "Sorted by values, 2 - 14"
ret_val = client.map_get_by_value_range(key, "mymap", 2, 14, aerospike.MAP_RETURN_VALUE)
print ret_val

#get first 3 indexes 
print "Index 0 - 3"
ret_val2 = client.map_get_by_index_range(key, "mymap", 0, 3, aerospike.MAP_RETURN_VALUE)
print ret_val2



pgupta@ubuntu:~/discussRepo/aerospike-discuss/stkovrflo_Py_SortedMaps$ python sortedMapExample.py 
Sorted by values, 2 - 14
[2, 3, 7, 12, 12, 13]
Index 0 - 3
[13, 3, 7]

python3 内存泄漏已在客户端版本 2.0.8 中修复。