

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

# 使用 Amazon Athena 查詢 Amazon S3 日誌檔案中的網際網路度量結果
<a name="CloudWatch-IM-view-cw-tools.S3_athena"></a>

您可以使用 Amazon Athena，查詢和檢視 Internet Monitor 發布至 Amazon S3 儲存貯體的網際網路度量結果。網路監視器中有個選項，可讓其針對受監控之城市網路 (用戶端位置和 ASN，通常為網際網路服務供應商 (ISP)) 的面向網際網路流量，將應用程式的網際網路度量發布至 S3 儲存貯體。無論您是否選擇將度量發布至 S3，網路監視器每五分鐘會針對各監視器的前 500 個 (按流量計算) 城市網路，自動將網際網路度量發布至 CloudWatch Logs。

本章節包含針對位於 S3 日誌檔案中的網際網路度量在 Athena 中建立資料表的方法步驟，以及提供[範例查詢](#CloudWatch-IM-view-cw-tools.S3_athena.athena-sample-queries)以查看度量的不同檢視。例如，您可以透過延遲影響查詢前 10 個受影響的城市網路。

## 使用 Amazon Athena 在網路監視器中建立網際網路度量的資料表
<a name="CloudWatch-IM-view-cw-tools.S3_athena.athena-queries"></a>

若要搭配網路監視器 S3 日誌檔案開始使用 Athena，請先建立網際網路度量的資料表。

按照此程序中的步驟，以根據 S3 日誌檔案在 Athena 中建立資料表。接著，您可以在資料表上執行 Athena 查詢 (例如，[這些範例網際網路測量查詢](#CloudWatch-IM-view-cw-tools.S3_athena.athena-sample-queries))，以取得度量的相關資訊。

**建立 Athena 資料表**

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

1. 在 Athena 查詢編輯器中，輸入查詢陳述式，以產生具有網路監視器網際網路度量的資料表。將 LOCATION 參數值取代為 S3 儲存貯體的位置 (存放網路監視器網際網路度量的位置)。

   ```
   CREATE EXTERNAL TABLE internet_measurements (
       version INT,
       timestamp INT,
       clientlocation STRING,
       servicelocation STRING,
       percentageoftotaltraffic DOUBLE,
       bytesin INT,
       bytesout INT,
       clientconnectioncount INT,
       internethealth STRING,
       trafficinsights STRING
   )
   PARTITIONED BY (year STRING, month STRING, day STRING)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   LOCATION
   's3://{{amzn-s3-demo-bucket}}/{{bucket_prefix}}/AWSLogs/{{account_id}}/internetmonitor/{{AWS_Region}}/'
   TBLPROPERTIES ('skip.header.line.count' = '1');
   ```

1. 輸入陳述式以建立讀取資料的分割區。例如，下列查詢會針對指定的日期和位置建立單一分割區：

   ```
   ALTER TABLE internet_measurements
   ADD PARTITION (year = 'YYYY', month = 'MM', day = 'dd')
   LOCATION
   's3://{{amzn-s3-demo-bucket}}/{{bucket_prefix}}/AWSLogs/{{account_id}}/internetmonitor/{{AWS_Region}}/{{YYYY/MM/DD}}';
   ```

1. 選擇**執行**。

**網際網路度量的範例 Athena 陳述式**

下列為產生資料表的陳述式範例：

```
CREATE EXTERNAL TABLE internet_measurements (
    version INT,
    timestamp INT,
    clientlocation STRING,
    servicelocation STRING,
    percentageoftotaltraffic DOUBLE,
    bytesin INT,
    bytesout INT,
    clientconnectioncount INT,
    internethealth STRING,
    trafficinsights STRING
)
PARTITIONED BY (year STRING, month STRING, day STRING)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION 's3://internet-measurements/TestMonitor/AWSLogs/1111222233332/internetmonitor/us-east-2/'
TBLPROPERTIES ('skip.header.line.count' = '1');
```

下列為建立讀取資料之分割區的陳述式範例：

```
ALTER TABLE internet_measurements
ADD PARTITION (year = '2023', month = '04', day = '07')
LOCATION 's3://internet-measurements/TestMonitor/AWSLogs/1111222233332/internetmonitor/us-east-2/2023/04/07/'
```

## 可搭配使用網路監視器之網際網路度量的範例 Amazon Athena 查詢
<a name="CloudWatch-IM-view-cw-tools.S3_athena.athena-sample-queries"></a>

本節包含可與 Amazon Athena 搭配使用的範例查詢，可取得有關發布至 Amazon S3 之應用程式網際網路度量的資訊。

**查詢前 10 個受影響的 (依總流量百分比) 用戶端位置和 ASN**

執行此 Athena 查詢可傳回前 10 個受影響的 (依總流量百分比) 城市網路[即用戶端位置和 ASN (通常為網際網路服務供應商)]。

```
SELECT json_extract_scalar(clientLocation, '$.city') as city,
    json_extract_scalar(clientLocation, '$.networkname') as networkName,
    sum(percentageoftotaltraffic) as percentageoftotaltraffic
FROM internet_measurements
GROUP BY json_extract_scalar(clientLocation, '$.city'),
    json_extract_scalar(clientLocation, '$.networkname')
ORDER BY percentageoftotaltraffic desc
limit 10
```

**查詢前 10 個受影響的 (依可用性) 用戶端位置和 ASN**

執行此 Athena 查詢可傳回前 10 個受影響的 (依總流量百分比) 城市網路[即用戶端位置和 ASN (通常為網際網路服務供應商)]。

```
SELECT json_extract_scalar(clientLocation, '$.city') as city,
    json_extract_scalar(clientLocation, '$.networkname') as networkName,
    sum(
        cast(
            json_extract_scalar(
                internetHealth,
                '$.availability.percentageoftotaltrafficimpacted'
            )
        as double ) 
    ) as percentageOfTotalTrafficImpacted
FROM internet_measurements
GROUP BY json_extract_scalar(clientLocation, '$.city'),
    json_extract_scalar(clientLocation, '$.networkname')
ORDER BY percentageOfTotalTrafficImpacted desc
limit 10
```

**查詢前 10 個受影響的 (依延遲情況) 用戶端位置和 ASN**

執行此 Athena 查詢可傳回前 10 個受影響的 (依延遲影響) 城市網路[即用戶端位置和 ASN (通常為網際網路服務供應商)]。

```
SELECT json_extract_scalar(clientLocation, '$.city') as city,
    json_extract_scalar(clientLocation, '$.networkname') as networkName,
    sum(
        cast(
            json_extract_scalar(
                internetHealth,
                '$.performance.percentageoftotaltrafficimpacted'
            )
        as double ) 
    ) as percentageOfTotalTrafficImpacted
FROM internet_measurements
GROUP BY json_extract_scalar(clientLocation, '$.city'),
    json_extract_scalar(clientLocation, '$.networkname')
ORDER BY percentageOfTotalTrafficImpacted desc
limit 10
```

**查詢用戶端位置和 ASN 的流量重點**

執行此 Athena 查詢可傳回流量重點資訊，(包含可用性分數、效能分數，以及城市網路的第一個位元組時間)，即用戶端位置和 ASN (通常為網際網路服務供應商)。

```
SELECT json_extract_scalar(clientLocation, '$.city') as city,
    json_extract_scalar(clientLocation, '$.subdivision') as subdivision,
    json_extract_scalar(clientLocation, '$.country') as country,
    avg(cast(json_extract_scalar(internetHealth, '$.availability.experiencescore') as double)) as availabilityScore,
    avg(cast(json_extract_scalar(internetHealth, '$.performance.experiencescore') as double)) performanceScore,
    avg(cast(json_extract_scalar(trafficinsights, '$.timetofirstbyte.currentexperience.value') as double)) as averageTTFB,
    sum(bytesIn) as bytesIn,
    sum(bytesOut) as bytesOut,
    sum(bytesIn + bytesOut) as totalBytes
FROM internet_measurements
where json_extract_scalar(clientLocation, '$.city') != 'N/A'
GROUP BY 
json_extract_scalar(clientLocation, '$.city'),
    json_extract_scalar(clientLocation, '$.subdivision'),
    json_extract_scalar(clientLocation, '$.country')
ORDER BY totalBytes desc
limit 100
```

如需有關使用 Athena 的詳細資訊，請參閱《Amazon Athena 使用者指南》[https://docs.aws.amazon.com/athena/latest/ug/](https://docs.aws.amazon.com/athena/latest/ug/)。