

# 为 ALB 访问日志创建表
<a name="create-alb-access-logs-table"></a>

1. 将以下 `CREATE TABLE` 语句复制并粘贴到 Athena 控制台的查询编辑器中，然后根据您自己的日志条目要求进行必要的修改。有关 Amazon 控制台入门的更多信息，请参阅 [开始使用](getting-started.md)。将 `LOCATION` 子句中的路径替换为 Amazon S3 访问日志文件夹位置。有关访问日志文件位置的更多信息，请参阅《User Guide for Application Load Balancers》**中的 [Access log files](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-log-file-format)。

   有关每个日志文件字段的信息，请参阅《*应用程序负载均衡器用户指南*》中的[访问日志条目](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）列。要为不包含这些条目的应用程序负载均衡器访问日志创建表，请从 `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` 表，使其中的数据可以供您发出查询。