Skip to content

Manage UDFs

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Applies to

  • Aerospike Developer SDK preview (Java 21+ and Python 3.10+)
  • Aerospike Database 6.0 or later unless a section states otherwise

User-Defined Functions (UDFs) are Lua modules that run inside the Aerospike server process. Register modules on the cluster before invoking them from a session.

Except where noted, snippets on this page use the imports below. A snippet lists additional import lines only when it needs a type not shown here. When this page includes a Complete example section, that block is fully self-contained with every import required to run it.

import com.aerospike.client.sdk.Cluster;
import com.aerospike.client.sdk.ClusterDefinition;
import com.aerospike.client.sdk.Session;
import com.aerospike.client.sdk.policy.Behavior;
import com.aerospike.client.sdk.task.RegisterTask;

Register from a file

registerUdf (Java) and register_udf_from_file (Python) take two path strings:

  1. Local file path: absolute path to the .lua source on your application host (for example /tmp/example.lua in the Python example, or the temp file path in the Java example). The SDK reads this file from disk before uploading it.
  2. Server module name: filename under each node’s mod-lua user path (for example example.lua). This is what Aerospike stores on the cluster, not a path on your machine.

Pass the module name without the .lua extension to executeUdf().function() (Java) or execute_udf().function() (Python) later.

String exampleLua = """
function readBin(r, name)
return r[name]
end
function multiplyAndAdd(r, a, b)
return r['factor'] * a + b
end
""";
try (Cluster cluster = new ClusterDefinition("localhost", 3000).connect()) {
Session session = cluster.createSession(Behavior.DEFAULT);
// Write the module locally, then register it on the cluster
java.nio.file.Path temp = java.nio.file.Files.createTempFile("example", ".lua");
java.nio.file.Files.writeString(temp, exampleLua);
RegisterTask task = session.registerUdf(temp.toAbsolutePath().toString(), "example.lua");
task.waitTillComplete();
// Module is available as package name "example" (no .lua suffix)
}

📖 API reference: Session.registerUdf | RegisterTask.waitTillComplete

Registration is asynchronous on the server. The client returns a RegisterTask immediately. Call waitTillComplete() (Java) or await wait_till_complete() (Python) before invoking the UDF so every node has the module.

Register from an inline string

Use an in-memory Lua source when the module is built in your application or read from a config service. The writeUnique example below registers a separate module (unique.lua) so it does not overwrite example.lua from Register from a file.

String uniqueLua = """
-- Set a particular bin only if the record does not already exist.
function writeUnique(r, name, value)
if not aerospike:exists(r) then
aerospike:create(r)
r[name] = value
aerospike:update(r)
end
end
""";
try (Cluster cluster = new ClusterDefinition("localhost", 3000).connect()) {
Session session = cluster.createSession(Behavior.DEFAULT);
RegisterTask task = session.registerUdfString(uniqueLua, "unique.lua");
task.waitTillComplete();
// Module is available as package name "unique"
}

📖 API reference: Session.registerUdfString

Remove a registered module

session.removeUdf("example.lua");

📖 API reference: Session.removeUdf

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?