我正在尝试将 map 存储在 aerospike 中并根据 map 的键获取数据。
首先我在存储 map 的 bin 上创建了一个索引
aql> create mapkeys index status on test.myset (state) String
aql> show indexes
+--------+---------+-----------+---------+-------+-----------+---------+------------+----------+
| ns | bin | indextype | set | state | indexname | path | sync_state | type |
+--------+---------+-----------+---------+-------+-----------+---------+------------+----------+
| "test" | "state" | "MAPKEYS" | "myset" | "RW" | "status" | "state" | "synced" | "STRING" |
+--------+---------+-----------+---------+-------+-----------+---------+------------+----------+
1 row in set (0.000 secs)
OK
然后我用java客户端来存储 map
AerospikeClient client = new AerospikeClient("127.0.0.1",3000);
WritePolicy writePolicy = new WritePolicy();
writePolicy.timeout=500;
for(int i = 1;i<10;i++){
Key key = new Key("test","myset",""+i);
client.delete(writePolicy, key);
HashMap<String,String> map = new HashMap<String,String>();
map.put("key1", "string1");
map.put("key2", "string2");
map.put("key3", "string3");
Bin bin = new Bin("state", map);
client.put(writePolicy, key, bin);
}
我通过apl查看了数据,数据清晰存在。
aql> select * from test.myset
+--------------------------------------------------------+
| state |
+--------------------------------------------------------+
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
| {"key1":"string1", "key2":"string2", "key3":"string3"} |
+--------------------------------------------------------+
10 rows in set (0.019 secs)
现在,当我尝试根据创建的索引进行查询时,它给出了
aql> select * from test.myset where status = 'key1'
0 rows in set (0.000 secs)
Error: (204) AEROSPIKE_ERR_INDEX
aql> select * from test.myset where state = 'key1'
0 rows in set (0.000 secs)
Error: (201) AEROSPIKE_ERR_INDEX_NOT_FOUND
有人可以帮我解决这个问题吗?我搜索了那个错误,但没有找到任何信息。谢谢。
最佳答案
除了 Numeric、String 和 Geo2DSphere 类型之外,Aerospike 还支持 MapKeys、MapValues、Lists 的二级索引。
对于您的场景,您可以按如下方式查询 Mapkey。
select * from test.myset in mapkeys where state='key1'
这应该会返回结果。
在 AQL 中,如果您键入帮助,您应该得到以下查询
SELECT <bins> FROM <ns>[.<set>]
SELECT <bins> FROM <ns>[.<set>] WHERE <bin> = <value>
SELECT <bins> FROM <ns>[.<set>] WHERE <bin> BETWEEN <lower> AND <upper>
SELECT <bins> FROM <ns>[.<set>] WHERE PK = <key>
SELECT <bins> FROM <ns>[.<set>] IN <indextype> WHERE <bin> = <value>
SELECT <bins> FROM <ns>[.<set>] IN <indextype> WHERE <bin> BETWEEN <lower> AND <upper>
同样,您也可以对 MapValue 运行查询。
关于aerospike - 在 Aerospike 中查询 map 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391968/