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.

import tiledb, tiledb.cloud, numpy, random

def mymedian():
  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(mymedian)
print(res)

Passing Arguments

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

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.

import tiledb, tiledb.cloud, numpy, random

def mymedian():
  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(mymedian)

# 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.

import tiledb, tiledb.cloud, numpy, random

def mymedian():
  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(mymedian, namespace="my_username")
print(res)

Resource Classes

Each UDF runs by default in an isolated environment with 2 CPUs and 2 GB of memory. You can choose an alternative runtime environment from the following list:

NameDescription

standard

2 CPUs, 2 GB RAM

large

8 CPUs, 8 GB RAM

Charges are based on the total number of CPUs selected, not on the actual use.

To run a udf in a specific environment, set the resource_class parameter to the name of the environment.

def mymedian():
  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(mymedian, resource_class="large")
print(res)

Registering a UDF

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

import tiledb, tiledb.cloud, numpy, random

def mymedian():
  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.

Last updated