Skip to content

Read

To read a record from the database, the Aerospike Rust client library can:

  • Read all bins in a record.
  • Read specified bins in a record.
  • Read only the metadata of a record.

Each method returns a Record struct that contains the metadata and bins of the record.

Read all bins in a record

This example reads all bins for a record. We match against KeyNotFoundError to handle missing records.

let key = as_key!("test", "myset", "mykey");
match client.get(&ReadPolicy::default(), &key, Bins::All).await {
Ok(record)
=> println!("name={:?}", record.bins.get("name")),
Err(Error(ErrorKind::ServerError(ResultCode::KeyNotFoundError), _))
=> println!("No such record: {}", key),
Err(err)
=> println!("Error fetching record: {}", err),
}

📖 API Reference: Client::get | ReadPolicy::default | Bins

Read specific bins of a record

This example only reads two bins from the record: name and age:

let key = as_key!("test", "test", "mykey");
match client.get(&ReadPolicy::default(), &key, ["name", "age"]).await {
Ok(record) => {
println!("name={:?}", record.bins.get("name"));
println!("age={:?}", record.bins.get("age"));
}
Err(Error::ServerError(ResultCode::KeyNotFoundError, ..)) => {
println!("No such record: {}", key);
}
Err(err) => {
println!("Error fetching record: {}", err);
}
}

📖 API Reference: Client::get | ReadPolicy::default

Read record metadata

Use Bins::None to fetch only the record’s metadata. The following example retrieves the record’s remaining time-to-live:

let key = as_key!("test", "myset", "mykey");
match client.get(&ReadPolicy::default(), &key, Bins::None).await {
Ok(record) => {
match record.time_to_live() {
None => println!("record never expires"),
Some(duration) => println!("ttl: {} secs", duration.as_secs()),
}
}
Err(Error::ServerError(ResultCode::KeyNotFoundError, ..)) => {
println!("No such record: {}", key);
}
Err(err) => {
println!("Error fetching record: {}", err);
}
}

📖 API Reference: Client::get | Record::time_to_live

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?