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

时间:2017-01-25 15:33:38

标签: aerospike

我看到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?

3 个答案:

答案 0 :(得分:3)

以下是一些示例代码,可帮助您入门: 同样在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]

答案 1 :(得分:2)

查看客户端的Python文档。

必须是3.8.4+版本 创建地图政策: 定义一个关键的有序或键值有序策略 {_ 3}}用于map_order

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

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

答案 2 :(得分:0)

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

enter image description here