Erstellen der Tabelle für CloudTrail-Protokolle in Athena mithilfe der Partitionsprojektion - Amazon Athena

Erstellen der Tabelle für CloudTrail-Protokolle in Athena mithilfe der Partitionsprojektion

Da CloudTrail-Protokolle über eine bekannte Struktur verfügen, deren Partitionsschema Sie im Voraus angeben können, können Sie die Abfragelaufzeit reduzieren und die Partitionsverwaltung mithilfe der Athena-Partitionsprojektion automatisieren. Partitionsprojektion fügt automatisch neue Partitionen hinzu, wenn neue Daten hinzugefügt werden. Dadurch entfällt die Notwendigkeit, Partitionen manuell mithilfe von hinzuzufügen ALTER TABLE ADD PARTITION.

Die folgende CREATE TABLE-Beispielanweisung verwendet automatisch die Partitionsprojektion in CloudTrail-Protokollen von einem angegebenen Datum bis zur Gegenwart für eine einzelne AWS-Region. Ersetzen Sie in den LOCATION und storage.location.template-Klauseln den Bucket, Konto-id und aws-region-Platzhalter durch entsprechend identische Werte. Ersetzen Sie für projection.timestamp.range den 01.01.2020 durch das Startdatum, das Sie verwenden möchten. Nach dem erfolgreichen Ausführen der Abfrage können Sie die Tabelle abfragen. Sie müssen ALTER TABLE ADD PARTITION nicht ausführen, um die Partitionen zu laden.

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}')

Weitere Informationen zur Partitionsprojektion finden Sie unter Partitionsprojektion mit Amazon Athena verwenden.