How do I get the Primary Key (PK) using AQL?

Summary

To retrieve the primary key (PK) in a record using AQL, KEY_SEND needs to be set to TRUE (it’s false by default) prior to inserting the record. PK is an internal designation used by AQL for the user-specified primary key. Internally, Aerospike does not store the user-specified primary key if KEY_SEND is not set to TRUE. It only stores the digest.

Note: RECORD_PRINT_METADATA does not need to be set to TRUE because the PK is returned by default if stored.

Example

Set key_send to true first.

aql> SET KEY_SEND TRUE
KEY_SEND = true

Then insert record via AQL, AQL internally (uses C client) sets write policy attribute sendKey to true. Your key (“key2” below) is then stored in the record.

aql> INSERT INTO test.demo (PK, foo, bar) VALUES ('key2', 456, 'abc')
[
    [
        {
          "Status": 0,
          "Message": "1 record affected."
        }
    ]
]

aql> select * from test.demo where pk='key2'
[
    [
        {
          "PK": "key2",
          "foo": 456,
          "bar": "abc"
        }
    ],
    [
        {
          "Status": 0
        }
    ]
]

Note: that also works with record_print_metadata set to be TRUE.

aql> set record_print_metadata TRUE
RECORD_PRINT_METADATA = true

aql> select * from test.demo where pk='key2'
[
    [
        {
          "PK": "key2",
          "edigest": "N/A",
          "ttl": -1,
          "gen": 1,
          "bins": {
            "foo": 456,
            "bar": "abc"
          }
        }
    ],
    [
        {
          "Status": 0
        }
    ]
]

References

Keywords

PK Primary Key AQL KEY_SEND

Timestamp

June 2020

© 2015 Copyright Aerospike, Inc. | All rights reserved. Creators of the Aerospike Database.