Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Primary Key from Aerospike node.js client

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.

like image 262
Abhijith S Avatar asked May 05 '16 12:05

Abhijith S


People also ask

How do I run aerospike on the cloud?

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.

How do I monitor my Aerospike database cluster?

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?

What is the difference between aerospike's spark and Hadoop connectors?

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.


2 Answers

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]

like image 192
sel-fish Avatar answered Oct 26 '22 17:10

sel-fish


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)
    })
  })
})
like image 2
Ronen Botzer Avatar answered Oct 26 '22 17:10

Ronen Botzer