Query S3 CSV Files with Amazon Athena SQL in AWS Emulator
With AWS Emulator running locally, you can define an Amazon Athena table over CSV files in an S3 bucket and try searching and aggregating them with SQL, all without an AWS account. These steps are useful if you write programs that aggregate CSV log or sales data in S3 with Athena and want to check the SQL and table definitions in a local environment or in CI. I also check how a header line is handled and how CSV values enclosed in double quotes are read.
The test environment was Ubuntu 24.04 (x86_64), AWS Emulator 1.0.757 (released November 28, 2026), and AWS CLI 2.42.5. I did not use an AWS account or credentials. I have not checked how the real Amazon Athena behaves or what it costs. Where the article mentions these, the source is the official documentation. I only tested CSV with a header line. I did not test partitions, other formats such as Parquet, views, CREATE TABLE AS SELECT, or calls from an SDK such as Boto3. I also have not confirmed whether handling of date columns matches real Athena: that the result column type comes back as varchar, and that values that don't match the type are stored as-is.
- 1. Start the emulator and point the AWS CLI at it
- 2. Put the CSV in S3
- 3. A query is started, then you wait for it to finish
- 4. Define a table over the CSV with CREATE EXTERNAL TABLE
- 5. Search and aggregate with SELECT
- 6. Results can be received through the API or as a CSV in S3
- 7. Adding a file to the same folder adds rows to the table
- 8. Without skip.header.line.count, NULL rows appear
- 9. Read CSV with quoted values using OpenCSVSerde
- 10. When a query fails, check StateChangeReason
Start the emulator and point the AWS CLI at it
To install AWS Emulator, follow the official Linux instructions. You start it with aws-emulator. The -t off option turns off telemetry.
aws-emulator -t off
Keep the emulator running, open another terminal, and set the environment variables from the official documentation. This sends AWS CLI requests to the emulator and uses its credentials.
export AWS_ENDPOINT_URL=https://127.0.0.1:9297
export AWS_SHARED_CREDENTIALS_FILE=~/.aws/emulator/9297/emulator_credentials
export AWS_CONFIG_FILE=~/.aws/emulator/9297/config
export AWS_CA_BUNDLE=~/.aws/emulator/9297/ca.pem
export AWS_EC2_METADATA_DISABLED=true
export AWS_DISABLE_HOST_PREFIX_INJECTION=true
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILE
Athena already had a workgroup named primary.
$ aws athena list-work-groups --query "WorkGroups[].[Name,State,EngineVersion.EffectiveEngineVersion]" --output text
primary ENABLED Athena engine version 3
Put the CSV in S3
This is the CSV file to search. The first line is the header.
$ cat sales.csv
order_id,order_date,item,quantity,price
1,2026-11-01,apple,3,120
2,2026-11-01,banana,5,80
3,2026-11-02,apple,1,120
4,2026-11-03,cherry,10,30
5,2026-11-03,banana,2,80
Create two buckets. example-data holds the CSV files, and example-athena-results is where Athena writes query results. The CSV goes under the folder (prefix) sales/.
$ aws s3api create-bucket --bucket example-data
{
"Location": "/example-data",
"BucketArn": "arn:aws:s3:::example-data"
}
$ aws s3api create-bucket --bucket example-athena-results
{
"Location": "/example-athena-results",
"BucketArn": "arn:aws:s3:::example-athena-results"
}
$ aws s3 cp sales.csv s3://example-data/sales/sales.csv --no-progress
upload: ./sales.csv to s3://example-data/sales/sales.csv
The rest of the flow is shown in this diagram.

