AWS Big Data Blog
Querying a Vertica data source in Amazon Athena using the Athena Federated Query SDK
The ability to query data and perform ad hoc analysis across multiple platforms and data stores with a single tool brings immense value to the big data analytical arena. As organizations build out data lakes with increasing volumes of data, there is a growing need to combine that data with large amounts of data in other data stores. As the variety of data increases, it becomes paramount to have a query tool to bridge two or more data stores with a single query.
Even though data lakes became popular for analytic workloads recently, it’s not uncommon to have data warehouses in addition to data lakes for various reporting and business intelligence (BI) use cases. It becomes imperative to be able to seamlessly query the data stored in the data warehouse and the data lake. To address this issue, Amazon Athena has released a feature called Athena Federated Query. Athena is an interactive query service provided by AWS that makes it easy to analyze data in Amazon Simple Storage Service (Amazon S3) using standard SQL. Vertica is a columnar MPP database platform that can be deployed in the cloud or on premises, and supports exabyte scale data warehouses. With Athena Federated Query and the Vertica connector, you can now run analytical queries over a data warehouse on Vertica and a data lake in Amazon S3.
Athena Federated Query includes pre-built connectors to a variety of AWS services and databases, as well as an SDK to build custom connectors to other databases and data stores. With this feature, federated queries can pull data from a data lake in an S3 bucket and from an external data source, and then combine it into a single result set in Athena. These connectors are an extension of the Athena query engine, which translates content between Athena and the external data source. Pre-built connectors exist for Amazon CloudWatch Logs, Amazon DynamoDB, Amazon DocumentDB (with MongoDB compatibility), and Amazon Relational Database Service (Amazon RDS), as well as a JDBC connector for Amazon Redshift, MySQL, and PostgreSQL. For other types of relational databases, you can use the Athena Federated Query SDK to create a custom connector.
In this post, we demonstrate how to deploy the custom connector between Athena and a Vertica database built using the Athena Federated Query SDK. After deploying the custom connector, we demonstrate issuing federated queries and moving data from Vertica to a data lake using CREATE TABLE AS (CTAS) with a federated query.
AWS services used in the solution
The Athena Federated Query SDK is an open-source framework to build custom connectors, and comes with a connector publish tool that deploys the connector executables in an application to the AWS Serverless Application Repository. The Athena Federated Query uses an AWS Lambda function that in turn uses the application deployed to the AWS Serverless Application Repository.
A custom connector is composed of a Lambda function that utilizes three components:
- MetadataHandler – An interface that exposes metadata information of schemas, tables, and columns from the underlying data store to Athena
- RecordHandler – An interface that provides hooks to read data from the external source and share it with the Athena query engine in Apache Arrow columnar format
- CompositeHandler – For managing running the
The Lambda function connects to the external data store using an appropriate connection protocol and sends the parsed SQL statement. In the case of Vertica, it is JDBC. The
RecordHandler processes the result set produced by the external data store and passes the rows to Athena for final processing. Multiple Lambda functions are called by Athena depending on the Lambda concurrency settings to read the result set in parallel. A spill bucket is used to handle a large dataset that exceeds the Lambda server’s capacity to process the result set.
The JDBC connection established by the Lambda function to the external database is used to send the parsed SQL statement and retrieve the result set rows from the external database. This scenario works well in terms of bandwidth for smaller databases and result sets. However, you might have Vertica deployments with petabyte or exabyte data warehouses. Typical queries return result sets on the order of 10, 20, 30 gigabytes, or more. Due to the bandwidth issue with a JDBC connection, the solution presented in this post modifies the Athena Federated Query SDK to implement a different route for the transmission of large result sets from Vertica to the Athena server for final processing.
The alternate solution utilizes the Vertica EXPORT command as a wrapper around the parsed SQL statement. You can use the EXPORT command to write a result set for a SQL statement directly to an S3 bucket using Vertica’s highly parallelized write to Amazon S3 using partitioning. This solution modifies the SDK to allow Athena to read the result set in the S3 bucket, determine the number of partitions, and call subsequent Lambda functions to parallelize the read of the result set. This produces an efficient way to move a multi-gigabyte result set from Vertica to Athena with parallelized writes from Vertica to Amazon S3 and parallelized reads from Amazon S3 to Athena. When connecting to the Vertica database, the SDK uses AWS Secrets Manager to retrieve a user ID and password for a service account on the Vertica database.
The following diagram shows the solution architecture for the Vertica custom connector when deployed to AWS.
The connector components are as follows:
- A user issues a federated SQL query in Athena against a table in Vertica.
- Athena parses the query and calls a Lambda function.
- The Lambda function makes a call to Secrets Manager to get the user ID and password for connecting to Vertica.
- The connector sends an EXPORT statement wrapper with the embedded SQL statement to Vertica through the JDBC connection. For example, see the following code:
- Vertica processes the SQL query and writes the result set to the S3 bucket specified in the EXPORT command. Vertica parallelizes the write to S3 bucket based on the
fileSizeMBparameter into as many partitions as needed for the result set.
- Athena calls a Lambda function to scan the S3 bucket in order to determine the number of files to read for the result set.
- Athena invokes multiple Lambda functions depending on the number of partitions using Amazon S3 Select. This allows Athena to parallelize the read of the S3 files.
- Athena combines the result set returned from Vertica with data scanned from the data lake, and returns the combined result set to the user.
Before you get started, make sure you have the following prerequisites:
- Amazon EC2 IAM role permissions – The AWS Identity and Access Management (IAM) role of the Amazon Elastic Compute Cloud (Amazon EC2) machines hosting the Vertica database must be given write permissions to the
VerticaExportS3 bucket, which is created when deploying the connector.
- Secrets Manager – The Vertica connection credentials are stored in Secrets Manager. The secret name is prefixed with
Vertica– and the secret value is the connection credentials.
- Lambda IAM role permissions – When the Lambda function is deployed to the AWS Serverless Application Repository, it creates a custom IAM role for the function to run. The custom role has the following IAM permissions in order to successfully perform the read and write functions associated with the
- For Secrets Manager,
GetSecretValuefor secrets with a prefix given in
- For Amazon S3, list, read, and write permissions for
ExportBucket, and list permissions for all S3 buckets
- For Athena,
To demonstrate the Athena Vertica connector capabilities, we use the following components:
- A Vertica database running in our AWS environment.
- A Vertica table called
orderscontaining details of customer orders.
- An Athena table called
customer, which has an S3 bucket as a data source. This table contains information regarding customers.
The following screenshot shows the details of the
customer table in Amazon S3.
The following screenshot shows the details of the
orders table in Vertica.
Setting up the Athena Vertica connector project
To set up your connector project, complete the following steps:
- Create an S3 bucket in your AWS account. This is the bucket where the result set from Vertica is exported.
- Create another S3 bucket in your AWS account. This is the bucket where the code for the connector is stored and retrieved.
- Grant the IAM role of the EC2 machines hosting the Vertica database read and write permissions to the S3 result set bucket, allowing Vertica to export data to the bucket.
- Clone the GitHub repo in your local folder.
- Open the project in your preferred IDE.
- From the
mvn clean install.
- From the
mvn clean install.
- From the
../tools/publish.sh <s3_code_bucket_name> athena-vertica [region]to publish the connector to your private AWS Serverless Application Repository.
- Upon successful completion of the script, the connector’s serverless application is published to the AWS Serverless Application Repository.
Deploying the connector
To deploy your connector, complete the following steps:
- On the AWS Serverless Application Repository console, choose Published Applications.
- On the Private Applications tab, select Show apps that create in order to see deployed applications.
- Choose the
- For AthenaCatalogName, enter the name of the connector Lambda function used when querying the Vertica tables (
- For SecretNameOrPrefix, enter the prefix used to store the Vertica credentials in Secrets Manager (the default is
- For SpillBucket, enter the S3 bucket name where data is spilled in case the result set data volume crosses a certain limit (
- For VerticaExportBucket, enter the S3 bucket where the result set from Vertica is exported (
- For VpcId, enter your VPC ID.
- For SpillPrefix, enter
- For SubnetIds, enter your subnet IDs.
- For VerticaConnectionString, enter the connection string of the Vertica database in the following format:
Where, vertica-username and vertica-password are the secret names of the Vertica user credentials stored in AWS Secrets Manager.
- Select I acknowledge that this app creates custom IAM roles.
- Choose Deploy.
Upon successful deployment, a Lambda function with the name given for
AthenaCatalogName is deployed in your AWS environment. We use this function to issue federated queries to Vertica. The connector is now deployed and ready to use.
Using the connector
On the Athena console, you can query Vertica tables as shown in the following code. The value for <
lambda_function> corresponds to the function you created in the previous section.
In this example, we named the function as
avc. The following screenshot shows our query results.
This demonstrates that the newly deployed connector read the user-requested columns and the Vertica source table, wrapped an EXPORT statement around the SQL statement, and ran it in Vertica. The results of this query were exported to the specified S3 bucket (
test-export-bucket) in Parquet format. The connector then invoked multiple Lambda functions to read the data from the S3 bucket using Amazon S3 Select and displayed it on the Athena console. Note that currently the connector exports Vertica
timestamptz data types as a
varchar data type. Therefore we need to use the
format) function to convert the timestamps columns into the correct data type.
We can also create an Athena table using CTAS with the result set of the Vertica query using the following query:
We can then use the newly created table to query the data as shown in the following screenshot.
In addition, we can also query and join the customer data in Amazon S3 and orders data in Vertica using the following sample query:
This query joins the orders data in Vertica with customer data in the S3 bucket in the
customer_id column and displays the results on the Athena console.
This demonstrates the ease of performing analytics across multiple platforms and data stores.
In this post, we introduced the Athena Vertica connector, its solution architecture, and demonstrated how to deploy the connector using the Athena Federated Query SDK. We saw how to run SQL queries on the Vertica data source. We also learned that we can use the connector to perform extract, transform, and load operations on the data in the Vertica tables and Amazon S3, enabling us to perform faster and better analytics across multiple platforms and data sources.
For more information about Athena Federated Query, see the GitHub repo.
Special acknowledgement goes to the Intuit Data Engineering staff Denise McInerney – Data Architect, Sanjay Rane – Group Engineering Manager – Data, and Kannan Nagarajan – Database Architect. They helped design, review, and support the development of the custom connector and architecture.
About the Authors
Kelly Ragan is a Senior Data Architect, Strategic Accounts Team, AWS Professional Services. He helps customers solve big data problems and wrestle with large-scale data warehouses. In his spare time, he enjoys snow skiing, bicycling, and camping in the Pacific Northwest.
Rohit Masur is an Associate Big Data Consultant, Data and Analytics Team, AWS Professional Services. He helps customers architect and implement solutions on AWS to get business value out of data. In his spare time, he enjoys reading books, going on long walks, and exploring new hiking trails in the Bay Area.