Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aerospike: how do I get record key?

Aerospike client has scanAll method for reading all rows from it's store. I use it in the folowing code:

ScanPolicy policy = new ScanPolicy();
policy.concurrentNodes = true;
policy.priority = Priority.DEFAULT;
policy.includeBinData = true;
policy.scanPercent = 100;

client.scanAll(policy, "namespaceName", "setName", new ScanCallback() {
    @Override
    public void scanCallback(Key key, Record record) throws AerospikeException {
        STORE.put(key.userKey.toLong(), record.getValue("binName").toString());
    }
});

But it is finished with NullPointerException, because userKey is null. All other fields are valid as expected. User key is the Long value, that was used for saving data:

client.put(writePolicy, new Key("namespaceName", "setName", userKey), new Bin("binName", value));

All is fine, if I do single request like this:

client.get(readPolicy, new Key("namespaceName", "setName", userKey));

What may be wrong? Why userKey is null?

like image 534
DmitryKanunnikoff Avatar asked Dec 01 '14 13:12

DmitryKanunnikoff


People also ask

How do I find my primary key for Aerospike?

I opened a feature request internally, TOOLS-551 and TOOLS-539. Keep an eye out for a mention of it in the release notes. option. When you create records via AQL after setting KEY_SEND true, (default is false), the primary key is stored as a BIN and then you can retrieve it via the record.

How do I get all of my records from Aerospike?

To get all records belonging to a set, you can use scan API and pass the required set. To get all records with a specific bin value, you can use secondary index. Please Refer to the doc for details.

What is the record key?

A record key is a set of record fields that is used to ensure the uniqueness of register records. A register cannot contain two records with identical values in all key fields. Record keys are also used for record identification (for example, if you need to find a specific list item).

What is Aerospike key?

A key uniquely identifies a record in the Aerospike database within a given namespace.


2 Answers

Aerospike uses key and set name to generate unique digest, So it stores only digest.

While inserting one record if you set writePolicy.sendKey = true then key will be stored as metadata of record. If one record is inserted with writePolicy.sendKey = true then only you will get key in scanCallback().

By default writePolicy.sendKey is false, so by default scanCallback() gets null as key. Thats why your key.userKey.toLong() gives NullPointerException.

like image 171
Jyoti Ranjan Adhikary Avatar answered Oct 18 '22 18:10

Jyoti Ranjan Adhikary


I too have faced this problem even at the time of writing we were setting the WritePolicy.sendkeys=true.

After 2-3 days debugging found that there were some issue with aerospike client version. Initially i were using 3.0.25 but after upgrading it to 3.0.35 it started working fine.

like image 3
krishna Avatar answered Oct 18 '22 19:10

krishna