Was this page helpful?
ScyllaDB Java Driver is available under the Apache v2 License. ScyllaDB Java Driver is a fork of DataStax Java Driver. See Copyright here.
User-defined functions (UDF) enable users to create user code written in JSR-232 compliant scripting languages that can be evaluated in CQL queries. SchemaBuilder offers API methods for creating and dropping UDFs.
To start a CREATE FUNCTION query, use createFunction in SchemaBuilder:
import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;
CreateFunctionStart create = createFunction("log");
Like all other CREATE queries, one may supply ifNotExists() to require that the UDF should only
be created if it doesn’t already exist, i.e.:
CreateFunctionStart create = createFunction("cycling", "log").ifNotExists();
You may also specify that you would like to replace an existing function by the same signature if it
exists. In this case, use orReplace:
CreateFunctionStart create = createFunction("cycling", "log").orReplace();
One may also specify the parameters of a function using withParameter:
createFunction("cycling", "left")
.withParameter("colName", DataTypes.TEXT)
.withParameter("num", DataTypes.DOUBLE)
There are a number of steps that must be executed to complete a function:
Specify whether the function is called on null input (calledOnNull) or if it should simply
return null (returnsNullOnNull).
Specify the return type of the function using returnsType
Specify language of the function body using withJavaLanguage, withJavaScriptLanguage, or
withLanguage
Specify the function body with as or asQuoted
For example, the following defines a complete CREATE FUNCTION statement:
createFunction("cycling", "log")
.withParameter("input", DataTypes.DOUBLE)
.calledOnNull()
.returnsType(DataTypes.DOUBLE)
.withJavaLanguage()
.asQuoted("return Double.valueOf(Math.log(input.doubleValue()));");
// CREATE FUNCTION cycling.log (input double) CALLED ON NULL INPUT RETURNS double LANGUAGE java
// AS 'return Double.valueOf(Math.log(input.doubleValue()));'
The as method expects an already quoted function body. Use asQuoted to quote it automatically.
Bodies containing single quotes use the $$ delimiter, as long as the body does not also contain
$$. Otherwise, the builder surrounds the body with single quotes and escapes each embedded single
quote by doubling it. For example:
createFunction("sayhi")
.withParameter("input", DataTypes.TEXT)
.returnsNullOnNull()
.returnsType(DataTypes.TEXT)
.withJavaScriptLanguage()
.asQuoted("'hi ' + input;");
// CREATE FUNCTION sayhi (input text) RETURNS NULL ON NULL INPUT RETURNS text
// LANGUAGE javascript AS $$ 'hi ' + input; $$
For a body containing both delimiters, such as return "O'Reilly $$";, asQuoted produces
'return "O''Reilly $$";'.
To create a DROP FUNCTION query, use dropFunction:
dropFunction("cycling", "log");
// DROP FUNCTION cycling.log
You may also specify ifExists:
dropFunction("log").ifExists();
// DROP FUNCTION IF EXISTS log
Was this page helpful?
ScyllaDB Java Driver is available under the Apache v2 License. ScyllaDB Java Driver is a fork of DataStax Java Driver. See Copyright here.