查询 Aerospike 中地图的键

Querying on key of a Map in Aerospike

我正在尝试将地图存储在 aerospike 中并根据地图的键获取数据。

首先,我在存储地图的容器上创建了一个索引

 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客户端来存储地图

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

有人可以帮我解决这个问题吗?我搜索了那个错误,但没有找到任何信息。谢谢。

更新

自 Aerospike 3.8.1 起,Secondary Index on List and Map are officially supported


原始回复

按映射键、映射值或列表值的二级索引查询 not officially supported yet.

也就是说,功能和语法在一定程度上是可用的。您需要:

  1. 创建类型为 MAPKEYSMAPVALUESLIST 的二级索引(您目前使用的类型是 STRING

  2. Select 如下(您缺少 IN MAPKEYS 部分):

    SELECT * FROM namespace.setname IN MAPKEYS WHERE bin = 'keyValue'

如果您在 AQL 控制台中键入 help,查询语法以及一些其他位可用。

除了数字、字符串和 Geo2DSphere 类型之外,Aerospike 还支持 MapKeys、MapValues、列表的二级索引。

对于您的场景,您可以在Mapkey上查询如下。

select * from test.myset in mapkeys where state='key1'

这应该 return 结果。

在 AQL 中,如果您键入 help,您应该得到以下查询

    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。