

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Avro SerDe
<a name="avro-serde"></a>

Usa Avro SerDe per creare tabelle Athena dai dati Avro.

## Nome della libreria di serializzazione
<a name="avro-serde-library-name"></a>

Il nome della libreria di serializzazione per Avro è. SerDe `org.apache.hadoop.hive.serde2.avro.AvroSerDe` Per informazioni tecniche, consulta la documentazione [AvroSerDe](https://cwiki.apache.org/confluence/display/Hive/AvroSerDe)di Apache. 

## Usa l'Avro SerDe
<a name="avro-serde-using"></a>

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](http://avro.apache.org/releases.html#Download). Per scaricare direttamente gli strumenti Apache Avro, accedi all'apposita pagina del [repository Maven Apache Avro](https://mvnrepository.com/artifact/org.apache.avro/avro-tools).

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 l'Avro SerDe nella tua `CREATE TABLE` dichiarazione, usa. `ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'` Specificare lo schema utilizzando la proposizione `WITH SERDEPROPERTIES`, come illustrato nell’esempio seguente.

**Nota**  
*myregion*Sostituiscilo `s3://athena-examples-myregion/path/to/data/` con l'identificatore della regione in cui esegui 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](http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236&amp;DB_Short_Name=On-Time) e sono forniti dal Dipartimento dei Trasporti degli Stati Uniti, [Ufficio delle statistiche sui trasporti](http://www.transtats.bts.gov/). Desaturati dall'originale.