

# Avro SerDe
<a name="avro-serde"></a>

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

## 序列化库名称
<a name="avro-serde-library-name"></a>

Avro SerDe 的序列化库名称是 `org.apache.hadoop.hive.serde2.avro.AvroSerDe`。有关技术信息，请参阅 Apache 文档中的 [AvroSerDe](https://cwiki.apache.org/confluence/display/Hive/AvroSerDe)。

## 使用 Avro SerDe
<a name="avro-serde-using"></a>

出于安全原因，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 版本](http://avro.apache.org/releases.html#Download)。要直接下载 Apache Avro 工具，请参阅 [Apache Avro 工具 Maven 存储库](https://mvnrepository.com/artifact/org.apache.avro/avro-tools)。

获取架构后，请使用 `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;
```

**注意**  
飞行表数据来自由美国运输部[交通统计局](http://www.transtats.bts.gov/)提供的[航班](http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236&amp;DB_Short_Name=On-Time)。从原来的数据中进行稀释。