Crea la tabella per i log CloudTrail in Athena utilizzando la proiezione delle partizioni
Poiché i log CloudTrail dispongono di una struttura nota il cui schema di partizione può essere specificato in anticipo, è possibile ridurre il tempo di esecuzione delle query e automatizzare la gestione delle partizioni utilizzando la funzione di proiezione delle partizioni Athena. La proiezione delle partizioni aggiunge automaticamente nuove partizioni man mano che vengono aggiunti nuovi dati. Ciò elimina la necessità di aggiungere manualmente le partizioni utilizzando ALTER TABLE ADD PARTITION.
L'esempio di istruzione seguente CREATE TABLE utilizza automaticamente la proiezione delle partizioni nei log CloudTrail da una data specificata fino alla presente per una singola Regione AWS. Nelle clausole LOCATION e storage.location.template, sostituire i segnaposto bucket, account-id, e aws-region con valori corrispondenti identici. Per projection.timestamp.range, sostituire 2020/01/01 con la data di inizio che si desidera usare. Dopo aver eseguito la query con esito positivo, è possibile eseguire query sulla tabella. Non è necessario eseguire ALTER TABLE ADD PARTITION per caricare le partizioni.
CREATE EXTERNAL TABLE cloudtrail_logs_pp( eventversion STRING, useridentity STRUCT< type: STRING, principalid: STRING, arn: STRING, accountid: STRING, invokedby: STRING, accesskeyid: STRING, username: STRING, onbehalfof: STRUCT< userid: STRING, identitystorearn: STRING>, sessioncontext: STRUCT< attributes: STRUCT< mfaauthenticated: STRING, creationdate: STRING>, sessionissuer: STRUCT< type: STRING, principalid: STRING, arn: STRING, accountid: STRING, username: STRING>, ec2roledelivery:string, webidfederationdata: STRUCT< federatedprovider: STRING, attributes: map<string,string>> > >, eventtime STRING, eventsource STRING, eventname STRING, awsregion STRING, sourceipaddress STRING, useragent STRING, errorcode STRING, errormessage STRING, requestparameters STRING, responseelements STRING, additionaleventdata STRING, requestid STRING, eventid STRING, readonly STRING, resources ARRAY<STRUCT< arn: STRING, accountid: STRING, type: STRING>>, eventtype STRING, apiversion STRING, recipientaccountid STRING, serviceeventdetails STRING, sharedeventid STRING, vpcendpointid STRING, vpcendpointaccountid STRING, eventcategory STRING, addendum STRUCT< reason:STRING, updatedfields:STRING, originalrequestid:STRING, originaleventid:STRING>, sessioncredentialfromconsole STRING, edgedevicedetails STRING, tlsdetails STRUCT< tlsversion:STRING, ciphersuite:STRING, clientprovidedhostheader:STRING> ) PARTITIONED BY ( `timestamp` string) ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/account-id/CloudTrail/aws-region' TBLPROPERTIES ( 'projection.enabled'='true', 'projection.timestamp.format'='yyyy/MM/dd', 'projection.timestamp.interval'='1', 'projection.timestamp.interval.unit'='DAYS', 'projection.timestamp.range'='2020/01/01,NOW', 'projection.timestamp.type'='date', 'storage.location.template'='s3://amzn-s3-demo-bucket/AWSLogs/account-id/CloudTrail/aws-region/${timestamp}')
Per maggiori informazioni sulla proiezione delle partizioni, consulta Proiezione delle partizioni con Amazon Athena.