Avro SerDe
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>
.jargetschema
パラメータを使用して、WITH SERDEPROPERTIES
ステートメントで使用できるスキーマを返します。
java -jar avro-tools-1.8.2.jar getschema my_data.avro
Avro をダウンロードする場合は、Apache Avro リリース
スキーマを取得したら、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;