I'm trying to retrieve the Primary key in the Aerospike node.js client using client.get(). I have inserted the records using client.put() by changing the policy to Aerospike.policy.key.SEND as mentioned here and here.
Now I want to retrieve the records along with the Primary key. I tried doing it like this as mentioned in the Aerospike Documentation but it doesn't seem to work.
var key = aerospike.key(aerospikeDBParams.defaultNamespace,aerospikeDBParams.defaultSet,count);
var readpolicy = {
key: aerospike.policy.key.SEND
}
client.get(key, readpolicy, function(err, rec, meta){}
I get all the bins but not the Primary key. Am I missing something here?
Thanks in advance.
Get instructions for running Aerospike on cloud providers, or compiling the server from source. Client APIs are supported with idiomatic interfaces for easy integration with your application. You can easily monitor your Aerospike Database cluster with the Aerospike Monitoring Stack or using the Aerospike Admin (asadm) command line tool.
You can easily monitor your Aerospike Database cluster with the Aerospike Monitoring Stack or using the Aerospike Admin (asadm) command line tool. Need Premium Features?
Aerospike's Spark connector allows InputFormat and OutputFormat usage, so you can use Spark's faster framework for running analytics. Aerospike community's Hadoop connector allows you to pull data directly from Hadoop clusters in order to analyze or check any form of unstructured data.
The fourth paramater of function is information about primary key you want.
Take my code as example:
var readpolicy = {
key: Aerospike.policy.key.SEND
}
var key = new Aerospike.Key(ns, set, "sel-fish")
client.get(key, readpolicy, function (err, record, metadata, key) {
if (null == err) {
console.log("get ok")
console.log(record)
console.log(metadata)
console.log(key)
}
})
The output is :
get ok
{ uid: 1000,
name: 'sel-fish',
dob: { mm: 12, dd: 29, yy: 1995 },
friends: [ 1001, 1002, 1003 ],
avatar: <Buffer 0a 0b 0c> }
{ ttl: 431997, gen: 3 }
Key { ns: 'test', set: '14', key: 'sel-fish', digest: null }
The version of driver is [email protected]
You're looking at the documentation of the older 1.x client. The documentation for the 2.x client is at http://www.aerospike.com/apidocs/nodejs/
As long as the key is stored in the write operation, you should be able to get it with subsequent reads.
const Aerospike = require('aerospike')
function assertOk (error, message) {
if (error) {
console.error('ERROR - %s: %s [%s]\n%s', message, error.message, error.code, error.stack)
throw error
}
}
const Key = Aerospike.Key
Aerospike.connect({ hosts: '127.0.0.1:3000' }, function (error, client) {
assertOk(error, 'Connecting to Aerospike cluster')
var key = new Aerospike.Key('test', 'demo', 1)
var bins = { a: 1, b: 2 }
var policy = {
key: Aerospike.policy.key.SEND
}
client.put(key, bins, {}, policy, function (error) {
assertOk(error, 'Writing database record')
client.get(key, function (error, record, meta) {
assertOk(error, 'Reading database record')
console.log(record, meta)
})
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With