
Preface
This post continues series of articles that present theAerosplike SQL driver.
Introduction
Previouspost explained the built-in functions provided by the Aerospike JDBC Driver. Here I am going to explain how to extend the functionality of the driver by implementing User Defined Functions (UDF).
Implementation of UDF
Right now aggregate functions cannot be customized, however you can implement scalar and collection functions using Java programming language.
Function is a named block of reusable code that used to perform single action. Typically functions return result. Functions may accept arguments.
Java 8 and higher provides interfacesSupplier
,Function
,BiFunction
. Implementations of these interfaces can be defined as UDF for the Aerospike JDBC driver. For example here is definition of functionnow()
that returns epoch in milliseconds:
publicclassNowimplementsnewSupplier<Long>(){@OverridepublicLongget(){returnSystem.currentTimeMillis();}};
This is example of function that receives one argument:
publicclassLowerimplementsnewFunction<String,String>(){@OverridepublicStringapply(Strings){returns==null?null:s.toLowerCase();}};
Implementation of function that accepts 2 arguments is similar. Create class that implementsBiFunction
. Unfortunately JDK does not define interface for function that accepts 3, 4 or more arguments as well as function that accepts any number of arguments. Such interface is defined by the driver:
packagecom.nosqldriver.util;@FunctionalInterfacepublicinterfaceVarargsFunction<T,R>{Rapply(T...t);}
If you want to implement UDF that accepts more than 2 arguments you have to implement this interface. In this case you have to add the driver to the compilation class path. Implementation of other types of functions does not require this dependency. Implementation ofVarargsFunction
has yet another complexity: the programmer is responsible on casting and verification of accepted arguments.
Deploying of UDF
Once UDF is implemented and verified it should be packed into jar file that should be added to the classpath of the driver. Various tools allow this. For exampleDbeaver andSquirreL allow defining driver packaged in several jar files. Once this is done the the function should be registered using connection parameter:
custom.function.NAME=FULLY_QUALIFIED_CLASS_NAME
e.g.
custom.function.now=com.mycompany.Now
The connection parameter can be supplied as a parameter of JDBC URL or as a connection property.
jdbc:aeropspike:myhost?custom.function.now=com.mycompany.Now
Once function is registered it can be used in SQL query:
selectfirst_name,now()frompeople
Conclusions
Aerospike JDBC Driver provides a comprehensive set of built-in functions. Moreover it is extendable using User-Defined Functions that can be easily implemented using Java programming language.
Project home
The project is available inGitHub.
What's next
Next article of this series will explain how to work with complex types.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse