Create
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 creating 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.
This example creates the following JSON document:
"sighting": { "occurred": 20200912, "reported": 20200916, "posted": 20201105, "report": { "city": "Kirkland", "duration": "~30 minutes", "shape": [ "circle" ], "state": "WA", "summary": "4 rotating orange lights in the Kingsgate area above the Safeway. Around 9pm the power went out in the Kingsgate area. Four lights were spotted rotating above the local Safeway and surrounding streets. They were rotating fast but staying relatively in the same spots. Also described as orange lights. About thirty minutes later they disappeared. The second they disappeared the power was restored. Later a station of police from Woodinville and Kirkland came to guard the street where it happened. They wouldnt let anyone go past the street, putting out search lights and flare signals so people couldnt drive past Safeway. The police also would not let people walk past to go home." }, "location": "\"{\"type\":\"Point\",\"coordinates\":[-122.1966441,47.69328259]}\"" }The JSON document is added to a bin called sightings which is of data
type map. 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);
// Aerospike namespace, set, and key_id to be used for the Aerospike keystring ns = "test";string setName = "table1";int keyId = 5;
Key key = new(ns, setName, keyId);Create a JSON document
Prepare the JSON document to be sent to Aerospike.
Dictionary<string, object> employee = new(){ ["id"] = "09", ["name"] = "Nitin", ["department"] = "Finance"};
Bin bin = new("employee", employee);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("employee"));}catch (AerospikeException e){ Console.WriteLine("Create failed\nError: {0}", e.Message);}finally{ client.Close();}Code block
Expand this section for a single code block to create 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);
Dictionary<string, object> employee = new(){ ["id"] = "09", ["name"] = "Nitin", ["department"] = "Finance"};
Bin bin = new("employee", employee);
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("employee"));}catch (AerospikeException e){ Console.WriteLine("Create failed\nError: {0}", e.Message);}finally{ client.Close();}