Skip to main content

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.

using Aerospike.Client;

// Define host configuration
Host config = new Host("127.0.0.1", 3000);
// Establishes a connection to the server
AerospikeClient client = new AerospikeClient(null, config);

The record structure:

Occurred: Integer  
Reported: Integer
Posted: Integer
Report: 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 5000
Key key = new Key("sandbox", "ufodata", 5000);

// Execute the UDF
Object result = client.Execute(
null, key, "example", "setRecent", Value.Get(20210101)
);

// View the updated record
Record record = client.Get(null, key);
Console.WriteLine("Record: {0}", record.ToString().Split("bins:")[1]);

// Close the connection to the server
client.Close();

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 5000
Key key = new Key("sandbox", "ufodata", 5000);

// Execute the UDF
Object result = client.Execute(
null, key, "example", "getDaysBetween", Value.Get("occurred"), Value.Get("posted")
);

// Do something
Console.WriteLine("{0} days between occurrence and post", result);

// Close the connection to the server
client.Close();

Batch operation with a UDF

note

Server version 6.0 and above is required for batch UDF operations

This example will use the multi-argument example from above, and apply it to a batch of records.

// Create batch of keys
Key[] keys = new Key[10];
for (int i = 0; i < 10; i++)
{
keys[i] = new Key("sandbox", "ufodata", (i + 100));
}

// Execute the UDF
BatchResults batchResult = client.Execute(
null, null, keys, "example", "getDaysBetween", Value.Get("occurred"), Value.Get("posted")
);

// Access the results
foreach (BatchRecord batchRecord in batchResult.records)
{
Record record = batchRecord.record;
if(record != null)
{
// Do something
Console.WriteLine("{0} days between occurrence and post\\n", record.bins["SUCCESS"]);
}
}

// Close the connection to the server
client.Close();

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 the policy
WritePolicy writePolicy = new WritePolicy();
// Build the expression
writePolicy.filterExp = Exp.Build(
Exp.BinExists("posted")
);

// Create statement
Statement stmt = new Statement();

// Set namespace and set name
stmt.SetNamespace("sandbox");
stmt.SetSetName("ufodata");

ExecuteTask task = client.Execute(
writePolicy, stmt, "example", "setRecent", Value.Get(20210101)
);

// Return the query status
int status = task.QueryStatus();

string queryStatus;
switch (status)
{
case 0: queryStatus = "not found";
break;
case 1: queryStatus = "in progress";
break;
case 2: queryStatus = "complete";
break;
}
Console.WriteLine("Query {0}.", queryStatus);

// Close the connection to the server
client.Close();

Code block

Expand this section for a single code block to apply a record UDF
using Aerospike.Client;

// Define host configuration
Host config = new Host("127.0.0.1", 3000);
// Establishes a connection to the server
AerospikeClient client = new AerospikeClient(null, config);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000
Key key = new Key("sandbox", "ufodata", 5000);

// Execute the UDF
Object result = client.Execute(
null, key, "example", "getDaysBetween", Value.Get("occurred"), Value.Get("posted")
);

// Do something
Console.WriteLine("{0} days between occurrence and post", result);

// Close the connection to the server
client.Close();