Erstellen einer Tabelle für CloudFront-Protokolle in Athena mithilfe manueller Partitionierung mit JSON
Um eine Tabelle für CloudFront-Standardprotokollfeldfelder im JSON-Format zu erstellen
-
Kopieren Sie die folgende beispielhafte DDL-Anweisung und fügen Sie sie in den Abfrageeditor der Athena-Konsole ein. Die Beispielanweisung verwendet die Protokolldateifelder, die im Abschnitt Standard-Protokolldateifelder des Amazon-CloudFront-Entwicklerhandbuchs dokumentiert sind. Ändern Sie
LOCATIONin den Amazon-S3-Bucket, in dem die Protokolle gespeichert werden.Diese Abfrage verwendet OpenX JSON SerDe mit den folgenden SerDe-Eigenschaften, um JSON-Felder in Athena korrekt zu lesen.
CREATE EXTERNAL TABLE `cf_logs_manual_partition_json`( `date` string , `time` string , `x-edge-location` string , `sc-bytes` string , `c-ip` string , `cs-method` string , `cs(host)` string , `cs-uri-stem` string , `sc-status` string , `cs(referer)` string , `cs(user-agent)` string , `cs-uri-query` string , `cs(cookie)` string , `x-edge-result-type` string , `x-edge-request-id` string , `x-host-header` string , `cs-protocol` string , `cs-bytes` string , `time-taken` string , `x-forwarded-for` string , `ssl-protocol` string , `ssl-cipher` string , `x-edge-response-result-type` string , `cs-protocol-version` string , `fle-status` string , `fle-encrypted-fields` string , `c-port` string , `time-to-first-byte` string , `x-edge-detailed-result-type` string , `sc-content-type` string , `sc-content-len` string , `sc-range-start` string , `sc-range-end` string ) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe' WITH SERDEPROPERTIES ( 'paths'='c-ip,c-port,cs(Cookie),cs(Host),cs(Referer),cs(User-Agent),cs-bytes,cs-method,cs-protocol,cs-protocol-version,cs-uri-query,cs-uri-stem,date,fle-encrypted-fields,fle-status,sc-bytes,sc-content-len,sc-content-type,sc-range-end,sc-range-start,sc-status,ssl-cipher,ssl-protocol,time,time-taken,time-to-first-byte,x-edge-detailed-result-type,x-edge-location,x-edge-request-id,x-edge-response-result-type,x-edge-result-type,x-forwarded-for,x-host-header') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 's3://amzn-s3-demo-bucket/' -
Führen Sie die Abfrage in der Athena-Konsole aus. Nach Beendigung der Abfrage registriert Athena die
cf_logs_manual_partition_json-Tabelle, sodass Sie die Daten zum Ausgeben von Abfragen nutzen können.
Beispielabfragen
Die folgende Abfrage addiert die Anzahl der von CloudFront für den 15. Januar 2025 bereitgestellten Bytes.
SELECT sum(cast("sc-bytes" as BIGINT)) as sc FROM cf_logs_manual_partition_json WHERE "date"='2025-01-15'
Um doppelte Zeilen (z. B. doppelte Leerzeilen) aus den Abfrageergebnissen zu entfernen, können Sie, wie im folgenden Beispiel die SELECT DISTINCT-Anweisung verwenden.
SELECT DISTINCT * FROM cf_logs_manual_partition_json