Radhika.S
Radhika.S

Reputation: 395

How to fetch primary key in Aerospike Node.js Client

I'm trying to fetch all records along with Primary key from Aerospike. I tried using client.query functionality as below

var query = client.query(aerospikeDBParams.dbName,"testRecords");
var stream = query.execute();

with this I'm getting all the fields except the Primary key.How can i get Primary key along with the other fields?

Thanks in advance

Upvotes: 4

Views: 413

Answers (1)

Ronen Botzer
Ronen Botzer

Reputation: 7117

You first need to actually store the primary key with the record. The client and server locate the record using its digest, which the client hashes using the (namespace, set, primary-key) information. The default value for the key write policy is Aerospike.policy.key.DIGEST. You will need to explicitly set that to Aerospike.policy.key.SEND instead.

See the Aerospike:module documentation. It contains this example:

// global policy, applied to all commands that do not override it
var config = {
  policies: {
    timeout: 100,
    retry: Aerospike.policy.retry.ONCE
  }
}

Aerospike.connect(config, (error, client) => {
  if (error) throw error

  var key = new Aerospike.Key('test', 'demo', 'k1')
  var record = {i: 1234}

  // override policy for put command
  var policy = {
    exists: Aerospike.policy.exists.CREATE,
    key: Aerospike.policy.key.SEND
  }

  client.put(key, record, {}, policy, (error) => {
    if (error && error.code === Aerospike.status.AEROSPIKE_ERR_RECORD_EXISTS) {
      console.info('record already exists')
    } else if (error) {
      throw error
    }
    client.close()
  })
})

When the keys are stored with the record a query will return them.

Upvotes: 3

Related Questions