Apache Drill – JDBC Interface & Custom Function

14. Apache Drill – JDBC Interface

Apache Drill provides JDBC interface to connect and execute queries.

We can use JDBC interface in JDBC based SQL Client like “SquirreL SQL Client” and work on all the features of drill.

We can use the same JDBC interface to connect drill from our Java based application.

Let us see how to connect drill and execute commands in our sample Java application using JDBC interface in this section.

Java Application

……

15. Apache Drill – Custom Function

Apache Drill has an option to create custom functions.

These custom functions are reusable SQL functions that you develop in Java to encapsulate the code that processes column values during a query.


Custom functions can perform calculations and transformations that the built-in SQL operators and functions do not provide.

Custom functions are called from within a SQL statement, like a regular function, and return a single value.

Apache Drill has custom aggregate function as well and it is still evolving. Let us see how to create a simple custom function in this section.


IsPass Custom Function
Apache Drill provides a simple interface, “DrillSimpleFunc”, which we have to implement to create a new custom function.

The “DrillSimpleFunc” interface has two methods, “setup” and “eval”.

The “setup” method is to initialize necessary variables.

“eval” method is actual method used to incorporate the custom function logic. The “eval” method has certain attributes to set function name, input and output variables.


Apache Drill provide a list of datatype to hold input and output variable like BitHolder, VarCharHolder, BigIntHolder, IntHolder, etc.

We can use these datatypes to pass on information between drill and custom function.

Now, let us create a new application using Maven with “com.tutorialspoint.drill.function” as the package name and “is-pass” as the library name.

mvn archetype:generate -DgroupId=com.tutorialspoint.drill.function -DartifactId=is-pass -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Here,
 -DgroupId - package name
 -DartifactId - argument

Then remove the App.java file and create new java file and name it as “IsPassFunc.java”. This java file will hold out custom function logic. The custom function logic is to check whether the particular student is secured pass in a particular subject by checking his mark with cutoff mark. The student mark will be first input and it will change according to the record.
The second input is the cutoff mark, which will be a constant and does not change for different records. The custom function will implement “DrillSimpleFunc” interface and just check whether the given input is higher than the cutoff. If the input is higher, then if returns true, otherwise false.

……

原文地址:https://www.cnblogs.com/panpanwelcome/p/13446273.html