Prolong your information mesh with Amazon Athena and federated views

Spread the love

Amazon Athena is a serverless, interactive analytics service constructed on the Trino, PrestoDB, and Apache Spark open-source frameworks. You should use Athena to run SQL queries on petabytes of information saved on Amazon Easy Storage Service (Amazon S3) in extensively used codecs reminiscent of Parquet and open-table codecs like Apache Iceberg, Apache Hudi, and Delta Lake. Nonetheless, Athena additionally permits you to question information saved in 30 completely different information sources—along with Amazon S3—together with relational, non-relational, and object shops working on premises or in different cloud environments.

In Athena, we consult with queries on non-Amazon S3 information sources as federated queries. These queries run on the underlying database, which implies you possibly can analyze the information with out studying a brand new question language and with out the necessity for separate extract, remodel, and cargo (ETL) scripts to extract, duplicate, and put together information for evaluation.

Lately, Athena added help for creating and querying views on federated information sources to carry larger flexibility and ease of use to make use of circumstances reminiscent of interactive evaluation and enterprise intelligence reporting. Athena additionally up to date its information connectors with optimizations that enhance efficiency and scale back value when querying federated information sources. The up to date connectors use dynamic filtering and an expanded set of predicate pushdown optimizations to carry out extra operations within the underlying information supply slightly than in Athena. Because of this, you get quicker queries with much less information scanned, particularly on tables with thousands and thousands to billions of rows of information.

On this put up, we present create and question views on federated information sources in an information mesh structure that includes information producers and customers.

The time period information mesh refers to a knowledge structure with decentralized information possession. A knowledge mesh allows domain-oriented groups with the information they want, emphasizes self-service, and promotes the notion of purpose-built information merchandise. In an information mesh, information producers expose datasets to the group and information customers subscribe to and eat the information merchandise created by producers. By distributing information possession to cross-functional groups, an information mesh can foster a tradition of collaboration, invention, and agility round information.

Let’s dive into the answer.

Answer overview

For this put up, think about a hypothetical ecommerce firm that makes use of a number of information sources, every enjoying a special position:

  • In an S3 information lake, ecommerce information are saved in a desk named Lineitems
  • Amazon ElastiCache for Redis shops Nations and ActiveOrders information, making certain ultra-fast reads of operational information by downstream ecommerce programs
  • On Amazon Relational Database Service (Amazon RDS), MySQL is used to retailer information like e-mail addresses and transport addresses within the Orders, Buyer, and Suppliers tables
  • For flexibility and low-latency reads and writes, an Amazon DynamoDB desk holds Half and Partsupp information

We wish to question these information sources in an information mesh design. Within the following sections, we arrange Athena information supply connectors for MySQL, DynamoDB, and Redis, after which run queries that carry out complicated joins throughout these information sources. The next diagram depicts our information structure.

Architecture diagram

As you proceed with this answer, notice that you’ll create AWS sources in your account. We’ve got offered you with an AWS CloudFormation template that defines and configures the required sources, together with the pattern MySQL database, S3 tables, Redis retailer, and DynamoDB desk. The template additionally creates the AWS Glue database and tables, S3 bucket, Amazon S3 VPC endpoint, AWS Glue VPC endpoint, and different AWS Identification and Entry Administration (IAM) sources which are used within the answer.

The template is designed to display use federated views in Athena, and isn’t supposed for manufacturing use with out modification. Moreover, the template makes use of the us-east-1 Area and won’t work in different Areas with out modification. The template creates sources that incur prices whereas they’re in use. Comply with the cleanup steps on the finish of this put up to delete the sources and keep away from pointless costs.


Earlier than you launch the CloudFormation stack, guarantee you’ve got the next conditions:

  • An AWS account that gives entry to AWS companies
  • An IAM consumer with an entry key and secret key to configure the AWS Command Line Interface (AWS CLI), and permissions to create an IAM position, IAM insurance policies, and stacks in AWS CloudFormation

Create sources with AWS CloudFormation

To get began, full the next steps:

  1. Select Launch Stack: Cloudformation Launch Stack
  2. Choose I acknowledge that this template could create IAM sources.

