AWS Lambda is a serverless computing service that lets you run code without provisioning or managing servers. Basically, you write code (in any of the supported languages) that processes an input message, does its logic, and returns an output message. This is intended for "small/short" processes that do not require much computing. The requests have a timeout and you are charged by the computing time consumed by all the calls to your lambda function.
This article explains how to create a Lambda Function in Java that connects to Denodo using the JDBC driver, executes a query, and returns the result as a String.
Requirements: install Apache Maven on your computer.
Create the Lambda Function
In this article, we will use the example “blank-java” of the sample application that AWS provides as a template for this new function.
Follow these steps:
- In the command line, execute this to install the JDBC driver of Denodo into your local Maven repository:
$ cd <DENODO_HOME>/tools/client-drivers/jdbc $ mvn install:install-file -Dfile=”./denodo-vdp-jdbcdriver.jar” -DgroupId=”com.denodo.vdpjdbcdriver” -DartifactId=”vdpjdbcdriver” -Dversion=9 -Dpackaging=”jar” |
- Clone or download the project aws-lambda-developer-guide from the Git repository of AWS.
This repository contains the “blank-java” application, among other things.
- Add the Denodo JDBC driver as a dependency of this project: in the file sample-apps/blank-java/pom.xml add this under <dependencies>:
<dependency> <groupId>com.denodo.vdpjdbcdriver</groupId> <artifactId>vdpjdbcdriver</artifactId> <version>9</version> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>apache-client</artifactId> <version>2.25.60</version> </dependency> |
- From the same file (pom.xml) delete the following line:
<excludeScope>test</excludeScope> |
- Check the last available version from the following repository Maven Repository: software.amazon.awssdk » lambda and update the <version> tag from your pom.xml file accordingly (for instance, the current version is 2.25.60):
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>lambda</artifactId> <version>2.25.60</version> </dependency> |
- In sample-apps/blank-java/src/main/java/example/Handler.java, replace it with this:
package example; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.services.lambda.LambdaClient; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.time.Duration; import java.util.Map; import java.util.Properties; // Handler value: example.Handler public class Handler implements RequestHandler<Map<String,String>, String> { @SuppressWarnings("unused") private static final LambdaClient lambdaClient = LambdaClient.builder().httpClientBuilder( ApacheHttpClient.builder() .maxConnections(100) .connectionTimeout(Duration.ofSeconds(5)) ).build(); @Override public String handleRequest(Map<String,String> event, Context context) { try { Properties connProperties = new Properties(); connProperties.put("user", "service_account_read_only"); connProperties.put("password", "my_password"); String connectionURL = "jdbc:vdb://acme.denodo.com:9999/admin"; Class.forName("com.denodo.vdp.jdbc.Driver"); try (Connection conn = DriverManager.getConnection(connectionURL, connProperties); Statement pstm = conn.createStatement()) { String sql = "SELECT 1 as int_field, 'Hello' as text_field FROM dual() " + "UNION SELECT NULL as int_field, 'World!!' as text_field FROM dual()"; ResultSet rs = pstm.executeQuery(sql); StringBuilder sb = new StringBuilder("OUTPUT: "); int row = 1; while (rs.next()) { sb.append("ROW(").append(row++).append(") = ["); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { sb.append(" ").append(rs.getObject(i)); } sb.append("] *** "); } return sb.toString(); } } catch (Exception e) { return "ERROR: " + e.getMessage(); } }
} |
Important: replace “acme.denodo.com:9999” with the URL of your Denodo server.
Note: in “String sql”, you can replace this query with the query you want.
- Delete the folder test of the project: sample-apps/blank-java/src/test
This folder contains automated tests for your function. To automate this testing, you will need to modify the Java classes of this folder. For now, you can delete the folder. - Create the .jar file with this new function: execute this in the command line:
$ cd aws-lambda-developer-guide/sample-apps/blank-java/ $ mvn clean package |
This creates the jar file sample-apps/blank-java/target/blank-java-1.0-SNAPSHOT.jar.
It contains all the dependencies of the project including the Denodo JDBC driver.
Note that the target directory contains two .jar files; you have to use the large one (it contains all the dependencies) because the AWS Lambda Java runtime environment only provides the Java Runtime Environment.
Define the Function in AWS
First you need to define the function on AWS:
- Open the Functions page on the Lambda console and click Create function.
- Choose Author from scratch.
- Under Basic Information, do this:
- In Function name: enter the name of the function. For example, “denodo_test_query”.
- For Runtime, select Java 17..
- Select your VDP server architecture (x86_64 / arm64)
- Click on Create function.
Upload the Lambda Function
Once the jar is generated, upload it as the source code of our lambda function:
- Upload the jar file with the compiled Java classes: click Upload > .zip or jar file and select the .jar file of your computer that you just built (sample-apps/blank-java/target/blank-java-1.0-SNAPSHOT.jar). Remember to select the largest file (which is the one that contains all the dependencies).
- Update the Handler class of the function: in Runtime settings, click Edit. Then, in the Handler box, enter example.Handler::handleRequest.
Test the Function
To test the function, follow these steps:
- Go to the Test tab of the function.
- Select New event and click Test (leave the box Name empty).
Leave the box below (the one with JSON) with the default value. This box is the data that the function will receive in this test. This sample function does not have input parameters so it will ignore this JSON.
If the function works as expected, you will see this:
If there is any error, the function will report the thrown exception.
This document was tested with the Denodo JDBC driver denodo-vdp-jdbcdriver-9.0.
References
The information provided in the Denodo Knowledge Base is intended to assist our users in advanced uses of Denodo. Please note that the results from the application of processes and configurations detailed in these documents may vary depending on your specific environment. Use them at your own discretion.
For an official guide of supported features, please refer to the User Manuals. For questions on critical systems or complex environments we recommend you to contact your Denodo Customer Success Manager.