我得到了我在RIAK中存储的Map数据类型。这是包含少数包含计数器的地图的地图。 序列化到json程序的预期输出将是:
{
"sample_map_key" : {
"maps" : {
"Q1" : {
"counters" : {
"a1" : 10,
"a2" : 13
}
},
"Q2" : {
"counters" : {
"a1":31,
"a2":21,
"a3":10
}
}
}
}
但我得到了异常:“TypeError:unhashable type:'Map'”。 谢谢你的帮助。以下是示例代码:
from riak.datatypes import Map
import riak
def main():
store_values()
read_values()
def store_values():
riak_client = riak.RiakClient(host="localhost", http_port="8087", protocol="pbc")
mapBucketType = riak.BucketType(riak_client, "maps")
maps_bucket = riak.RiakBucket(riak_client, "sample_maps_bucket", mapBucketType)
sample_map = Map(maps_bucket, "sample_map_key")
sample_map.maps["Q1"].counters["a1"].increment(10)
sample_map.maps["Q1"].counters["a2"].increment(13)
sample_map.maps["Q2"].counters["a1"].increment(31)
sample_map.maps["Q2"].counters["a2"].increment(21)
sample_map.maps["Q2"].counters["a3"].increment(10)
sample_map.store()
def read_values():
riak_client = riak.RiakClient(host="localhost", http_port="8087", protocol="pbc")
mapBucketType = riak.BucketType(riak_client, "maps")
maps_bucket = riak.RiakBucket(riak_client, "sample_maps_bucket", mapBucketType)
sample_map = Map(maps_bucket, "sample_map_key")
sample_map.reload()
results = {"sample_map_key": to_json(sample_map)}
print results
def to_json(map):
obj = {}
for m in map.maps:
if "maps" not in obj:
obj["maps"] = {}
obj["maps"][m] = to_json(map.maps[m])
for c in map.counters:
if "counters" not in obj:
obj["counters"] = {}
obj["counters"][c] = map.counters[c]
return obj
if __name__ == '__main__':
main()
完整追溯:
Traceback (most recent call last): File "sampleMap.py", line 48, in <module>
main() File "sampleMap.py", line 5, in main
read_values() File "sampleMap.py", line 31, in read_values
results = {"sample_map_key": to_json(sample_map)} File "sampleMap.py", line 40, in to_json
obj["maps"][m] = to_json(map.maps[m]) File "/usr/local/lib/python2.7/dist-packages/riak/datatypes/map.py", line 26, in __getitem__
return self.map[(key, self.datatype)] File "/usr/local/lib/python2.7/dist-packages/riak/datatypes/map.py", line 171, in __getitem__
if key in self._value: TypeError: unhashable type: 'Map'
答案 0 :(得分:1)
确定。我搞定了。似乎RIAK库和描述之间存在不一致。迭代map.maps并不是产生键而是值。但是,如果你迭代地图本身,你会得到键/类型。以下是工作代码:
from riak.datatypes import Map
import riak
def main():
store_values()
read_values()
def store_values():
riak_client = riak.RiakClient(host="localhost", http_port="8087", protocol="pbc")
mapBucketType = riak.BucketType(riak_client, "maps")
maps_bucket = riak.RiakBucket(riak_client, "sample_maps_bucket", mapBucketType)
sample_map = Map(maps_bucket, "sample_map_key")
sample_map.maps["Q1"].counters["a1"].increment(10)
sample_map.maps["Q1"].counters["a2"].increment(13)
sample_map.maps["Q2"].counters["a1"].increment(31)
sample_map.maps["Q2"].counters["a2"].increment(21)
sample_map.maps["Q2"].counters["a3"].increment(10)
sample_map.store()
def read_values():
riak_client = riak.RiakClient(host="localhost", http_port="8087", protocol="pbc")
mapBucketType = riak.BucketType(riak_client, "maps")
maps_bucket = riak.RiakBucket(riak_client, "sample_maps_bucket", mapBucketType)
sample_map = Map(maps_bucket, "sample_map_key")
sample_map.reload()
results = {"sample_map_key": to_json(sample_map)}
print results
def to_json(map):
obj = {}
for m, type in map:
if (type == "map"):
if "maps" not in obj:
obj["maps"] = {}
obj["maps"][m] = to_json(map.maps[m])
if (type == "counter"):
if "counters" not in obj:
obj["counters"] = {}
obj["counters"][m] = map.counters[m].value
return obj
if __name__ == '__main__':
main()