Parquet SerDe - Amazon Athena

Parquet SerDe

使用 Parquet SerDe 从 Parquet 数据中创建 Athena 表。

Parquet SerDe 用于采用 Parquet 格式存储的数据。要将数据转换为 Parquet 格式,您可以使用 CREATE TABLE AS SELECT (CTAS) 查询。有关更多信息,请参阅从查询结果创建表(CTAS)CTAS 查询的示例将 CTAS 和 INSERT INTO 用于 ETL 和数据分析

序列化库名称

Parquet SerDe 的序列化库名称为 org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe。有关源代码信息,请参阅 Apache 文档中的 Class ParquetHiveSerDe

注意

s3://athena-examples-myregion/path/to/data/ 中,将 myregion 替换为您运行 Athena 所在的区域标识符,例如 s3://athena-examples-us-west-1/path/to/data/

使用以下 CREATE TABLE 语句,根据 Amazon S3 中以 Parquet 格式存储的底层数据创建一个 Athena 表:

CREATE EXTERNAL TABLE flight_delays_pq ( 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) STORED AS PARQUET LOCATION 's3://athena-examples-myregion/flight/parquet/' tblproperties ("parquet.compression"="SNAPPY");

在该表上运行 MSCK REPAIR TABLE 语句以刷新分区元数据:

MSCK REPAIR TABLE flight_delays_pq;

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

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

飞行表数据来自由美国运输部交通统计局提供的航班。从原来的数据中进行稀释。

忽略 Parque 统计数据

当读取 Parquet 数据时,您可能会收到如下错误消息:

HIVE_CANNOT_OPEN_SPLIT: Index x out of bounds for length y HIVE_CURSOR_ERROR: Failed to read x bytes HIVE_CURSOR_ERROR: FailureException at Malformed input: offset=x HIVE_CURSOR_ERROR: FailureException at java.io.IOException: can not read class org.apache.parquet.format.PageHeader: Socket is closed by peer.

要解决此问题,请使用 CREATE TABLEALTER TABLE SET TBLPROPERTIES 语句将 Parquet Serde parquet.ignore.statistics 属性设置为 true,如以下示例所示。

CREATE TABLE 示例

... ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' WITH SERDEPROPERTIES ( 'parquet.ignore.statistics'='true') STORED AS PARQUET ...

ALTER TABLE 示例

ALTER TABLE ... SET TBLPROPERTIES ('parquet.ignore.statistics'='true')