Create a user-defined function (UDF)

Write custom functions using Java or JavaScript to use in SELECT, INSERT, and UPDATE statements or as a building block for a user-defined aggregate (UDA). Functions are only available within the keyspace where it is defined. To preserve fast performance, use user-defined functions for short computations, and create Java UDFs instead of Javascript UDFs.

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.

Prerequisites

By default, the ability to add user-defined functions is disabled. To enable, change the following settings in the cassandra.yaml file:

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-in java 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 OR REPLACE FUNCTION cycling.fLog (
  input double
) 
  CALLED ON NULL INPUT 
  RETURNS double 
  LANGUAGE java AS 
    $$
      return Double.valueOf(Math.log(input.doubleValue()));
    $$ 
;
  • Actions when the input from the target column is null:

    • CALLED ON NULL INPUT ensures the function always executes when called.

    • RETURNS NULL ON NULL INPUT ensures the function always returns NULL if any of the input arguments are NULL.

  • 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, the IF 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 and IF NOT EXISTS cannot be used in the same command.


Related information:

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com