

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

# 查詢 IIS 日誌檔案格式
<a name="querying-iis-logs-iis-log-file-format"></a>

與 W3C 擴充格式不同，[IIS 日誌檔案格式](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc728311(v%3dws.10))具有固定的欄位集，並包含逗號作為分隔符號。LazySimpleSerDe 將逗號作為分隔符號，並將逗號後的空格作為下一個欄位的開頭。

以下範例會以 IIS 日誌檔案格式顯示資料範例。

```
203.0.113.15, -, 2020-02-24, 22:48:38, W3SVC2, SERVER5, 198.51.100.4, 254, 501, 488, 200, 0, GET, /index.htm, -, 
203.0.113.4, -, 2020-02-24, 22:48:39, W3SVC2, SERVER6, 198.51.100.6, 147, 411, 388, 200, 0, GET, /about.html, -, 
203.0.113.11, -, 2020-02-24, 22:48:40, W3SVC2, SERVER7, 198.51.100.18, 170, 531, 468, 200, 0, GET, /image.png, -, 
203.0.113.8, -, 2020-02-24, 22:48:41, W3SVC2, SERVER8, 198.51.100.14, 125, 711, 868, 200, 0, GET, /intro.htm, -,
```

## 在 Athena 中為 IIS 日誌檔案建立資料表
<a name="querying-iis-logs-creating-a-table-in-athena-for-iis-log-files"></a>

若要在 Amazon S3 中查詢 IIS 日誌檔案格式日誌，請先建立資料表結構描述，以便讓 Athena 可以讀取日誌資料。

**若要在 Athena 中為 IIS 日誌檔案格式日誌建立資料表**

1. 前往 [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 開啟 Athena 主控台。

1. 將下列 DDL 陳述式貼上至 Athena 主控台，並注意下列幾點：

   1. 若要指定逗號分隔符號，請使用 `FIELDS TERMINATED BY ','`。

   1. 修改 LOCATION 's3://amzn-s3-demo-bucket/*iis-log-file-folder*/' 中的值，以指向 Amazon S3 中的 IIS 日誌格式日誌檔案。

   ```
   CREATE EXTERNAL TABLE `iis_format_logs`(
   client_ip_address string,
   user_name string,
   request_date string,
   request_time string,
   service_and_instance string,
   server_name string,
   server_ip_address string,
   time_taken_millisec string,
   client_bytes_sent string,
   server_bytes_sent string,
   service_status_code string,
   windows_status_code string,
   request_type string,
   target_of_operation string,
   script_parameters string
      )
   ROW FORMAT DELIMITED
     FIELDS TERMINATED BY ','
   STORED AS INPUTFORMAT
     'org.apache.hadoop.mapred.TextInputFormat'
   OUTPUTFORMAT
     'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
   LOCATION
     's3://amzn-s3-demo-bucket/iis-log-file-folder/'
   ```

1. 在 Athena 主控台中執行查詢來註冊 `iis_format_logs` 資料表。查詢完成時，您就可以從 Athena 查詢日誌了。

## IIS 日誌格式選取查詢範例
<a name="querying-iis-logs-example-iis-log-format-select-query"></a>

下列查詢範例從資料表 `iis_format_logs` 中選取請求日期、請求時間、請求目標和以毫秒為單位的花費時間。`WHERE` 子句篩選請求類型為 `GET` 且 HTTP 狀態碼為 `200` (成功) 的案例。在查詢中，請注意 `' GET'` 和 `' 200'` 中需要有前置空格，才能使查詢成功。

```
SELECT request_date, request_time, target_of_operation, time_taken_millisec
FROM iis_format_logs
WHERE request_type = ' GET' AND service_status_code = ' 200'
```

下圖顯示範例資料的查詢結果。

![\[Athena 中存放在 Amazon S3 中 IIS 日誌檔案格式日誌檔案的查詢結果範例。\]](http://docs.aws.amazon.com/zh_tw/athena/latest/ug/images/querying-iis-logs-2.png)