A query is started, then you wait for it to finish
You start an Athena SQL query with aws athena start-query-execution. As described in the StartQueryExecution API reference, it returns only the query ID. First, create the database shop.
$ aws athena start-query-execution --query-string "CREATE DATABASE IF NOT EXISTS shop" --result-configuration OutputLocation=s3://example-athena-results/
{
"QueryExecutionId": "efe721fd-692c-4c1f-9bc9-6deca9fd5134"
}
To check whether the query has finished, pass that ID to get-query-execution. The state was SUCCEEDED.
$ aws athena get-query-execution --query-execution-id efe721fd-692c-4c1f-9bc9-6deca9fd5134 --query 'QueryExecution.[Status.State,StatementType,SubstatementType]' --output text
SUCCEEDED DDL CREATE_DATABASE
Typing this by hand for every query is tedious, so I wrote a script that starts the query, waits for it, and fetches the results. The database is fixed to shop, and the results location is fixed to s3://example-athena-results/. When the state becomes SUCCEEDED, the script gets the rows with get-query-results. If the query fails, it prints the reason (StateChangeReason).
#!/bin/bash
# Usage: ./athena-query.sh "SQL"
set -eu
id=$(aws athena start-query-execution \
--query-string "$1" \
--query-execution-context Database=shop \
--result-configuration OutputLocation=s3://example-athena-results/ \
--query QueryExecutionId --output text)
for i in $(seq 30); do
state=$(aws athena get-query-execution --query-execution-id "$id" \
--query QueryExecution.Status.State --output text)
case "$state" in SUCCEEDED|FAILED|CANCELLED) break ;; esac
sleep 1
done
echo "QueryExecutionId: $id"
echo "State: $state"
if [ "$state" = SUCCEEDED ]; then
aws athena get-query-results --query-execution-id "$id" \
--query 'ResultSet.Rows[*].Data[*].VarCharValue' --output text
else
aws athena get-query-execution --query-execution-id "$id" \
--query QueryExecution.Status.StateChangeReason --output text
fi
When --query 'ResultSet.Rows[*].Data[*].VarCharValue' is combined with --output text, each result row was shown as one tab-separated line in the test environment. A NULL column has no VarCharValue, so it shows up shifted left in this output. For results that may contain NULLs, check the JSON output instead, as shown later.
Define a table over the CSV with CREATE EXTERNAL TABLE
CREATE EXTERNAL TABLE gives names and types to the CSV columns. For LOCATION, specify the folder where the CSV is stored, not a file name, and end the path with /. The Athena documentation page on specifying table locations states that Athena reads all the data in that folder and that you should not include file names or wildcards.
$ ./athena-query.sh "CREATE EXTERNAL TABLE sales (order_id int, order_date date, item string, quantity int, price int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION 's3://example-data/sales/' TBLPROPERTIES ('skip.header.line.count'='1')"
QueryExecutionId: ec260899-38e5-4363-a5a9-a552af1d6db7
State: SUCCEEDED
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' specifies rows whose fields are separated by commas. According to the Lazy Simple SerDe page, if you write only ROW FORMAT DELIMITED, Athena uses LazySimpleSerDe, the default SerDe for reading CSV. TBLPROPERTIES ('skip.header.line.count'='1') tells Athena to skip the first line of the file, which is the header.
The LOCATION page says that running CREATE TABLE registers the table in the AWS Glue Data Catalog. In the emulator, the table definition was also visible through the Glue API. Location came back without the trailing /.
$ aws glue get-table --database-name shop --name sales --query 'Table.{Type:TableType,Location:StorageDescriptor.Location,Serde:StorageDescriptor.SerdeInfo.SerializationLibrary,Columns:StorageDescriptor.Columns,SkipHeader:Parameters."skip.header.line.count"}'
{
"Type": "EXTERNAL_TABLE",
"Location": "s3://example-data/sales",
"Serde": "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe",
"Columns": [
{
"Name": "order_id",
"Type": "int"
},
{
"Name": "order_date",
"Type": "date"
},
{
"Name": "item",
"Type": "string"
},
{
"Name": "quantity",
"Type": "int"
},
{
"Name": "price",
"Type": "int"
}
],
"SkipHeader": "1"
}
Search and aggregate with SELECT
Selecting all rows returned the 5 data rows, without the header. The first row of the result contains the column names.
$ ./athena-query.sh "SELECT * FROM sales"
QueryExecutionId: 1826feb8-63a4-48f5-a6e5-a7072282aab6
State: SUCCEEDED
order_id order_date item quantity price
1 2026-11-01 apple 3 120
2 2026-11-01 banana 5 80
3 2026-11-02 apple 1 120
4 2026-11-03 cherry 10 30
5 2026-11-03 banana 2 80
Next, sales per item (sum of quantity × price), sorted from highest to lowest.
$ ./athena-query.sh "SELECT item, sum(quantity * price) AS total FROM sales GROUP BY item ORDER BY total DESC"
QueryExecutionId: 5a9fbdf0-3eef-46a4-818d-83ac29266187
State: SUCCEEDED
item total
banana 560
apple 480
cherry 300
Filtering by date also worked. The order_date column was defined as date, so the comparison uses DATE '2026-11-02'.
$ ./athena-query.sh "SELECT order_date, count(*) AS orders FROM sales WHERE order_date >= DATE '2026-11-02' GROUP BY order_date ORDER BY order_date"
QueryExecutionId: f8c9d6e2-c3b8-460c-a621-d00cd31ff6cc
State: SUCCEEDED
order_date orders
2026-11-02 1
2026-11-03 2
Results can be received through the API or as a CSV in S3
The get-query-results call used in the script returns JSON in the following form. Each row has Data, and the value is in VarCharValue. All values are strings, and the column types are in ResultSetMetadata.ColumnInfo. (Only the first two rows are shown here, and ColumnInfo is also omitted.)
$ aws athena get-query-results --query-execution-id 5a9fbdf0-3eef-46a4-818d-83ac29266187
{
"ResultSet": {
"Rows": [
{
"Data": [
{
"VarCharValue": "item"
},
{
"VarCharValue": "total"
}
]
},
{
"Data": [
{
"VarCharValue": "banana"
},
{
"VarCharValue": "560"
}
]
},
(略)
According to the page on query output files, Athena saves query results to the results location as a file named query-ID.csv. DDL results such as CREATE are saved as text files (.txt), and .metadata is the file that get-query-results uses. The page says that queries run from the AWS CLI or the API are saved directly under the results location. In the emulator, a file named with the query ID also appeared directly under the results location. The .csv file holds the SELECT result, and the .txt file holds the CREATE result.
$ aws athena get-query-execution --query-execution-id 5a9fbdf0-3eef-46a4-818d-83ac29266187 --query 'QueryExecution.[ResultConfiguration.OutputLocation,Statistics.DataScannedInBytes]' --output text
s3://example-athena-results/5a9fbdf0-3eef-46a4-818d-83ac29266187.csv 166
$ aws s3 ls s3://example-athena-results/
2026-11-28 04:13:14 226 1826feb8-63a4-48f5-a6e5-a7072282aab6.csv
2026-11-28 04:13:14 1359 1826feb8-63a4-48f5-a6e5-a7072282aab6.csv.metadata
2026-11-28 04:13:16 59 5a9fbdf0-3eef-46a4-818d-83ac29266187.csv
2026-11-28 04:13:16 550 5a9fbdf0-3eef-46a4-818d-83ac29266187.csv.metadata
2026-11-28 04:13:11 0 ec260899-38e5-4363-a5a9-a552af1d6db7.txt
2026-11-28 04:13:07 0 efe721fd-692c-4c1f-9bc9-6deca9fd5134.txt
$ aws s3 cp s3://example-athena-results/5a9fbdf0-3eef-46a4-818d-83ac29266187.csv -
"item","total"
"banana","560"
"apple","480"
"cherry","300"
In the result CSV, every value was enclosed in double quotes. The 166 in DataScannedInBytes matches the size of the sales.csv file that was read (166 bytes).
Adding a file to the same folder adds rows to the table
LOCATION points to a folder, so if you add a CSV to that folder, the next query reads it without recreating the table. I added sales2.csv, which has one data row and also a header line.
$ printf "order_id,order_date,item,quantity,price\n6,2026-11-04,apple,4,120\n" > sales2.csv
$ aws s3 cp sales2.csv s3://example-data/sales/sales2.csv --no-progress
upload: ./sales2.csv to s3://example-data/sales/sales2.csv
$ ./athena-query.sh "SELECT count(*) AS orders, sum(quantity) AS quantity FROM sales"
QueryExecutionId: 3d8f17c1-075b-4484-9e36-1a721c7fbd2c
State: SUCCEEDED
orders quantity
6 25
The table now has 6 rows, and the header line of the second file was skipped too. In the emulator, skip.header.line.count skipped the first line of each file.
The LOCATION page warns you not to put data you don't want read into the same folder. CSV files with a different shape, or memo files, are also read as table rows. Use a separate folder for each table.
Without skip.header.line.count, NULL rows appear
I created a table named sales_no_skip over the same folder, without skip.header.line.count. The query did not fail, but two rows with a NULL order_id appeared (the header lines of the two files).
$ ./athena-query.sh "CREATE EXTERNAL TABLE sales_no_skip (order_id int, order_date date, item string, quantity int, price int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION 's3://example-data/sales/'"
QueryExecutionId: 95525632-e2c5-466a-ab97-994f4f54418f
State: SUCCEEDED
$ ./athena-query.sh "SELECT * FROM sales_no_skip WHERE order_id IS NULL"
QueryExecutionId: 93808aaf-12dd-4d48-9c17-232cae4a4c1f
State: SUCCEEDED
order_id order_date item quantity price
order_date item
order_date item
This output shifts NULL columns to the left, so I checked one row in JSON. The numeric columns (order_id, quantity, price) had no value, and item contained the header text item. In the emulator, the date column order_date also held the header text order_date as-is.
$ aws athena get-query-results --query-execution-id 93808aaf-12dd-4d48-9c17-232cae4a4c1f --query 'ResultSet.Rows[1]'
{
"Data": [
{},
{
"VarCharValue": "order_date"
},
{
"VarCharValue": "item"
},
{},
{}
]
}
Because no error is raised, it is easy to miss that the count(*) result has grown by the number of header lines. For CSV files that have a header line, set skip.header.line.count.
Read CSV with quoted values using OpenCSVSerde
I prepared a CSV in which values containing commas are enclosed in double quotes. On line 2, the double quotes inside the value are written as a pair, "".
$ cat memo.csv
id,memo
1,"Tokyo, Japan"
2,"say ""hi"""
I put this file in the memo/ folder and created a table named memo_simple with the same ROW FORMAT DELIMITED as before.
$ aws s3 cp memo.csv s3://example-data/memo/memo.csv --no-progress
upload: ./memo.csv to s3://example-data/memo/memo.csv
$ ./athena-query.sh "CREATE EXTERNAL TABLE memo_simple (id int, memo string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION 's3://example-data/memo/' TBLPROPERTIES ('skip.header.line.count'='1')"
QueryExecutionId: 0ffece1f-8ec4-417f-920b-e8323ae76d15
State: SUCCEEDED
Reading it this way, the commas inside the values also split the fields, and the double quotes were kept. The Lazy Simple SerDe page also says it is for data whose values are not enclosed in quotes.
$ ./athena-query.sh "SELECT * FROM memo_simple"
QueryExecutionId: db98b56c-f739-4668-b0ac-6b55bad400a5
State: SUCCEEDED
id memo
1 "Tokyo
2 "say ""hi"""
To read quoted values, specify OpenCSVSerde with ROW FORMAT SERDE. According to the Open CSV SerDe page, the default quote character is the double quote.
$ ./athena-query.sh "CREATE EXTERNAL TABLE memo_csv (id string, memo string) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' LOCATION 's3://example-data/memo/' TBLPROPERTIES ('skip.header.line.count'='1')"
QueryExecutionId: e3aa3022-4cb8-498f-94cc-a292befb50da
State: SUCCEEDED
$ ./athena-query.sh "SELECT * FROM memo_csv"
QueryExecutionId: 8aa7f74c-7d6a-45ab-997c-17c6826c4eb5
State: SUCCEEDED
id memo
1 Tokyo, Japan
2 say "hi"
The value containing a comma stayed in one column, and "" became a single ".
The same page lists some caveats for OpenCSVSerde. It does not support line breaks inside values. For DATE columns, it reads only a number of days since January 1, 1970 (such as 18276) as a date. The page advises declaring date strings such as 2026-11-01 as string columns and converting them inside the query.
When a query fails, check StateChangeReason
If you specify a table that does not exist, start-query-execution itself does not return an error, and a query ID is returned. You can tell that the query failed because its state becomes FAILED, and the reason is shown in StateChangeReason. Here is the output in that case.
$ ./athena-query.sh "SELECT * FROM no_such_table"
QueryExecutionId: 8d411746-ce9c-49b9-9ab5-549bd0560cec
State: FAILED
TABLE_NOT_FOUND: line 1:15: Table 'awsdatacatalog.shop.no_such_table' does not exist
When a test runs an Athena query, check that the state becomes SUCCEEDED, not just that a query ID was returned.



