0
down vote
favorite
I have a requirement where I have to find the record in an aerospike
based on attributeId. The data in aerospike is inthe below format
{
name=ABC,
id=xyz,
ts=1445879080423,
inference={2601=0.6}
}
Now I will be getting the value “2601” programatically and I should
find this record based on this value. But the problem is the value is in
a Map and the size of this map may be more than 1 like
inference={{2601=0.6},{2830=0.9},{2931=0.8}}
So how can I find this record using attributeId in java. Any suggestions much appreciated
HI Sathiya
You could use a Query on a secondary index providing it meets your Latency requirements. Secondary indexes can be created on String or Integer values. These can be in a Bin or they can be List values Map keys or Map values (only at the top level).
For your case, you could create an index like this using AQL:
CREATE MAPKEYS INDEX ON . (inference) STRING
you can then do a query using a Statement with an equality filter on ‘inference’
I hope this helps
Cross-linking to the same question asked on Stackoverflow here.
Hi @helipilot50 I am not very clear about what you have explained as I am a beginner in aerospike. For now I have found a workaround. I think I may better understand if I can get an example of how to do this. Thanks for the answer
Hi @helipilot50 I tried using the code which you shared with me. I created index using code for the inferences bin. Here is the result of “show indexes”
aql> show indexes
+------------------+--------------+-----------+------------------+----------+-------+--------------+--------------+------------+-----------+
| ns | bins | indextype | set | num_bins | state | indexname | path | sync_state | type |
+------------------+--------------+-----------+------------------+----------+-------+--------------+--------------+------------+-----------+
| "knowledge-base" | "updateTD" | "NONE" | "app-inferences" | 1 | "RW" | "updateTime" | "updateTD" | "synced" | "NUMERIC" |
| "knowledge-base" | "inferences" | "MAPKEYS" | "app-inferences" | 1 | "WO" | "inf_index" | "inferences" | "synced" | "STRING" |
+------------------+--------------+-----------+------------------+----------+-------+--------------+--------------+------------+-----------+
I created index for map keys. Now when I try to get data using this index using below code I get > com.aerospike.client.AerospikeException: Error Code 203: Index not readable
ScanPolicy policy = new ScanPolicy();
Statement stmt = new Statement();
stmt.setNamespace(namespace);
stmt.setSetName(set);
stmt.setFilters(Filter.contains("inferences",
IndexCollectionType.MAPKEYS, "1201"));
RecordSet recordSet = client.query(null, stmt);
try {
while (recordSet.next()) {
Key s = recordSet.getKey();
}
} catch (Exception e) {
//getting exception here
System.out.println();
}
I am not sure what this error indicates. I searched online but there are no docs related to this error. It will be of great help if you can throw some light on this. Thanks a lot in advance
Hi Sathiya
Error Code 203: Index not readable means that this index is in the process of being created. You can see “WO” under the state column, this means “Write Only”. Once the index is created, it state should change to “RW” and you should not get the 203 error.
Ping me if you need more help
Peter
1 Like
Hi @helipilot50 Thanks a lot. As you pointed out the index state changed to RW now and I am not getting the exception. But when I try to iterate thru the record set, I am not getting any record from the query. To be specific I have a value 2508 as Map key in my aerospike. When I try to get the data from aerospike using this index I am not getting any data. But I verified that the data exists in aerospike. In my case the data was already present in aerospike and I am creating index just now. Can this be the cause of this issue? Thanks in advance.