The CloudFormation stack takes roughly 20–half-hour to finish. You possibly can monitor its progress on the AWS CloudFormation console. When standing reads CREATE_COMPLETE, your AWS account could have the sources essential to implement this answer.

Deploy connectors and hook up with information sources

With our sources provisioned, we are able to start to attach the dots in our information mesh. Let’s begin by connecting the information sources created by the CloudFormation stack with Athena.

  1. On the Athena console, select Knowledge sources within the navigation pane.
  2. Select Create information supply.
  3. For Knowledge sources, choose MySQL, then select Subsequent.
  4. For Knowledge supply identify, enter a reputation, reminiscent of mysql. The Athena connector for MySQL is an AWS Lambda operate that was created for you by the CloudFormation template.
  5. For Connection particulars, select Choose or enter a Lambda operate.
  6. Select mysql, then select Subsequent.
  7. Assessment the knowledge and select Create information supply.
  8. Return to the Knowledge sources web page and select mysql.
  9. On the connector particulars web page, select the hyperlink below Lambda operate to entry the Lambda console and examine the operate related to this connector.
    mysql Data Soruce details
  10. Return to the Athena question editor.
  11. For Knowledge supply, select mysql.
  12. For Database, select the gross sales database.
  13. For Tables, it is best to see an inventory of MySQL tables which are prepared so that you can question.
  14. Repeat these steps to arrange the connectors for DynamoDB and Redis.

In any case 4 information sources are configured, we are able to see the information sources on the Knowledge supply drop-down menu. All different databases and tables, just like the lineitem desk, which is saved on Amazon S3, are outlined within the AWS Glue Knowledge Catalog and might be accessed by selecting AwsDataCatalog as the information supply.

This image shows AwsDataCatalog is being selected as Data Source

Analyze information with Athena

With our information sources configured, we’re prepared to begin working queries and utilizing federated views in an information mesh structure. Let’s begin by looking for out how a lot revenue was made on a given line of elements, damaged out by provider nation and 12 months.

For such a question, we have to calculate, for every nation and 12 months, the revenue for elements ordered in annually that have been stuffed by a provider in every nation. Revenue is outlined because the sum of [(l_extendedprice*(1-l_discount)) - (ps_supplycost * l_quantity)] for all line objects describing elements within the specified line.

Answering this query requires querying all 4 information sources—MySQL, DynamoDB, Redis, and Amazon S3—and is completed with the next SQL:

    n_name nation,
	12 months(CAST(o_orderdate AS date)) as o_year,
	((l_extendedprice * (1 - l_discount)) - (CAST(ps_supplycost AS double) * l_quantity)) as quantity
    mysql.gross sales.provider,
    mysql.gross sales.orders,
    ((s_suppkey = l_suppkey)
    AND (ps_suppkey = l_suppkey)
	AND (ps_partkey = l_partkey)
	AND (p_partkey = l_partkey)
	AND (o_orderkey = l_orderkey)
	AND (s_nationkey = CAST(Regexp_extract(_key_, '.*-(.*)', 1) AS int)))

Working this question on the Athena console produces the next consequence.

Result of above query

This question is pretty complicated: it includes a number of joins and requires particular information of the proper strategy to calculate revenue metrics that different end-users could not possess.

To simplify the evaluation expertise for these customers, we are able to disguise this complexity behind a view. For extra info on utilizing views with federated information sources, see Querying federated views.

Use the next question to create the view within the data_lake database below the AwsDataCatalog information supply:

CREATE OR REPLACE VIEW "data_lake"."federated_view" AS
    n_name nation,
	12 months(CAST(o_orderdate AS date)) as o_year,
	((l_extendedprice * (1 - l_discount)) - (CAST(ps_supplycost AS double) * l_quantity)) as quantity
    mysql.gross sales.provider,
    mysql.gross sales.orders,
    ((s_suppkey = l_suppkey)
    AND (ps_suppkey = l_suppkey)
	AND (ps_partkey = l_partkey)
	AND (p_partkey = l_partkey)
	AND (o_orderkey = l_orderkey)
	AND (s_nationkey = CAST(Regexp_extract(_key_, '.*-(.*)', 1) AS int)))

