User-defined functions

As of version 3.0, Apache Cassandra allows users to create user-defined functions (UDFs). As CQL does not supply much in the way of extra tools and string utilities found in SQL, some of that function can be recreated with a UDF. Let's say that we want to query the current year from a date column. The date column will return the complete year, month, and day:

SELECT todate(now()) FROM system.local; system.todate(system.now())-----------------------------                  2018-08-03(1 rows)

To just get the year back, we could handle that in the application code, or, after enabling user-defined functions in the cassandra.yaml file, we could write a small UDF using the Java language:

CREATE OR REPLACE FUNCTION year (input DATE) RETURNS ...

Get Mastering Apache Cassandra 3.x - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.