Skip to content
Visual explainer 3h to learn 5 steps

Query a partitioned S3 data lake with Athena and Glue

Catalog a partitioned S3 data set with Glue, query it with Athena and cut scan cost with formats and partitions.

By Invicly Tech, , in AWS Cloud Engineering. About 12 minutes to read.

Topic 21 of 38

In short

A data lake on AWS is just files in S3 plus a catalog that says what those files mean. The AWS Glue Data Catalog stores table definitions (columns, format, location, partitions), and Amazon Athena runs SQL over the files without any servers to manage. Athena bills by the bytes it scans, so the way you lay out and format the files decides both speed and cost.

Think of it like this: S3 is the warehouse shelves, the Glue Data Catalog is the index card system, and Athena is the clerk who only walks to the shelves the index cards point at.

Explained simply

Our file-processing service writes one small result record for every file it handles: the file ID, its status, its size and when it was processed. After a few months you want answers like how many files failed last week. You could load everything into a database, but there is a cheaper way: leave the records as files in S3 and query them where they sit. That setup is called a data lake.

To query files, something has to know what is inside them. The AWS Glue Data Catalog is that something. It holds table definitions: the column names and types, the file format and the S3 folder. A Glue crawler can read sample files and guess the definition for you, or you can write it yourself with a CREATE TABLE statement.

Amazon Athena is the query engine. You type SQL in the console or call it from the CLI, Athena reads the files the catalog points to, and it writes the results to an S3 bucket you choose. There are no servers to start or stop. You pay for the amount of data each query scans, so a query that reads less costs less.

Two habits keep scans small. First, split the data into folders by a column you filter on, usually a date. These folders are called partitions, and Athena skips the ones your query does not need. Second, store curated data as Parquet instead of CSV or JSON. Parquet keeps each column together and compresses it, so Athena reads only the columns you ask for.

Cost note: Athena, Glue crawlers and S3 are paid services, but a lab with a few megabytes of synthetic data costs cents. Athena has a minimum charge of 10 MB per query. If you want zero cost, you can practise the same ideas locally with DuckDB, which reads partitioned Parquet folders with plain SQL.

Press Play on a diagram to walk through it one step at a time.

FlowFrom uploaded files to a cheap Athena query
  1. 1
    Land raw files

    Worker writes JSON results to s3://example-lab-lake/raw/

  2. 2
    Partition by date

    Keys like processed_date=2026-12-01/

  3. 3
    Catalog the table

    Glue crawler, DDL or partition projection

  4. 4
    Convert to Parquet

    Athena CTAS or a Glue job writes curated/

  5. 5
    Query with filters

    WHERE processed_date = ... scans one folder

SequenceWhat happens when you run one Athena query
Analyst
Athena
Glue Data Catalog
S3
  1. 1SELECT ... WHERE processed_date = '2026-12-01'
    Analyst to Athena: SELECT ... WHERE processed_date = '2026-12-01'
  2. 2Get table schema and partition location
    Athena to Glue Data Catalog: Get table schema and partition location
  3. 3Parquet, columns, one matching partition
    Glue Data Catalog to Athena: Parquet, columns, one matching partition
  4. 4Read only needed columns in that folder
    Athena to S3: Read only needed columns in that folder
  5. 5Write results CSV to results bucket
    Athena to S3: Write results CSV to results bucket
  6. 6Rows plus data scanned for billing
    Athena to Analyst: Rows plus data scanned for billing

In depth

Partition layout and how Athena finds partitions

Hive-style partition folders put the column name in the path: s3://example-lab-lake/curated/file_results/processed_date=2026-12-01/part-0001.parquet. Athena maps processed_date to a virtual column you can filter on. But Athena only reads partitions it knows about, and there are three ways to tell it. MSCK REPAIR TABLE scans the table location and adds any Hive-style folders it finds; simple but slow when there are thousands of folders. ALTER TABLE ADD PARTITION adds one partition explicitly, which a pipeline can run after writing a new day. Partition projection skips the catalog lookup entirely: you describe the pattern in table properties (for example a date range and format) and Athena calculates the folder paths itself. Projection is the usual choice for date partitions that grow every day, because nobody has to remember to register them.

  • Partition by what you filter on, usually date or region
  • Aim for files of roughly 100 MB or more, not thousands of tiny ones
  • Avoid partitioning by IDs with millions of distinct values
  • Partition projection: no repair jobs for daily folders

