Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Extending the Aerospike JDBC driver with UDFs
Alexander Radzin
Alexander Radzin

Posted on

     

Extending the Aerospike JDBC driver with UDFs

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer, java and open source enthusiast. Likes nice design and solving unsolvable challenges
  • Location
    Israel
  • Education
    https://english.spbstu.ru/
  • Work
    Software engineer at Upsolver
  • Joined

More fromAlexander Radzin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp