选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

Avro SerDe

聚焦模式
Avro SerDe - Amazon Athena

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

序列化库名称

Avro SerDe 的序列化库名称是 org.apache.hadoop.hive.serde2.avro.AvroSerDe。有关技术信息,请参阅 Apache 文档中的 AvroSerDe

使用 Avro SerDe

出于安全原因,Athena 不支持使用 avro.schema.url 指定表架构;改为使用 avro.schema.literal

要从 Avro 格式的数据中提取架构,请使用位于已安装的 Avro 版本的 java 子目录中的 Apache avro-tools-<version>.jar 文件。使用 getschema 参数返回可在 WITH SERDEPROPERTIES 语句中使用的架构,如以下示例所示。

java -jar avro-tools-1.8.2.jar getschema my_data.avro

要下载 Avro,请参阅 Apache Avro 版本。要直接下载 Apache Avro 工具,请参阅 Apache Avro 工具 Maven 存储库

获取架构后,请使用 CREATE TABLE 语句根据 Amazon S3 中存储的底层 Avro 数据创建一个 Athena 表。要在您的 CREATE TABLE 语句中指定 Avro SerDe,请使用 ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'。使用 WITH SERDEPROPERTIES 子句指定架构,如以下示例所示。

注意

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

CREATE EXTERNAL TABLE flights_avro_example ( yr INT, flightdate STRING, uniquecarrier STRING, airlineid INT, carrier STRING, flightnum STRING, origin STRING, dest STRING, depdelay INT, carrierdelay INT, weatherdelay INT ) PARTITIONED BY (year STRING) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' WITH SERDEPROPERTIES ('avro.schema.literal'=' { "type" : "record", "name" : "flights_avro_subset", "namespace" : "default", "fields" : [ { "name" : "yr", "type" : [ "null", "int" ], "default" : null }, { "name" : "flightdate", "type" : [ "null", "string" ], "default" : null }, { "name" : "uniquecarrier", "type" : [ "null", "string" ], "default" : null }, { "name" : "airlineid", "type" : [ "null", "int" ], "default" : null }, { "name" : "carrier", "type" : [ "null", "string" ], "default" : null }, { "name" : "flightnum", "type" : [ "null", "string" ], "default" : null }, { "name" : "origin", "type" : [ "null", "string" ], "default" : null }, { "name" : "dest", "type" : [ "null", "string" ], "default" : null }, { "name" : "depdelay", "type" : [ "null", "int" ], "default" : null }, { "name" : "carrierdelay", "type" : [ "null", "int" ], "default" : null }, { "name" : "weatherdelay", "type" : [ "null", "int" ], "default" : null } ] } ') STORED AS AVRO LOCATION 's3://athena-examples-myregion/flight/avro/';

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

MSCK REPAIR TABLE flights_avro_example;

按照出发总数查询前 10 个出发城市。

SELECT origin, count(*) AS total_departures FROM flights_avro_example WHERE year >= '2000' GROUP BY origin ORDER BY total_departures DESC LIMIT 10;
注意

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

本页内容

隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。