How to get the primary key in the query results?

I can do this with the client’s “get” method and adding option Aerospike::POLICY_KEY_SEND, but the “query” method does not have this option.

https://github.com/aerospike/aerospike-client-php/blob/master/doc/aerospike_query.md

It’s true that scan() and query() do not have an explicit key policy, but they will by default reflect the key if it was stored with the record by a previous put().

If a record does not have the key stored with it (due to an OPT_POLICY_KEY set to POLICY_KEY_DIGEST) you will see a NULL for the ‘key’ field of the record’s key, and if it was stored (by setting OPT_POLICY_KEY to POLICY_KEY_SEND) you will see the value.

For example:

Name: User 123
Email:me.123@example.com
Key: array (
  'ns' => 'test',
  'set' => 'users',
  'key' => NULL,
  'digest' => '?h?Sr????R[??v?2????',
)
Name: Roberto
Email:roberto@hal-inst.org
Key: array (
  'ns' => 'test',
  'set' => 'users',
  'key' => 2345,
  'digest' => '^???3?M??ꌱ?7vw0N??',
)

The key is being stored and returned if I use the get() with OPT_POLICY_KEY=POLICY_KEY_SEND. The problem is query() and scan() does not allow this OPT_POLICY_KEY option so the key returned is always NULL.

Again, down at the C client level there is no key policy for queries and scans. What happens is that the full key is reflected, which is a tuple of (namespace, set, key, digest). If you ever stored the record’s key with an explicit POLICY_KEY_SEND that data will come back. If you did not, it will be NULL in PHP, None in Python, etc. The two records above are an example I displayed using var_export().

If it behaves otherwise for you, it is a bug. Please open a new issue on our GitHub repo with details about your server version, client release, and OS. Sample code would help too. Thanks!

Hi, i was pretty happy to see this thread as it seemed to be the solution to my problem is was trying to fix for 3 hours now. But even if i try like you say it does not work. Please see my code and the output here. Im i missing something?

    $tab = "mytable";


    $characters = array("freudian.circuits@hal-inst.org" => 162,
        "philip.j.fry@planet-express.com" => 40,
        "tarunga.leela@planet-express.com" => 39,
        "amy.wong@planet-express.com" => 34,
        "hubert.j.farnsworth@planet-express.com" => 173,
        "bender.bending.rodriguez@planet-express.com" => 1055);

    $i=4;

    foreach ($characters as $email => $age) {

        $key = $this->db->initKey(self::NSPACE, $tab, "imastring_".$i++);

        $put_vals = array("email" => $email, "age" => $age);

        $status = $this->db->put($key, $put_vals, 0, array(\Aerospike::OPT_POLICY_KEY => \Aerospike::POLICY_KEY_SEND));

    }

    $key = $this->db->initKey(self::NSPACE, $tab,"imastring_".(4));

    $status = $this->db->get($key, $record);
    print_r($record);

Whatever i try, the key stays empty.

Array ( [key] => Array ( [ns] => temp_grid [set] => mytable [key] => [digest] => =ÅÝ÷ekÀU!+$E{ò­ 4` ) … )

Hmm. I ran this test script just now with the PHP client 3.4.2 against an Aerospike 3.6.2 server:

<?php
$config = ["hosts" => [["addr" => "192.168.119.3", "port" => 3000]]];

$client = new Aerospike($config);
$status = $client->createIndex("test", "test", "age", Aerospike::INDEX_TYPE_INTEGER, "age_index");

for ($i=1;$i<=1000;$i++) {
    $key = $client->initKey("test", "test", "$i");
    $bins = ["name" => "me-".$i, "age" => (int) $i];
    $client->remove($key);
    $client->put($key, $bins,  0, [Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND]);
}

$where = Aerospike::predicateBetween("age", 1, 200);

$status = $client->query("test", "test", $where, function ($record) {
    var_dump($record['key']);
});

$client->close();
?>

And I get back:

array(4) {
  ["ns"]=>
  string(4) "test"
  ["set"]=>
  string(4) "test"
  ["key"]=>
  string(3) "306"
  ["digest"]=>
  string(20) "?E
                ?2j?#Ei?????"
}
array(4) {
  ["ns"]=>
  string(4) "test"
  ["set"]=>
  string(4) "test"
  ["key"]=>
  string(3) "466"
  ["digest"]=>
  string(20) "?P?k????/????????V"
}
array(4) {
  ["ns"]=>
  string(4) "test"
  ["set"]=>
  string(4) "test"
  ["key"]=>
  string(3) "498"
  ["digest"]=>
  string(20) "i?䝡??????!?W??:"
}

The key field is a numeric string value between 0 and 1000, as expected. Can you try that for yourself? You may need to adjust the namespace if you removed ‘test’.

If that doesn’t work correctly, let me know which client and server releases are you using, and on which OS. Thanks.

Hi, thank you for the quick reply, i changed the database but still not getting the key. Even with your script 1:1. I will try some different ways. Anyway thanks again!

It would be good to get your aerospike.conf and information about which OS and client release you’re using. Also the release of the server you’re working with.