Why file format changes the bill

Athena pricing in most Regions is about 5 US dollars per terabyte scanned (check the current Athena pricing page for your Region). Scanned bytes are measured after compression. A CSV or JSON file is row-based: to read one column Athena must read every row in full. Parquet and ORC are columnar: each column is stored in its own chunks with min and max statistics, so Athena reads only the needed columns and can skip chunks whose statistics rule out a match. A typical result: the same data is a fraction of the size in Parquet with Snappy compression, and a query reading two of ten columns reads only a few percent of the bytes. The easiest conversion in a lab is an Athena CTAS query that selects from the raw JSON table and writes Parquet into a curated prefix.

CompareSame data, three formats
CSV
  • Row-based text
  • No types or schema inside
  • Reads every column
  • Easy to inspect by eye
JSON lines
  • Row-based text
  • Nested fields allowed
  • Reads every column
  • Common raw landing format
Parquet
  • Columnar and binary
  • Types and statistics stored
  • Reads only needed columns
  • Best for curated tables

Controlling access and runaway queries

Athena queries run with the caller's IAM permissions. A user needs permission for Athena actions, for the Glue Data Catalog database and table, for s3:GetObject and s3:ListBucket on the data location, and for writing to the query results location. Keep the results bucket separate from the data bucket and give it a lifecycle rule, because every query writes a result file there. Workgroups let you separate teams or labs: each workgroup can force a results location, force encryption and set a per-query data usage limit, which cancels any query that scans more than the limit. For finer control, such as hiding a column from some readers, register the location with Lake Formation, covered in the governance topic.

  • Per-query limit: cancels a query above N bytes scanned
  • Results bucket: lifecycle rule to expire old results
  • Use a lab workgroup so costs show up separately
  • Query history shows data scanned for each query

Troubleshooting empty or wrong results

Most Athena surprises come from a mismatch between the catalog and the files. Zero rows usually means partitions are not registered, the LOCATION path is wrong (it must end at the folder, not a file), or the partition folder name does not match the declared column. HIVE_BAD_DATA or type errors mean a file does not match the declared types, for example a string in a bigint column. Duplicate or odd rows often mean stray files, such as a _SUCCESS marker or an old CSV, sit in the same prefix. A crawler can also create one table per folder if the files in different folders look too different. Fix it by making layouts consistent or by writing the DDL yourself.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Lay out partitions in S3~35m
  2. 2
    Register tables in the Glue Data Catalog~35m
  3. 3
    Query with Athena~35m
  4. 4
    Compare CSV, JSON and Parquet~35m
  5. 5
    Limit scanned bytes and access~35m

Key points

  • The data stays in S3; the Glue Data Catalog only holds metadata about it.
  • Athena charges per terabyte scanned, so partitions and columnar formats cut the bill directly.
  • Partition columns only help when your queries filter on them.
  • Parquet stores data by column and compresses it, so a query reading three columns skips the rest.
  • Workgroups set a results location and can cap the bytes a single query may scan.

Key terms

Tap a card to flip it. 0/6 known

Examples

sql
CREATE EXTERNAL TABLE lab.file_results (
  file_id string, status string, bytes bigint
)
PARTITIONED BY (processed_date string)
STORED AS PARQUET
LOCATION 's3://example-lab-lake/curated/file_results/';

MSCK REPAIR TABLE lab.file_results;  -- load Hive-style partitions

SELECT status, count(*) AS files
FROM lab.file_results
WHERE processed_date = '2026-12-01'
GROUP BY status;

Define a partitioned Parquet table, load its partitions, then query one day.

Convert raw JSON results to partitioned Parquet with CTAS

