1、Athenaテーブルを作成する
ここで場所を指定して直接作成するか、SQLをコピーしてAthenaで作成する
今回はパーティションを追加するため、SQLをコピーして、パーティションを追加してAthenaで作成する
create_table.sql
CREATE EXTERNAL TABLE cloudtrail_logs_aws_service_log (
eventVersion STRING,
userIdentity STRUCT<
type: STRING,
principalId: STRING,
arn: STRING,
accountId: STRING,
invokedBy: STRING,
accessKeyId: STRING,
userName: STRING,
sessionContext: STRUCT<
attributes: STRUCT<
mfaAuthenticated: STRING,
creationDate: STRING>,
sessionIssuer: STRUCT<
type: STRING,
principalId: STRING,
arn: STRING,
accountId: STRING,
userName: 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,
resources ARRAY<STRUCT<
arn: STRING,
accountId: STRING,
type: STRING>>,
eventType STRING,
apiVersion STRING,
readOnly STRING,
recipientAccountId STRING,
serviceEventDetails STRING,
sharedEventID STRING,
vpcEndpointId STRING
)
COMMENT 'CloudTrail table for aws-service-log bucket'
PARTITIONED BY(region string, year string, month string) #パーティション
ROW FORMAT SERDE 'com.amazon.emr.hive.serde.CloudTrailSerde'
STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 's3://<BUCKET-NAME>/AWSLogs/<account-id>/CloudTrail/'
TBLPROPERTIES ('classification'='cloudtrail');
2、パーティションをロードする
下記のコマンドを実行し、データを全部ロードする
load_all_data.sql
MSCK REPAIR TABLE <table-name>
全部ロードしたくない場合は下記のコマンドで指定するパーティションをロードする
load_data.sql
ALTER TABLE <table-name>
ADD PARTITION (region='ap-northeast-1', year='2021', month='10')
location 's3://<BUCKET-NAME>/AWSLogs/<account-id>/CloudTrail/ap-northeast-1/2021/10/'