Subsequent, run a easy choose question to validate the view was created efficiently: SELECT * FROM federated_view restrict 10

The consequence needs to be much like our earlier question.

With our view in place, we are able to carry out new analyses to reply questions that might be difficult with out the view as a result of complicated question syntax that might be required. For instance, we are able to discover the whole revenue by nation:

SELECT nation, sum(quantity) AS complete
from federated_view
GROUP BY nation 

Your outcomes ought to resemble the next screenshot.

Result of above query

As you now see, the federated view makes it easier for end-users to run queries on this information. Customers are free to question a view of the information, outlined by a educated information producer, slightly than having to first purchase experience in every underlying information supply. As a result of Athena federated queries are processed the place the information is saved, with this strategy, we keep away from duplicating information from the supply system, saving useful time and value.

Use federated views in a multi-user mannequin

Thus far, we’ve glad one of many ideas of an information mesh: we created an information product (federated view) that’s decoupled from its originating supply and is obtainable for on-demand evaluation by customers.

Subsequent, we take our information mesh a step additional by utilizing federated views in a multi-user mannequin. To maintain it easy, assume we’ve one producer account, the account we used to create our 4 information sources and federated view, and one client account. Utilizing the producer account, we give the buyer account permission to question the federated view from the buyer account.

The next determine depicts this setup and our simplified information mesh structure.

Multi-user model setup

Comply with these steps to share the connectors and AWS Glue Knowledge Catalog sources from the producer, which incorporates our federated view, with the buyer account:

  1. Share the information sources mysql, redis, dynamo, and data_lake with the buyer account. For directions, consult with Sharing an information supply in Account A with Account B. Word that Account A represents the producer and Account B represents the buyer. Be sure to use the identical information supply names from earlier when sharing information. That is essential for the federated view to work in a cross-account mannequin.
  2. Subsequent, share the producer account’s AWS Glue Knowledge Catalog with the buyer account by following the steps in Cross-account entry to AWS Glue information catalogs. For the information supply identify, use shared_federated_catalog.
  3. Change to the buyer account, navigate to the Athena console, and confirm that you simply see federated_view listed below Views within the shared_federated_catalog Knowledge Catalog and data_lake database.
  4. Subsequent, run a pattern question on the shared view to see the question outcomes.

Result of sample query

Clear up

To wash up the sources created for this put up, full the next steps:

  1. On the Amazon S3 console, empty the bucket athena-federation-workshop-<account-id>.
  2. In case you’re utilizing the AWS CLI, delete the objects within the athena-federation-workshop-<account-id> bucket with the next code. Be sure to run this command on the proper bucket.
    aws s3 rm s3://athena-federation-workshop-<account-id> --recursive
  3. On the AWS CloudFormation console or the AWS CLI, delete the stack athena-federated-view-blog.


On this put up, we demonstrated the performance of Athena federated views. We created a view spanning 4 completely different federated information sources and ran queries towards it. We additionally noticed how federated views could possibly be prolonged to a multi-user information mesh and ran queries from a client account.

To benefit from federated views, guarantee you might be utilizing Athena engine model 3 and improve your information supply connectors to the newest model out there. For info on improve a connector, see Updating an information supply connector.

Concerning the Authors

Saurabh Bhutyani is a Principal Large Knowledge Specialist Options Architect at AWS. He’s captivated with new applied sciences. He joined AWS in 2019 and works with prospects to offer architectural steerage for working scalable analytics options and information mesh architectures utilizing AWS analytics companies like Amazon EMR, Amazon Athena, AWS Glue, AWS Lake Formation, and Amazon DataZone.

Pathik Shah is a Sr. Large Knowledge Architect on Amazon Athena. He joined AWS in 2015 and has been focusing within the large information analytics house since then, serving to prospects construct scalable and strong options utilizing AWS analytics companies.

Leave a Reply

Your email address will not be published. Required fields are marked *