用于 CSV、TSV 和自定义分隔文件的LazySimpleSerDe - Amazon Athena
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

用于 CSV、TSV 和自定义分隔文件的LazySimpleSerDe

指定此 SerDe 是可选的。这是 SerDe 在默认情况下使用的 CSV、TSV 和自定义分隔格式数据的 Athena。如果您不指定任何 SerDe 并且仅指定 SerDe,则会使用此 ROW FORMAT DELIMITED。 如果您的数据没有用引号引起来的值,请使用此 SerDe。

有关 LazySimpleSerDe的参考文档,请参阅《Apache Hive 开发人员指南》中的 Hive SerDe 部分。

库名称

的类库名称为 LazySimpleSerDe。org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe 有关 LazySimpleSerDe 类的信息,请参阅 LazySimpleSerDe

忽略标头

要在定义表时忽略数据中的标头,您可以使用 skip.header.line.count 表属性,如以下示例所示。

TBLPROPERTIES ("skip.header.line.count"="1")

有关示例,请参阅CREATE TABLE查询 Amazon VPC 流日志中的 查询 Amazon CloudFront 日志 语句。

Examples

以下示例说明如何使用 Athena 根据 CSV 和 TSV 在 LazySimpleSerDe 中创建表。 要使用此 SerDe 反序列化自定义分隔的文件,请使用 FIELDS TERMINATED BY 子句指定单字符分隔符,如以下示例所示。LazySimpleSerDe 不支持多字符分隔符。

CSV 示例

使用 CREATE TABLE 语句根据在 Amazon S3 中存储的底层 CSV 数据创建一个 Athena 表。

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 ) PARTITIONED BY (year STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ESCAPED BY '\\' LINES TERMINATED BY '\n' LOCATION 's3://athena-examples-myregion/flight/csv/';

每次将新分区添加到此表时,请运行 MSCK REPAIR TABLE 以刷新分区元数据:

MSCK REPAIR TABLE flight_delays_csv;

查询前 10 个延迟超过 1 小时的路线:

SELECT origin, dest, count(*) as delays FROM flight_delays_csv WHERE depdelayminutes > 60 GROUP BY origin, dest ORDER BY 3 DESC LIMIT 10;

TSV 示例

此示例假定源 TSV 数据保存在 s3://mybucket/mytsv/ 中。

使用 CREATE TABLE 语句根据在 Amazon S3 中存储的 TSV 数据创建一个 Athena 表。请注意,此示例不会引用 SerDe 中的任何 ROW FORMAT 类,因为它使用 LazySimpleSerDe,并且它可以省略。该示例指定字符和行分隔符的 SerDe 属性,以及转义字符:

CREATE EXTERNAL TABLE flight_delays_tsv ( 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 ) PARTITIONED BY (year STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ESCAPED BY '\\' LINES TERMINATED BY '\n' LOCATION 's3://athena-examples-myregion/flight/tsv/';

每次将新分区添加到此表时,请运行 MSCK REPAIR TABLE 以刷新分区元数据:

MSCK REPAIR TABLE flight_delays_tsv;

查询前 10 个延迟超过 1 小时的路线:

SELECT origin, dest, count(*) as delays FROM flight_delays_tsv WHERE depdelayminutes > 60 GROUP BY origin, dest ORDER BY 3 DESC LIMIT 10;