Aerospike::get - gets a record from the Aerospike database
public int Aerospike::get ( array $key, array &$record [, array $select [, array $options]] )
Aerospike::get() will read a record with a given key, where the record is filled with an array of bins and values. The bins returned in record can be filtered by passing a select array of bin names. Non-existent bins will appear in the record with a NULL value.
key the key under which the record can be found. An array with keys ['ns','set','key'] or ['ns','set','digest'].
record an array of key, metadata, and bins:
Array:
key => Array
ns => namespace
set => set name
key => the primary index key
digest => a RIPEMD-160 hash of the key, and always present
meta => Array
ttl => time in seconds until the record expires
generation => reflects the number of times the record has been altered
bins => Array of bin-name => value pairs
select an array of bin names which are the subset to be returned.
options including
- Aerospike::OPT_READ_TIMEOUT
- Aerospike::OPT_POLICY_KEY
- Aerospike::OPT_POLICY_CONSISTENCY
- Aerospike::OPT_POLICY_REPLICA
Returns an integer status code. Compare to the Aerospike class status constants. When non-zero the Aerospike::error() and Aerospike::errorno() methods can be used.
<?php
$config = array("hosts"=>array(array("addr"=>"localhost", "port"=>3000)));
$db = new Aerospike($config);
if (!$db->isConnected()) {
echo "Aerospike failed to connect[{$db->errorno()}]: {$db->error()}\n";
exit(1);
}
$key = $db->initKey("test", "users", 1234);
$status = $db->get($key, $record);
if ($status == Aerospike::OK) {
var_dump($record);
} elseif ($status == Aerospike::ERR_RECORD_NOT_FOUND) {
echo "A user with key ". $key['key']. " does not exist in the database\n";
} else {
echo "[{$db->errorno()}] ".$db->error();
}
?>
We expect to see:
array(3) {
["key"]=>
array(4) {
["digest"]=>
string(40) "436a3b9fcafb96d12844ab1377c0ff0d7a0b70cc"
["namespace"]=>
NULL
["set"]=>
NULL
["key"]=>
NULL
}
["meta"]=>
array(2) {
["generation"]=>
int(3)
["ttl"]=>
int(12345)
}
["bins"]=>
array(3) {
["email"]=>
string(9) "[email protected]"
["name"]=>
string(9) "You There"
["age"]=>
int(33)
}
}
<?php
// assuming this follows Example #1
// Getting a filtered record
$filter = array("email", "manager");
unset($record);
$status = $db->get($key, $record, $filter);
if ($status == Aerospike::OK) {
var_dump($record);
} else {
echo "[{$db->errorno()}] ".$db->error();
}
?>
We expect to see:
array(3) {
["key"]=>
array(4) {
["digest"]=>
string(40) "436a3b9fcafb96d12844ab1377c0ff0d7a0b70cc"
["namespace"]=>
NULL
["set"]=>
NULL
["key"]=>
NULL
}
["meta"]=>
array(2) {
["generation"]=>
int(3)
["ttl"]=>
int(12344)
}
["bins"]=>
array(2) {
["email"]=>
string(15) "[email protected]"
["manager"]=>
NULL
}
}