在Aerospike中查询地图的关键

时间:2016-04-03 22:22:16

标签: aerospike aql

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

首先,我在bin上创建了一个Index,我正在存储地图

 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

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

2 个答案:

答案 0 :(得分:6)

除了Numeric,String和Geo2DSphere类型之外,Aerospike还支持MapKeys,MapValues,Lists上的二级索引。

对于您的场景,您可以按如下方式查询Mapkey。

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

这应该返回结果。

在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的查询。

答案 1 :(得分:4)

Update:

As of Aerospike 3.8.1, Secondary Index on List and Map are officially supported.


Original Response:

Query by secondary index on map keys, map values, or list values are not officially supported yet.

That said, the functionality and syntax is somewhat available. You need to:

  1. Create a secondary index with type MAPKEYS, MAPVALUES or LIST (you're using type STRING at the moment)

  2. Select as follows (you're missing the IN MAPKEYS part):

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

The query syntax, as well as some other bits, is available if you type help while in the AQL console.