Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Read

Jump to the Code block for a combined complete example.

Setup

The following examples will use the setup and record below to illustrate single record data retreival from an Aerospike database.

using Aerospike.Client;
// Define host configuration
Host config = new Host("127.0.0.1", 3000);
// Establishes a connection to the server
AerospikeClient client = new AerospikeClient(null, config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
Key key = new Key("sandbox", "ufodata", 5001);

The record structure:

+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
| PK | occurred | reported | posted | report | location |
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
| 5001 | 20220531 | 20220601 | 20220601 | MAP('{"shape":["circle", "flash", "disc"], "summary":"Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!", "city":"Ann Arbor", "state":"Michigan", "duration":"5 minutes"}') | GeoJSON('{"type":"Point","coordinates":[42.2808,83.743]}') |
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

Policies

Instead of using the default read policies, we can set them on a per command basis.

The following example creates a new read policy and sets the socket timeout for the command.

// Create new read policy
Policy policy = new Policy();
policy.socketTimeout = 300;

Read a record

Record exists

Checking record existence is faster than getting the record because it only needs to look at the primary index.

// Returns true if exists, false if not
bool exists = client.Exists(policy, key);
// Do something
Console.WriteLine("Exists: {0}", exists);
// Close the connection to the server
client.Close();

Record metadata only

Read record metadata (generation and expiration information) without reading the bins for a specified key.

// Get record metadata
Record record = client.GetHeader(policy, key);
// Do something
Console.WriteLine("Record: {0}", record);
// Close the connection to the server
client.Close();

Whole record

Read the record metadata and all bins for a specified key.

// Get whole record
Record record = client.Get(policy, key);
// Do something
Console.WriteLine("Record: {0}", record.ToString().Split("bins:")[1]);
// Close the connection to the server
client.Close();

Specific bins

Read the record metadata and the report and location bins from the record.

// Get bins 'report' and 'location'
Record record = client.Get(policy, key, "report", "location");
// Do something
Console.WriteLine("Record: {0}", record.ToString().Split("bins:")[1]);
// Close the connection to the server
client.Close();

Code block

Expand this section for a single code block to read a record
using Aerospike.Client;
// Define host configuration
Host config = new Host("127.0.0.1", 3000);
// Establishes a connection to the server
AerospikeClient client = new AerospikeClient(null, config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
Key key = new Key("sandbox", "ufodata", 5001);
// Create new read policy
Policy policy = new Policy();
policy.socketTimeout = 300;
// Get whole record
Record record = client.Get(policy, key);
// Do something
Console.WriteLine("Record: {0}", record.ToString().Split("bins:")[1]);
// Close the connection to the server
client.Close();
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?