

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 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/` 中，以您執行 Athena 之處的區域識別符 (例如 `s3://athena-examples-us-west-1/path/to/data/`) 取代 {{myregion}}。

```
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)。從原本資料去掉飽和度。