You can translate the document:

AWS Lambda is a serverless compute 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:

  1. In the command line, execute this to install the JDBC driver of Denodo in to your local Maven repository:

$ cd <DENODO_HOME>/tools/tools/client-drivers/jdbc

$ mvn install:install-file -Dfile=./denodo-vdp-jdbcdriver.jar -DgroupId=com.denodo.vdpjdbcdriver -DartifactId=vdpjdbcdriver -Dversion=8 -Dpackaging=jar

  1. 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.

  1. Add the Denodo JDBC driver as a dependency of this project: in the file sample-apps/blank-java/pom.xml add this:

<dependency>

    <groupId>com.denodo.vdpjdbcdriver</groupId>

    <artifactId>vdpjdbcdriver</artifactId>

    <version>8</version>

</dependency>

  1. In sample-apps/blank-java/src/main/java/example/Handler.java, replace the method “handleRequest” with this:

@Override

public String handleRequest(SQSEvent 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";

                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.

  1. 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.
  2. 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:

  1. Open the Functions page on the Lambda console and click Create function.
  2. Choose Author from scratch. 
  3. Under Basic Information, do this:
  • In Function name: enter the name of the function. For example, “denodo_test_query”.
  • For Runtime, select Java 8.
  1. Click on Create function.

 

Upload the Lambda Function

Once the jar is generated, upload it as the source code of our lambda function:

  1. 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 (tha is the one that contains all the dependencies).
  2. 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:

  1. Go to the Test tab of the function.

  1. 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-8.0-update-20210209.

References

Create a Lambda function with the console

Java sample applications for AWS Lambda

Questions

Ask a question

You must sign in to ask a question. If you do not have an account, you can register here