

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

# 查詢存放在 Amazon S3 中的 Apache 日誌
<a name="querying-apache-logs"></a>

您可以使用 Amazon Athena 查詢存放在 Amazon S3 帳戶中的 [Apache HTTP Server 日誌檔案](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 for Apache 日誌中建立資料表
<a name="querying-apache-logs-creating-a-table-in-athena"></a>

查詢 Amazon S3 中存放的 Apache 日誌之前，必須為 Athena 建立資料表結構描述才能讀取日誌資料。若要建立 Apache 記錄的 Athena 資料表，您可以使用 [Grok SerDe](grok-serde.md)。如需有關使用 Grok SerDe 的詳細資訊，請參閱《AWS Glue 開發人員指南》**中的[撰寫 Grok 自訂分類器](https://docs.aws.amazon.com/glue/latest/dg/custom-classifier.html#custom-classifier-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` 資料表選取請求接收的時間、用戶端請求的文字，以及伺服器狀態碼。`WHERE` 子句會篩選 HTTP 狀態碼 `404` (找不到頁面)。  

```
SELECT request_received_time, client_request, server_status
FROM apache_logs
WHERE server_status = '404'
```
下圖顯示 Athena 查詢編輯器中查詢的結果。  

![\[從 Athena 為 HTTP 404 項目查詢 Apache 日誌。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/querying-apache-logs-1.png)


**Example – 篩選成功的請求**  
下列查詢範例會選取使用者 ID、請求接收時間、用戶端請求的文字，以及 `apache_logs` 資料表中的伺服器狀態碼。HTTP 狀態碼 `200` 的 `WHERE` 子句篩選條件 (成功)。  

```
SELECT user_id, request_received_time, client_request, server_status
FROM apache_logs
WHERE server_status = '200'
```
下圖顯示 Athena 查詢編輯器中查詢的結果。  

![\[從 Athena 為 HTTP 200 項目查詢 Apache 日誌。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/querying-apache-logs-2.png)


**Example – 依時間戳記篩選**  
下列範例會查詢要求接收時間大於指定時間戳記的記錄。  

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