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.
Setup
The following examples will use the setup and record structure below to illustrate single record creation in an Aerospike database.
import com.aerospike.client.AerospikeClient;import com.aerospike.client.policy.RecordExistsAction;import com.aerospike.client.policy.WritePolicy;import com.aerospike.client.Bin;import com.aerospike.client.Key;import com.aerospike.client.Value;
import java.util.ArrayList;import java.util.HashMap;import java.util.Map;
// Establishes a connection to the serverAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);The record structure:
Occurred: IntegerReported: IntegerPosted: IntegerReport: Map{ shape: List, summary: String, city: String, state: String, duration: String}Location: GeoJSONPolicies
Write policies define additional semantics for the write operation. Instead of using the default write policies, we can set them on a per command basis.
The following example creates a new write policy container object that sets the send key policy to true. This stores the user defined key with the record, and returns it with read commands.
Setting recordExistsAction to RecordExistsAction.CREATE_ONLY ensures the
command fails instead of overwriting an existing record at the key.
// Create a new write policyWritePolicy writePolicy = new WritePolicy();writePolicy.sendKey = true;writePolicy.recordExistsAction = RecordExistsAction.CREATE_ONLY;Key
The key is a tuple made up of: (namespace, set, user defined key).
The following example creates a key using the sandbox namespace, ufodata set, and user defined key 5001.
// Create the record keyKey key = new Key("sandbox", "ufodata", 5001);Create a record
A record is the basic unit of storage in the database. A record is composed of: (key, metadata, bins).
Setup
Expand the block below to see the creation of the variables used in the write example below.
View the data creation
// Create a list of shapes to add to the report mapArrayList<String> shape = new ArrayList<String>();shape.add("circle");shape.add("flash");shape.add("disc");
// Create the report mapMap reportMap = new HashMap<String, Object>();reportMap.put("city", "Ann Arbor");reportMap.put("state", "Michigan");reportMap.put("shape", shape);reportMap.put("duration", "5 minutes");reportMap.put("summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!");
// Format coordinates as a GeoJSON stringString geoLoc = "{\"type\":\"Point\", \"coordinates\":[42.2808,83.7430]}";Write
The following example shows the creation of the bins and the writing of the record to the database.
// Create the bins as Bin("binName", value)Bin occurred = new Bin("occurred", 20220531);Bin reported = new Bin("reported", 20220601);Bin posted = new Bin("posted", 20220601);// reportMap defined in the section aboveBin report = new Bin("report", reportMap);// geoLoc defined in the section aboveBin location = new Bin("location", Value.getAsGeoJSON(geoLoc));
// Write the record to Aerospikeclient.put(writePolicy, key, occurred, reported, posted, report, location);
// Close the connection to the serverclient.close();Code block
Expand this section for a single code block to create a record
import com.aerospike.client.AerospikeClient;import com.aerospike.client.policy.RecordExistsAction;import com.aerospike.client.policy.WritePolicy;import com.aerospike.client.Bin;import com.aerospike.client.Key;import com.aerospike.client.Value;
import java.util.ArrayList;import java.util.HashMap;import java.util.Map;
// Establishes a connection to the serverAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Create a new write policyWritePolicy writePolicy = new WritePolicy();writePolicy.sendKey = true;writePolicy.recordExistsAction = RecordExistsAction.CREATE_ONLY;
// Create the record keyKey key = new Key("sandbox", "ufodata", 5001);
// Create a list of shapes to add to the report mapArrayList<String> shape = new ArrayList<String>();shape.add("circle");shape.add("flash");shape.add("disc");
// Create the report mapMap reportMap = new HashMap<String, Object>();reportMap.put("city", "Ann Arbor");reportMap.put("state", "Michigan");reportMap.put("shape", shape);reportMap.put("duration", "5 minutes");reportMap.put("summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!");
// Format coordinates as a GeoJSON stringString geoLoc = "{\"type\":\"Point\", \"coordinates\":[42.2808,83.7430]}";
// Create the bins as Bin("binName", value)Bin occurred = new Bin("occurred", 20220531);Bin reported = new Bin("reported", 20220601);Bin posted = new Bin("posted", 20220601);// reportMap defined in the section aboveBin report = new Bin("report", reportMap);// geoLoc defined in the section aboveBin location = new Bin("location", Value.getAsGeoJSON(geoLoc));
// Write the record to Aerospikeclient.put(writePolicy, key, occurred, reported, posted, report, location);
// Close the connection to the serverclient.close();