Como os logs do CloudTrail têm uma estrutura conhecida com um esquema de partição que você pode especificar antecipadamente, é possível reduzir o runtime das consultas e automatizar o gerenciamento de partições usando o atributo de projeção de partições do Athena. A projeção de partições adiciona automaticamente novas partições à medida que os dados são adicionados. Isso elimina a necessidade de adicionar manualmente as partições usando ALTER TABLE ADD PARTITION
.
A instrução de exemplo CREATE TABLE
a seguir usa automaticamente a projeção de partições com base nos logs do CloudTrail a partir de uma data especificada até o dia de hoje para uma única Região da AWS. Nas cláusulas LOCATION
e storage.location.template
, substitua os espaços reservados bucket
, account-id
e aws-region
pelos valores idênticos correspondentes. Em projection.timestamp.range
, substitua 2020
/01
/01
pela data de início desejada. Depois que você executar a consulta com êxito, poderá consultar a tabela. Você não precisa executar ALTER TABLE ADD PARTITION
para carregar as partições.
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}')
Para obter mais informações sobre projeção de partições, consulte Usar projeção de partições com o Amazon Athena.