Avro SerDe
SerDe 名
ライブラリ名
org.apache.hadoop.hive.serde2.avro.AvroSerDe
例
セキュリティ上の理由から、Athena では、テーブルスキーマを指定するための avro.schema.url
の使用がサポートされていません。avro.schema.literal
を使用します。Avro 形式のデータからスキーマを抽出するには、Apache の avro-tools-<version>.jar
で getschema
パラメータを使用します。これによって返されるスキーマを WITH SERDEPROPERTIES
ステートメントで使用できます。以下に例を示します。
java -jar avro-tools-1.8.2.jar getschema my_data.avro
avro-tools-<version>.jar
ファイルは、インストールした Avro リリースの java
サブディレクトリにあります。Avro をダウンロードする場合は、Apache Avro リリース
スキーマを取得したら、CREATE TABLE
ステートメントを使用して、Amazon S3 に保存されている基盤となる Avro データに基づいて Athena テーブルを作成します。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;