Apply a UDF
Jump to the Code block for a combined complete example.
UDFs that execute on a single record are Record UDFs. The record may or may not exist in the database, and the UDF may create, read, and/or update a record.
Setup
The following examples will use the setup below to illustrate applying a record UDF.
import ( "fmt" "github.com/aerospike/aerospike-client-go/v6")
// Establishes a connection to the serverclient, err := aerospike.NewClient("127.0.0.1", 3000)if err != nil { log.Fatal(err)}defer client.Close()
The record structure:
Occurred: IntegerReported: IntegerPosted: IntegerReport: Map{ shape: List, summary: String, city: String, state: String, duration: String}Location: GeoJSON
Single record commands
Single argument
This example uses the setRecent
function in the module example.lua
.
See Manage UDFs for example.lua
code and information on registering the UDF.
This compares the posted
bin to a supplied value and writes true
/false
to a recent
bin based on the result.
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000key, err := aerospike.NewKey("sandbox", "ufodata", 5000)if err != nil { log.Fatal(err)}
// Execute the UDFresult, err := client.Execute( nil, key, "example", "setRecent", aerospike.NewValue(20210101))if err != nil { log.Fatal(err)}
// View the updated recordrecord, err := client.Get(nil, key)if err != nil { log.Fatal(err)}
fmt.Printf("Record: %v", record.Bins)
Multiple argument
This example uses the getDaysBetween
function in the module example.lua
.
See Manage UDFs for example.lua
code and information on registering the UDF.
This compares two dates, provived by passing the bin names with the date values, and returns the days between. The following example will return the days between the sighting occurence and posting to the site.
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000key, err := aerospike.NewKey("sandbox", "ufodata", 5000)if err != nil { log.Fatal(err)}
// Execute the UDFresult, err := client.Execute( nil, key, "example", "getDaysBetween", aerospike.NewValue("occurred"), aerospike.NewValue("posted"))if err != nil { log.Fatal(err)}
// Do somethingfmt.Printf("%v days between occurrence and post", result)
Batch operation with a UDF
This example will use the multi-argument example from above, and apply it to a batch of records.
// Create batch of keyskeys := make([]*aerospike.Key, 10)for i := 0; i < 10; i++ { keys[i], err = aerospike.NewKey("sandbox", "ufodata", (i + 100)) if err != nil { log.Fatal(err) }}
// Execute the UDFresults, err := client.BatchExecute( nil, nil, keys, "example", "getDaysBetween", aerospike.NewValue("occurred"), aerospike.NewValue("posted"))if err != nil { log.Fatal(err)}
// Access the resultsfor _, result := range results { if result != nil { // Do something fmt.Printf("%v days between occurrence and post\\n", result.Record.Bins["SUCCESS"]) }}
Background query with a UDF
This example will use the single argument example from above, and apply it to all records that match a Filter Expression through a background query.
// Create new write policyqueryPolicy := aerospike.NewQueryPolicy()queryPolicy.FilterExpression = aerospike.ExpBinExists("posted")
// Create statementstmt := aerospike.NewStatement("sandbox", "ufodata")
task, err := client.ExecuteUDF( queryPolicy, stmt, "example", "setRecent", aerospike.NewValue(20210101))if err != nil { log.Fatal(err)}
// Return the query statusstatus, err := task.IsDone();if err != nil { // Handle error} else { if status { fmt.Printf("Query complete") } else { fmt.Printf("Query in progress") }}
Code block
Expand this section for a single code block to apply a record UDF
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()
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000 key, err := aerospike.NewKey("sandbox", "ufodata", 5000) if err != nil { log.Fatal(err) }
// Execute the UDF result, err := client.Execute( nil, key, "example", "getDaysBetween", aerospike.NewValue("occurred"), aerospike.NewValue("posted")) if err != nil { log.Fatal(err) }
// Do something fmt.Printf("%v days between occurrence and post", result)}