Skip to content

Read

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Jump to the Code block for a combined complete example.

Create a document record

The following example demonstrates reading a document record with a JSON helper library. In Aerospike, JSON documents are handled as a Collection Data Type (CDT). A JSON object is equivalent to a map, and a JSON array is equivalent to a list.

In this example, we create the following JSON document:

employees = [{"id": "09", "name": "Nitin", "department": "Finance"},
{"id": "10", "name": "Jonathan", "department": "Human Resources"},
{"id": "11", "name": "Caitlin", "department": "Engineering"}]

The JSON document is added to a bin called employees which is of data type list. There are some advantages to holding an entire document in a single bin, rather than spreading out the document’s fields over multiple bins. There is less metadata overhead when namespaces contain fewer, larger bins.

Setup

Import the necessary helpers, create a client connection, and create a key.

using Aerospike.Client;
using System;
using System.Collections.Generic;
AerospikeClient client = new("localhost", 3000);
string ns = "test";
string setName = "table1";
int keyId = 5;
Key key = new(ns, setName, keyId);

Prepare the JSON document to be sent to Aerospike.

List<Dictionary<string, object>> employees = new()
{
new() { ["id"] = "09", ["name"] = "Nitin", ["department"] = "Finance" },
new() { ["id"] = "10", ["name"] = "Jonathan", ["department"] = "Human Resources" },
new() { ["id"] = "11", ["name"] = "Caitlin", ["department"] = "Engineering" }
};
Bin bin = new("employees", employees);

Write

Write the document to Aerospike.

WritePolicy writePolicy = new()
{
sendKey = true
};
try
{
client.Put(writePolicy, key, bin);
Record record = client.Get(null, key);
Console.WriteLine("Create succeeded\nKey: {0}\nRecord: {1}",
key.userKey, record.GetValue("employees"));
}
catch (AerospikeException e)
{
Console.WriteLine("Create failed\nError: {0}", e.Message);
}

Read

Read the name field from the third employee in the list (index 2).

Record record = client.Operate(null, key,
CDTOperation.SelectByPath(
"employees",
SelectFlag.VALUE,
CTX.ListIndex(2),
CTX.MapKey(Value.Get("name")))
);
Console.WriteLine("The 'name' field at list index 2: {0}",
record.GetValue("employees"));
client.Close();

Code block

Expand this section for a single code block to read a document record.
using Aerospike.Client;
using System;
using System.Collections.Generic;
AerospikeClient client = new("localhost", 3000);
string ns = "test";
string setName = "table1";
int keyId = 5;
Key key = new(ns, setName, keyId);
List<Dictionary<string, object>> employees = new()
{
new() { ["id"] = "09", ["name"] = "Nitin", ["department"] = "Finance" },
new() { ["id"] = "10", ["name"] = "Jonathan", ["department"] = "Human Resources" },
new() { ["id"] = "11", ["name"] = "Caitlin", ["department"] = "Engineering" }
};
Bin bin = new("employees", employees);
WritePolicy writePolicy = new()
{
sendKey = true
};
try
{
client.Put(writePolicy, key, bin);
Record created = client.Get(null, key);
Console.WriteLine("Create succeeded\nKey: {0}\nRecord: {1}",
key.userKey, created.GetValue("employees"));
}
catch (AerospikeException e)
{
Console.WriteLine("Create failed\nError: {0}", e.Message);
}
Record record = client.Operate(null, key,
CDTOperation.SelectByPath(
"employees",
SelectFlag.VALUE,
CTX.ListIndex(2),
CTX.MapKey(Value.Get("name")))
);
Console.WriteLine("The 'name' field at list index 2: {0}",
record.GetValue("employees"));
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?