Amazon Athena
User Guide  | API Reference

CSV (OpenCSVSerDe)

Athena offers the following options to deserialize CSV files to create Athena tables:

  • Use the OpenCSV SerDe, which lets you specify separator characters, quote characters, and escape characters. This works well for deserializing CSV files that have values enclosed in quotes; however, all columns in the table are of STRING data type, which limits available operators in subsequent queries. In addition, embedded newlines are not supported.
  • Don't specify a SerDe. In this case, the default LazySimpleSerDe is used. You can also specify this SerDe explicitly, if desired. Your data needs to be relatively clean for you to use this approach. For example, CSV files with values enclosed in quotes fail to deserialize.

CSV SerDe (OpenCSVSerde)#

SerDe Name#


Library Name#

To use this SerDe, specify the fully qualified class name, as follows:

ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'


This example presumes a source CSV file saved in s3://mybucket/mycsv/ with the following data contents:


Use a CREATE TABLE statement to create an Athena table based on this CSV file and reference the OpenCSVSerDe class in ROW FORMAT, also specifying SerDe properties for character separator, quote character, and escape character:

CREATE EXTERNAL TABLE myopencsvtable (
   col1 string,
   col2 string,
   col3 string,
   col4 string
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
   'separatorChar' = ',',
   'quoteChar' = '\"',
   'escapeChar' = '\\'
LOCATION 's3://location/of/csv/';

Query all values in the table.

SELECT * FROM myopencsvtable;

The query returns the following values.

col1     col2    col3    col4
a1       a2      a3      a4
1        2       abc     def
a        a1      abc3    ab4


Specifying this SerDe is optional. This is the SerDe for CSV that Athena uses by default if you don't specify any SerDe and only specify ROW FORMAT DELIMITED. Use this SerDe if your data is relatively clean, for example, it does not have values enclosed in quotes.

Library Name#

The Class library name for the LazySimpleSerDe is org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe. For more information about it, see LazySimpleSerDe


Use the CREATE TABLE statement to create an Athena table from the underlying CSV file stored in Amazon S3.


You can query data in regions other than the region where you run Athena. Standard inter-region data transfer rates for Amazon S3 apply in addition to standard Athena charges. To reduce data transfer charges, replace myregion in s3://athena-examples-myregion/path/to/data/ with the region identifier where you run Athena, for example, s3://athena-examples-us-east-1/path/to/data/.
CREATE EXTERNAL TABLE flight_delays_csv (
    yr INT,
    quarter INT,
    month INT,
    dayofmonth INT,
    dayofweek INT,
    flightdate STRING,
    uniquecarrier STRING,
    airlineid INT,
    carrier STRING,
    tailnum STRING,
    flightnum STRING,
    originairportid INT,
    originairportseqid INT,
    origincitymarketid INT,
    origin STRING,
    origincityname STRING,
    originstate STRING,
    originstatefips STRING,
    originstatename STRING,
    originwac INT,
    destairportid INT,
    destairportseqid INT,
    destcitymarketid INT,
    dest STRING,
    destcityname STRING,
    deststate STRING,
    deststatefips STRING,
    deststatename STRING,
    destwac INT,
    crsdeptime STRING,
    deptime STRING,
    depdelay INT,
    depdelayminutes INT,
    depdel15 INT,
    departuredelaygroups INT,
    deptimeblk STRING,
    taxiout INT,
    wheelsoff STRING,
    wheelson STRING,
    taxiin INT,
    crsarrtime INT,
    arrtime STRING,
    arrdelay INT,
    arrdelayminutes INT,
    arrdel15 INT,
    arrivaldelaygroups INT,
    arrtimeblk STRING,
    cancelled INT,
    cancellationcode STRING,
    diverted INT,
    crselapsedtime INT,
    actualelapsedtime INT,
    airtime INT,
    flights INT,
    distance INT,
    distancegroup INT,
    carrierdelay INT,
    weatherdelay INT,
    nasdelay INT,
    securitydelay INT,
    lateaircraftdelay INT,
    firstdeptime STRING,
    totaladdgtime INT,
    longestaddgtime INT,
    divairportlandings INT,
    divreacheddest INT,
    divactualelapsedtime INT,
    divarrdelay INT,
    divdistance INT,
    div1airport STRING,
    div1airportid INT,
    div1airportseqid INT,
    div1wheelson STRING,
    div1totalgtime INT,
    div1longestgtime INT,
    div1wheelsoff STRING,
    div1tailnum STRING,
    div2airport STRING,
    div2airportid INT,
    div2airportseqid INT,
    div2wheelson STRING,
    div2totalgtime INT,
    div2longestgtime INT,
    div2wheelsoff STRING,
    div2tailnum STRING,
    div3airport STRING,
    div3airportid INT,
    div3airportseqid INT,
    div3wheelson STRING,
    div3totalgtime INT,
    div3longestgtime INT,
    div3wheelsoff STRING,
    div3tailnum STRING,
    div4airport STRING,
    div4airportid INT,
    div4airportseqid INT,
    div4wheelson STRING,
    div4totalgtime INT,
    div4longestgtime INT,
    div4wheelsoff STRING,
    div4tailnum STRING,
    div5airport STRING,
    div5airportid INT,
    div5airportseqid INT,
    div5wheelson STRING,
    div5totalgtime INT,
    div5longestgtime INT,
    div5wheelsoff STRING,
    div5tailnum STRING
      ESCAPED BY '\\'
    LOCATION 's3://athena-examples-myregion/flight/csv/';

Run MSCK REPAIR TABLE to refresh partition metadata.

MSCK REPAIR TABLE flight_delays_csv;

Query the top 10 routes delayed by more than 1 hour.

SELECT origin, dest, count(*) as delays
FROM flight_delays_csv
WHERE depdelayminutes > 60
GROUP BY origin, dest


The flight table data comes from Flights provided by US Department of Transportation, Bureau of Transportation Statistics. Desaturated from original.