Creating user-defined function (UDF)
Write custom functions using Java and other programming languages for use in SELECT, INSERT, and UPDATE statements.
Write custom functions using Java and other programming languages for use in SELECT, INSERT, and UPDATE statements. Function are only available within the keyspace where it is defined.
You can define functions that are executed against data stored in a table as part of a query result. The function must be created prior to its use in a SELECT statement. The function is performed on each row of the table.
cassandra.yaml
- The cassandra.yaml file is located in the installation_location/conf directory.
Adding additional languages
DataStax Distribution of Apache Cassandra™ supports functions written in Java and JavaScript by default. Add
support for other scripting languages, Python
,
Ruby
, and Scala
by adding a JAR to the
classpath. Install the JAR file into:
installation_location/lib/jsr223/[language]/[jar-name].jar
where language is 'jruby', 'jython', or 'scala'.
Prerequisites
- Java: Set enable_user_defined_functions to
true
- Javascript and other custom languages: Set enable_scripted_user_defined_functions to
true
.
Procedure
-
Create a function, specifying the data type of the returned value, the
language, and the actual code of the function to be performed. The following
function,
fLog()
, computes the logarithmic value of each input. It is a built-injava
function and used to generate linear plots of non-linear data. For this example, it presents a simple math function to show the capabilities of user-defined functions.CREATE FUNCTION cycling.flog(target_name double) CALLED ON NULL INPUT RETURNS double LANGUAGE java AS $$return Double.valueOf(Math.log(input.doubleValue()));$$;
Note:- target_name
- Actions when the input from the target column is null:
CALLED ON NULL INPUT
ensures the function will always be executed.RETURNS NULL ON NULL INPUT
ensures the function will always returnNULL
if any of the input arguments isNULL
.
RETURNS
defines the CQL data type of the value returned by the function.
-
A function can be replaced with a different function if
OR REPLACE
is used as shown in the example above. Optionally, theIF NOT EXISTS
keywords can be used to create the function only if another function with the same signature does not exist in the keyspace.OR REPLACE
andIF NOT EXISTS
cannot be used in the same command.CREATE OR REPLACE FUNCTION cycling.flog(input double) CALLED ON NULL INPUT RETURNS double LANGUAGE java AS $$return Double.valueOf(Math.log(input.doubleValue()));$$;