Building a Data Lake Module 5: Cataloging and Querying with Glue and Athena
Introduction
Files in S3 become useful when engines know their schema and location. The AWS Glue Data Catalog stores that metadata, and Amazon Athena runs SQL directly on the files and charges per amount of data scanned. This module creates a catalog table and queries it. Check current Athena pricing, and keep experiments small.
Prerequisites
- Module 4 completed
- Parquet files in an S3 bucket (see Module 3)
Step-by-Step Guide
Step 1: Understand the catalog
The Data Catalog holds databases and tables that describe data in S3: columns, types, format, location, and partitions. The data stays in S3. Athena, Glue jobs, Amazon EMR, and Redshift Spectrum all read the same catalog, so you define a table once and use it from many tools.
Step 2: Create a database and a table in Athena
In the Athena query editor, set a query results location in S3 first. Then create a database and an external table that points at your Parquet files. External means dropping the table does not delete the files.
CREATE DATABASE IF NOT EXISTS sales_lake;
CREATE EXTERNAL TABLE sales_lake.orders (
order_id bigint,
customer_id bigint,
amount double
)
PARTITIONED BY (order_date date)
STORED AS PARQUET
LOCATION 's3://acme-datalake-clean-dev/sales/orders/';Step 3: Load the partitions
A partitioned table needs to know which partitions exist. Run MSCK REPAIR TABLE to discover Hive-style folders, or use a Glue crawler (next module) or partition projection, which computes partitions from a pattern without registering each one.
MSCK REPAIR TABLE sales_lake.orders;
SHOW PARTITIONS sales_lake.orders;Step 4: Query efficiently
Athena bills by data scanned, so select only the columns you need and filter on the partition column. SELECT star on a large Parquet table reads every column. Preview with LIMIT, and check the data-scanned figure shown after each query.
SELECT order_date, count(*) AS orders, sum(amount) AS revenue
FROM sales_lake.orders
WHERE order_date BETWEEN date '2026-09-01' AND date '2026-09-07'
GROUP BY order_date
ORDER BY order_date;Step 5: Create curated tables with CTAS
CREATE TABLE AS SELECT writes the result of a query as new files and registers the table in one statement, which is a quick way to build curated datasets. Choose Parquet, compression, and partitioning in the table properties.
CREATE TABLE sales_lake.daily_revenue
WITH (
format = 'PARQUET',
write_compression = 'SNAPPY',
external_location = 's3://acme-datalake-curated-dev/sales/daily_revenue/'
) AS
SELECT order_date, count(*) AS orders, sum(amount) AS revenue
FROM sales_lake.orders
GROUP BY order_date;FAQs
- Why is my Athena query slow or expensive?
- Usual causes are reading many small files, scanning all columns, missing partition filters, and uncompressed text formats. Convert to Parquet, partition by date, and filter on the partition column.
Summary
The Glue Data Catalog records where data lives and its schema, Athena queries it with SQL, partition filters and Parquet keep scans small, and CTAS builds curated tables from query results.
Related tutorials
- Databricks Module 8: SQL Warehouses, Performance, Cost, and a First Look at MLflow
- Databricks Module 7: Jobs, Orchestration, and Deploying with Git and Bundles
- Databricks Module 6: Declarative Pipelines and the Medallion Architecture
- Databricks Module 5: Ingesting Files with Auto Loader and COPY INTO
- Databricks Module 4: Unity Catalog, Volumes, and Governance
Comments
Log in or create a free account to join the discussion.