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.

Setup

The following examples will use the setup and record structure below to illustrate single record creation in an Aerospike database.

import (
"fmt"
"github.com/aerospike/aerospike-client-go/v6"
)
// Establishes a connection to the server
client, err := aerospike.NewClient("127.0.0.1", 3000)
if err != nil {
log.Fatal(err)
}
defer client.Close()

The record structure:

Occurred: Integer
Reported: Integer
Posted: Integer
Report: Map
{
shape: List,
summary: String,
city: String,
state: String,
duration: String
}
Location: GeoJSON

Policies

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.

// Create new write policy
policy := aerospike.NewWritePolicy(0,0)
policy.SendKey = true

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 key
key, err := aerospike.NewKey("sandbox", "ufodata", 5001)
if err != nil {
log.Fatal(err)
}

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 map
shape := []string{"circle", "flash", "disc"}
// Create the report map
reportMap := map[string]interface{}{
"city": "Ann Arbor",
"state": "Michigan",
"shape": shape,
"duration": "5 minutes",
"summary": "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"}
// Format coordinates as a GeoJSON string
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)
occurred := aerospike.NewBin("occurred", 20220531)
reported := aerospike.NewBin("reported", 20220601)
posted := aerospike.NewBin("posted", 20220601)
// reportMap defined in the section above
report := aerospike.NewBin("report", reportMap)
// geoLoc defined in the section above
location := aerospike.NewBin("location", aerospike.NewGeoJSONValue(geoLoc))
// Write the record to Aerospike
err = client.PutBins(policy, key, occurred, reported, posted, report, location)
if err != nil {
log.Fatal(err)
}

Code block

Expand this section for a single code block to create a record
import (
"fmt"
"github.com/aerospike/aerospike-client-go/v6"
)
func main() {
// Establishes a connection to the server
client, err := aerospike.NewClient("127.0.0.1", 3000)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Create new write policy
policy := aerospike.NewWritePolicy(0,0)
policy.SendKey = true
// Create the record key
key, err := aerospike.NewKey("sandbox", "ufodata", 5001)
if err != nil {
log.Fatal(err)
}
// Create a list of shapes to add to the report map
shape := []string{"circle", "flash", "disc"}
// Create the report map
reportMap := map[string]interface{}{
"city": "Ann Arbor",
"state": "Michigan",
"shape": shape,
"duration": "5 minutes",
"summary": "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"}
// Format coordinates as a GeoJSON string
geoLoc := "{\"type\":\"Point\", \"coordinates\":[42.2808,83.7430]}"
// Create the bins as Bin("binName", value)
occurred := aerospike.NewBin("occurred", 20220531)
reported := aerospike.NewBin("reported", 20220601)
posted := aerospike.NewBin("posted", 20220601)
// reportMap defined in the section above
report := aerospike.NewBin("report", reportMap)
// geoLoc defined in the section above
location := aerospike.NewBin("location", aerospike.NewGeoJSONValue(geoLoc))
// Write the record to Aerospike
err = client.PutBins(policy, key, occurred, reported, posted, report, location)
if err != nil {
log.Fatal(err)
}
}
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?