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;from aerospike_sdk import ClientRegister from a file
registerUdf (Java) and register_udf_from_file (Python) take two path strings:
- Local file path: absolute path to the
.luasource on your application host (for example/tmp/example.luain the Python example, or the temp file path in the Java example). The SDK reads this file from disk before uploading it. - Server module name: filename under each node’s
mod-luauser path (for exampleexample.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
EXAMPLE_LUA = """\function readBin(r, name) return r[name]end
function multiplyAndAdd(r, a, b) return r['factor'] * a + bend"""
async with Client("localhost:3000") as client: # Write the module locally, then register it on the cluster with open("/tmp/example.lua", "w", encoding="utf-8") as f: f.write(EXAMPLE_LUA)
task = await client.register_udf_from_file("/tmp/example.lua", "example.lua") await task.wait_till_complete() # Module is available as package name "example" (no .lua suffix)📖 API reference:
Client.register_udf_from_file|RegisterTask.wait_till_complete
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
UNIQUE_LUA = b"""\-- 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) endend"""
async with Client("localhost:3000") as client: task = await client.register_udf(UNIQUE_LUA, "unique.lua") await task.wait_till_complete() # Module is available as package name "unique"📖 API reference:
Client.register_udf
Remove a registered module
session.removeUdf("example.lua");📖 API reference:
Session.removeUdf
task = await client.remove_udf("example.lua")await task.wait_till_complete()📖 API reference:
Client.remove_udf