Prerequisites: a sandbox account, a bucket named example-lab-lake with synthetic JSON lines under raw/, and a table lab.raw_results defined over it. Step 1: run the CTAS below in a lab workgroup. Step 2: check the Recent queries tab and note data scanned. Step 3: run the same aggregate against both tables; the Parquet query should scan far fewer bytes. Evidence: Parquet files appear under curated/file_results/processed_date=.../. Cleanup: DROP TABLE removes only metadata, so also delete the curated prefix with aws s3 rm --recursive.

sql
CREATE TABLE lab.file_results
WITH (
  format = 'PARQUET',
  write_compression = 'SNAPPY',
  external_location = 's3://example-lab-lake/curated/file_results/',
  partitioned_by = ARRAY['processed_date']
) AS
SELECT file_id, status, bytes,
       substr(processed_at, 1, 10) AS processed_date
FROM lab.raw_results;

Use partition projection so new days appear automatically

The worker writes a new date folder every day, and someone keeps forgetting MSCK REPAIR TABLE. Step 1: add projection properties to the table, describing processed_date as a date with a known format and range. Step 2: query yesterday's date without running any repair. Evidence: rows come back for a folder that was never added to the catalog. Rollback: set projection.enabled to false and register partitions again the old way.

sql
ALTER TABLE lab.file_results SET TBLPROPERTIES (
  'projection.enabled' = 'true',
  'projection.processed_date.type' = 'date',
  'projection.processed_date.format' = 'yyyy-MM-dd',
  'projection.processed_date.range' = '2026-01-01,NOW',
  'storage.location.template' =
    's3://example-lab-lake/curated/file_results/processed_date=${processed_date}/'
);

Quick reference

  • Athena bills per byte scanned (minimum 10 MB per query); partitions and Parquet cut scanned bytes.
  • MSCK REPAIR TABLE loads Hive-style partitions; ALTER TABLE ADD PARTITION adds one at a time.
  • Partition projection computes partition paths from table properties, no catalog updates needed.
  • CTAS with format = 'PARQUET' converts raw data into a new curated table.
  • DROP TABLE on an external table deletes metadata only; delete the S3 prefix separately.
  • Workgroups enforce result location, encryption and a per-query scan limit.
  • Queries need Athena, Glue and S3 permissions plus write access to the results location.

Common mistakes

  • Forgetting to load new partitions, so Athena returns zero rows for yesterday's data.
  • Partitioning by a high-cardinality value like file_id, creating millions of tiny folders.
  • Running SELECT * on raw JSON, which scans every byte of every column.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which change usually reduces Athena cost the most for a query reading 3 of 20 columns?

Question 2

2New daily folders exist in S3 but Athena returns no rows for them. The table has no partition projection. What is missing?

Question 3

3What does the Glue Data Catalog store?

Question 4

4Which partition key is the worst choice for a table of file results?

Question 5

5How can you stop a single lab query from scanning a huge amount of data by mistake?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create a local partitioned data set and query one partition with DuckDB, without any AWS account.

  2. Exercise 2medium

    In a sandbox account, define an Athena table over JSON lines in s3://example-lab-lake/raw/, run a count, and record the data scanned.

  3. Exercise 3hard

    Write a least-privilege IAM policy for an analyst who may query only the lab database through the lab workgroup.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need a Glue crawler to use Athena?
No. A crawler is convenient for guessing schemas, but you can define tables with CREATE EXTERNAL TABLE in Athena, which is more predictable and costs nothing extra.
Is Athena free?
No. You pay per byte scanned, plus normal S3 storage and requests. A small lab costs cents, and you can practise locally with DuckDB for free.
Can Athena update or delete rows?
Not in plain external tables. Apache Iceberg tables in Athena support UPDATE, DELETE and MERGE, which is the usual route when you need row changes.
Why are my query results stored in S3?
Athena writes every result set to the results location so you can download or reuse it. Add a lifecycle rule so those files do not pile up.

Go deeper

ShareXLinkedInWhatsApp