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

The Aerospike Knowledge Base has moved to https://support.aerospike.com. Content on https://discuss.aerospike.com is being migrated to either https://support.aerospike.com or https://docs.aerospike.com. Maintenance on articles stored in this repository ceased on December 31st 2022 and this article may be stale. If you have any questions, please do not hesitate to raise a case via https://support.aerospike.com.

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