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 use the following setup to illustrate applying a record UDF.
const Aerospike = await import("aerospike");const batchType = Aerospike.batchType;const exp = Aerospike.exp;
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
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 5000const key = new Aerospike.Key("sandbox", "ufodata", 5000);
// Creates a UDF objectconst udf = { module: "example", funcname: "setRecent", args: [20210101],};
// Execute the UDFawait client.apply(key, udf);
// View the updated recordconst record = await client.get(key);console.info("Record: %o", 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, provided by passing the bin names with the date values, and returns the days between. The following example returns the days between the sighting occurrence and posting to the site.
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000const key = new Aerospike.Key("sandbox", "ufodata", 5000);
// Creates a UDF objectconst udf = { module: "example", funcname: "getDaysBetween", args: ["occurred", "posted"],};
// Execute the UDFconst result = await client.apply(key, udf);
// output the result of the UDFconsole.info("%o days between occurrence and post", result);
Batch operation with a UDF
This example uses the multi-argument example from above, and apply it to a batch of records.
// Create batch of keysconst keys = Array.from( { length: 10 }, (_, i) => new Aerospike.Key("sandbox", "ufodata", i + 1),);
// Creates a UDF objectconst udf = { module: "example", funcname: "getDaysBetween", args: ["occurred", "posted"],};
// Execute the UDFconst batchResult = await client.batchApply(keys, udf);
// Access the recordsfor (const { record } of batchResult) { console.info("%o days between occurrence and post", record.bins.SUCCESS);}
// Close the connection to the serverawait 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 new write policyconst writePolicy = new Aerospike.WritePolicy({ filterExpression: exp.binExists("posted"),});
// Create queryconst query = client.query("sandbox", "ufodata");
// Execute the queryconst job = await query.background( "example", "setRecent", [20210101], writePolicy,);
// Return the query statusconst jobInfo = await job.info();
switch (jobInfo.status) { case 0: console.info("Query not found"); break; case 1: console.info("Query in progress," + jobInfo.progressPct + "% complete"); break; case 2: console.info("Query complete," + jobInfo.recordsRead + " records read"); break;}
// Close the connection to the serverawait client.close();
Code block
Expand this section for a single code block to apply a record UDF
const Aerospike = await import("aerospike");
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000const key = new Aerospike.Key("sandbox", "ufodata", 5000);
// Creates a UDF objectconst udf = { module: "example", funcname: "getDaysBetween", args: ["occurred", "posted"],};
// Execute the UDFawait client.apply(key, udf);
await client.close();