

# 查询存储在 Amazon S3 中的 Apache 日志
<a name="querying-apache-logs"></a>

您可以使用 Amazon Athena 以查询存储在 Amazon S3 账户中的 [Apache HTTP 服务器日志文件](https://httpd.apache.org/docs/2.4/logs.html)。本主题介绍如何创建表架构以查询常见日志格式的 Apache [访问日志](https://httpd.apache.org/docs/2.4/logs.html#accesslog)文件。

常见日志格式中的字段包括客户端 IP 地址、客户端 ID、用户 ID、接收请求的时间戳、客户端请求的文本、服务器状态代码以及返回给客户端的对象的大小。

以下示例数据显示了 Apache 的常见日志格式。

```
198.51.100.7 - Li [10/Oct/2019:13:55:36 -0700] "GET /logo.gif HTTP/1.0" 200 232
198.51.100.14 - Jorge [24/Nov/2019:10:49:52 -0700] "GET /index.html HTTP/1.1" 200 2165
198.51.100.22 - Mateo [27/Dec/2019:11:38:12 -0700] "GET /about.html HTTP/1.1" 200 1287
198.51.100.9 - Nikki [11/Jan/2020:11:40:11 -0700] "GET /image.png HTTP/1.1" 404 230
198.51.100.2 - Ana [15/Feb/2019:10:12:22 -0700] "GET /favicon.ico HTTP/1.1" 404 30
198.51.100.13 - Saanvi [14/Mar/2019:11:40:33 -0700] "GET /intro.html HTTP/1.1" 200 1608
198.51.100.11 - Xiulan [22/Apr/2019:10:51:34 -0700] "GET /group/index.html HTTP/1.1" 200 1344
```

## 在 Athena 中为 Apache 日志创建表
<a name="querying-apache-logs-creating-a-table-in-athena"></a>

必须先为 Athena 创建一个表架构，以便它能够读取日志数据，然后才能查询存储在 Amazon S3 中的 Apache 日志。若要为 Apache 日志创建 Athena 表，可以使用 [Grok SerDe](grok-serde.md)。有关使用 Grok SerDe 的更多信息，请参阅《AWS Glue 开发人员指南[https://docs.aws.amazon.com/glue/latest/dg/custom-classifier.html#custom-classifier-grok](https://docs.aws.amazon.com/glue/latest/dg/custom-classifier.html#custom-classifier-grok)》中的 *编写 Grok 自定义分类器*。

**要在 Athena 中为 Apache Web 服务器日志创建表**

1. 从 [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 打开 Athena 控制台。

1. 将以下 DDL 语句粘贴到 Athena 查询编辑器中。修改 `LOCATION 's3://amzn-s3-demo-bucket/{{apache-log-folder}}/'` 中的值以指向 Amazon S3 中的 Apache 日志。

   ```
   CREATE EXTERNAL TABLE apache_logs (
     client_ip string,
     client_id string,
     user_id string,
     request_received_time string,
     client_request string,
     server_status string,
     returned_obj_size string
     )
   ROW FORMAT SERDE
      'com.amazonaws.glue.serde.GrokSerDe'
   WITH SERDEPROPERTIES (
      'input.format'='^%{IPV4:client_ip} %{DATA:client_id} %{USERNAME:user_id} %{GREEDYDATA:request_received_time} %{QUOTEDSTRING:client_request} %{DATA:server_status} %{DATA: returned_obj_size}$'
      )
   STORED AS INPUTFORMAT
      'org.apache.hadoop.mapred.TextInputFormat'
   OUTPUTFORMAT
      'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
   LOCATION
      's3://amzn-s3-demo-bucket/{{apache-log-folder}}/';
   ```

1. 在 Athena 控制台中运行查询以注册 `apache_logs` 表。查询完成后，调查结果准备就绪，可供您从 Athena 查询。

### 示例查询
<a name="querying-apache-logs-example-select-queries"></a>

**Example – 筛选 404 错误**  
以下的示例查询从 `apache_logs` 表中选择了请求接收时间、客户端请求的文本以及服务器状态代码。HTTP 状态代码 `404`（未找到页面）的 `WHERE` 子句筛选条件。  

```
SELECT request_received_time, client_request, server_status
FROM apache_logs
WHERE server_status = '404'
```
下图显示了 Athena 查询编辑器中的查询结果。  

![从 Athena 查询 Apache 日志的 HTTP 404 条目。](http://docs.aws.amazon.com/zh_cn/athena/latest/ug/images/querying-apache-logs-1.png)


**Example – 筛选成功的请求**  
以下的示例查询从 `apache_logs` 表中选择了用户 ID、请求接收时间、客户端请求的文本以及服务器状态代码。HTTP 状态代码 `200`（成功）的子句筛选条件 `WHERE`。  

```
SELECT user_id, request_received_time, client_request, server_status
FROM apache_logs
WHERE server_status = '200'
```
下图显示了 Athena 查询编辑器中的查询结果。  

![从 Athena 查询 Apache 日志的 HTTP 200 条目。](http://docs.aws.amazon.com/zh_cn/athena/latest/ug/images/querying-apache-logs-2.png)


**Example – 按时间戳筛选**  
如下示例将查询请求接收时间大于指定时间戳的记录。  

```
SELECT * FROM apache_logs WHERE request_received_time > 10/Oct/2023:00:00:00
```