

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立 ALB 存取日誌的資料表
<a name="create-alb-access-logs-table"></a>

1. 複製下列 `CREATE TABLE` 陳述式，並將其貼到 Athena 主控台查詢編輯器，然後針對自己的日誌項目要求視需要進行修改。如需有關 Athena 主控台入門的資訊，請參閱[開始使用](getting-started.md)。將 `LOCATION` 子句中的路徑取代為您的 Amazon S3 存取日誌資料夾位置。如需有關存取日誌檔案位置的詳細資訊，請參閱《Application Load Balancer 使用者指南**》中的[存取日誌檔案](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-log-file-format)。

   如需有關每個日誌檔案欄位的資訊，請參閱《Application Load Balancer 使用者指南**》中的[存取日誌項目](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-log-entry-format)。
**注意**  
下列範例 `CREATE TABLE` 陳述式包含最近新增的 `classification`、`classification_reason` 和 `conn_trace_id` (「可追溯性 ID」或 TID) 資料欄。若要為不含這些項目的 Application Load Balancer 存取日誌建立資料表，請從 `CREATE TABLE` 陳述式移除相應的資料欄，然後據此修改規則表達式。

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS alb_access_logs (
               type string,
               time string,
               elb string,
               client_ip string,
               client_port int,
               target_ip string,
               target_port int,
               request_processing_time double,
               target_processing_time double,
               response_processing_time double,
               elb_status_code int,
               target_status_code string,
               received_bytes bigint,
               sent_bytes bigint,
               request_verb string,
               request_url string,
               request_proto string,
               user_agent string,
               ssl_cipher string,
               ssl_protocol string,
               target_group_arn string,
               trace_id string,
               domain_name string,
               chosen_cert_arn string,
               matched_rule_priority string,
               request_creation_time string,
               actions_executed string,
               redirect_url string,
               lambda_error_reason string,
               target_port_list string,
               target_status_code_list string,
               classification string,
               classification_reason string,
               conn_trace_id string
               )
               ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
               WITH SERDEPROPERTIES (
               'serialization.format' = '1',
               'input.regex' = 
           '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) (.*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-_]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\\s]+?)\" \"([^\\s]+)\" \"([^ ]*)\" \"([^ ]*)\" ?([^ ]*)? ?( .*)?'
               )
               LOCATION 's3://amzn-s3-demo-bucket/{{access-log-folder-path}}/'
   ```
**注意**  
我們建議在 `input.regex` 參數的結尾一律保留模式 {{`?( .*)?`}}，以便在新增新的 ALB 日誌欄位時，處理未來的日誌項目。

1. 在 Athena 主控台中執行查詢。查詢完成之後，Athena 會註冊 `alb_access_logs` 資料表，讓其中的資料可供您發出查詢。