Serverless UDFs

Basic Usage

Below we show how to use Python UDFs in TileDB Cloud, with an example that uses numpy to compute the median of random numbers.

Python
Python
import tiledb, tiledb.cloud, numpy, random
def median():
vals = []
for i in range(1, random.randrange(2,50)):
vals.append(random.randrange(0, i))
return numpy.median(vals)
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
res = tiledb.cloud.udf.exec(median)
print(res)

Passing Arguments

The UDF can receive any number of arguments, with keyword arguments supported as well.

Python
Python
import tiledb, tiledb.cloud, numpy, random
def multi_args(arg1, arg2, arg3=None, arg4={}):
# These will print in the logs of the udf
print("type(arg1)={}, arg1={}\n".format(type(arg1), arg1))
print("type(arg2)={}, arg2={}\n".format(type(arg2), arg2))
print("type(arg3)={}, arg3={}\n".format(type(arg3), arg3))
print("type(arg4)={}, arg4={}\n".format(type(arg4), arg4))
return
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
res = tiledb.cloud.udf.exec(multi_args,
[1,2,3],
{"dictionary": "arg2_test"},
False,
arg4=True)
print(res) # None since the function returned nothing
# View the logs
print(tiledb.cloud.last_udf_task().logs)

Asynchronous Execution

An async version of UDFs is available, which returns a future.

Python
Python
import tiledb, tiledb.cloud, numpy, random
def median():
vals = []
for i in range(1, random.randrange(2,50)):
vals.append(random.randrange(0, i))
return numpy.median(vals)
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
# Res will be a future
res = tiledb.cloud.udf.exec_async(median)
# call res.get() to block on the results
print(res.get())

Selecting Who to Charge

If you you are a member of an organization, then by default the organization is charged for your UDF. If you would like to charge the UDF task to yourself, you just need to add one extra argument namespace.

Python
Python
import tiledb, tiledb.cloud, numpy, random
def median():
vals = []
for i in range(1, random.randrange(2,50)):
vals.append(random.randrange(0, i))
return numpy.median(vals)
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
res = tiledb.cloud.udf.exec(median, namespace="my_username")
print(res)

Registering a UDF

You can register a UDF (similar to arrays) as follows:

Python
Python
import tiledb, tiledb.cloud, numpy, random
def median():
vals = []
for i in range(1, random.randrange(2,50)):
vals.append(random.randrange(0, i))
return numpy.median(vals)
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
tiledb.cloud.udf.register_generic_udf(median, name="my_median", namespace="my_username")

In order to be able to register a UDF you need to set up the default storage path for you and/or your organization.

Retry Settings

See Retry Settings.