Avro SerDe - Amazon Athena

Avro SerDe

Utilizzare Avro SerDe per creare tabelle Athena dai dati Avro.

Nome della libreria di serializzazione

Il nome della libreria di serializzazione per Avro SerDe è org.apache.hadoop.hive.serde2.avro.AvroSerDe. Per informazioni tecniche, vedere AvroserDe nella documentazione diApache.

Utilizzare Avro SerDe

Per motivi di sicurezza, Athena non supporta l'utilizzo di avro.schema.url per specificare lo schema di tabella; utilizzare invece avro.schema.literal.

Per estrarre lo schema dai dati in formato Avro, utilizzare il file avro-tools-<version>.jar Apache che si trova nella sottodirectory java della versione Avro installata. Utilizzare il parametro getschema per restituire uno schema che può essere utilizzato nella dichiarazione WITH SERDEPROPERTIES, come nell'esempio seguente.

java -jar avro-tools-1.8.2.jar getschema my_data.avro

Per scaricare Avro, consulta la pagina relativa alle release di Apache Avro. Per scaricare direttamente gli strumenti Apache Avro, accedi all'apposita pagina del repository Maven Apache Avro.

Dopo aver ottenuto lo schema, utilizzare un'istruzione CREATE TABLE per creare una tabella Athena basata sui dati Avro sottostanti archiviati in Amazon S3. Per specificare Avro SerDe nella dichiarazione CREATE TABLE, utilizzare ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'. Specificare lo schema utilizzando la proposizione WITH SERDEPROPERTIES, come illustrato nell'esempio seguente.

Nota

Sostituire myregion in s3://athena-examples-myregion/path/to/data/ con l'identificativo della Regione in cui si esegue Athena, ad esempio 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/';

Esegui l'istruzione MSCK REPAIR TABLE sulla tabella per aggiornare i metadati della partizione.

MSCK REPAIR TABLE flights_avro_example;

Esegui una query per trovare le prime 10 città in base al numero totale di partenze.

SELECT origin, count(*) AS total_departures FROM flights_avro_example WHERE year >= '2000' GROUP BY origin ORDER BY total_departures DESC LIMIT 10;
Nota

I dati della tabella di volo provengono da Flights e sono forniti dal Dipartimento dei Trasporti degli Stati Uniti, Ufficio delle statistiche sui trasporti. Desaturati dall'originale.