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

Create

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.

// Imports
import com.aerospike.client.*;
import com.aerospike.client.Record;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.WritePolicy;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
AerospikeClient client = new AerospikeClient("localhost", 3000);
// Aerospike namespace, set, and key_id to be used for the Aerospike key
String namespace = "sandbox";
String set = "ufo";
int key_id = 5001;
String binName = "sighting";
// define Aerospike key
Key key = new Key(namespace, set, key_id);

Create a JSON document

Prepare the JSON document to be sent to Aerospike.

// Example JSON string
String sighting = "{\"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 wouldn't let anyone go past the street, putting out search lights and flare signals so people couldn't drive past Safeway. The police also would not let people walk past to go home.\"}, \"location\": \"\\\"{\\\"type\\\":\\\"Point\\\",\\\"coordinates\\\":[-122.1966441,47.69328259]}\\\"\"}}";
// Convert string to Java Map
Map<String, Object> sightingMap = new HashMap<>();
sightingMap = new Gson().fromJson(sighting, sightingMap.getClass());

Write

Write the document to Aerospike.

// Set an Aerospike bin with the bin name "sighting" and put the JSON document in as a map
Bin bin = new Bin(binName, Value.get(sightingMap));
// Create the write policy
WritePolicy writePolicy = new WritePolicy();
writePolicy.recordExistsAction = RecordExistsAction.UPDATE;
// Write the record to Aerospike
try {
// Writing record
client.put(writePolicy, key, bin);
// Reading record
Record record = client.get(null, key);
Object value = record.getValue(binName);
System.out.printf("Create succeeded \nKey: %d \nRecord: %s", key.userKey.toLong(), new Gson().toJson(value));
} // Create record failed
catch (AerospikeException ae) {
System.out.println("Create failed\\nError: " + ae.getMessage());
} // Cleanup
finally {
// Close the client
client.close();
}

Code block

Expand this section for a single code block to create a document record.
import com.aerospike.client.*;
import com.aerospike.client.Record;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.WritePolicy;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
AerospikeClient client = new AerospikeClient("localhost", 3000);
// Aerospike namespace, set, and key_id to be used for the Aerospike key
String namespace = "sandbox";
String set = "ufo";
int key_id = 5001;
String binName = "sighting";
// Define Aerospike key
Key key = new Key(namespace, set, key_id);
// Example JSON string
String sighting = "{\"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 wouldn't let anyone go past the street, putting out search lights and flare signals so people couldn't drive past Safeway. The police also would not let people walk past to go home.\"}, \"location\": \"\\\"{\\\"type\\\":\\\"Point\\\",\\\"coordinates\\\":[-122.1966441,47.69328259]}\\\"\"}}";
// Convert string to Java Map
Map<String, Object> sightingMap = new HashMap<>();
sightingMap = new Gson().fromJson(sighting, sightingMap.getClass());
// Set an Aerospike bin with the bin name "sighting" and put the JSON document in as a map
Bin bin = new Bin(binName, Value.get(sightingMap));
// Create the write policy
WritePolicy writePolicy = new WritePolicy();
writePolicy.recordExistsAction = RecordExistsAction.UPDATE;
// Write the record to Aerospike
try {
// Writing record
client.put(writePolicy, key, bin);
// Reading record
Record record = client.get(null, key);
Object value = record.getValue(binName);
System.out.printf("Create succeeded \nKey: %d \nRecord: %s", key.userKey.toLong(), new Gson().toJson(value));
} // Create record failed
catch (AerospikeException ae) {
System.out.println("Create failed\\nError: " + ae.getMessage());
} // Cleanup
finally {
// Close the client
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?