本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 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-
中,以您執行 Athena 之處的區域識別符 (例如 myregion
/path/to/data/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;