

# 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 releases](http://avro.apache.org/releases.html#Download)(Apache Avro 릴리스)를 참조하세요. Apache Avro Tools를 직접 다운로드하려면 [Apache Avro tools Maven repository](https://mvnrepository.com/artifact/org.apache.avro/avro-tools)(Apache Avro Tools 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;
```

**참고**  
비행 테이블 데이터의 출처는 미국 운수부 [운송통계국](http://www.transtats.bts.gov/)에서 제공한 [항공편](http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236&amp;DB_Short_Name=On-Time)입니다. 원본에서 채도를 낮췄습니다.