Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...