

# Amazon Athena를 사용하여 Amazon S3 로그 파일에서 인터넷 측정값 쿼리
<a name="CloudWatch-IM-view-cw-tools.S3_athena"></a>

Amazon Athena를 사용하여 Internet Monitor가 Amazon S3 버킷에 게시하는 인터넷 측정값을 쿼리하고 확인할 수 있습니다. Internet Monitor에는 애플리케이션의 인터넷 측정값을 모니터하는 도시-네트워크(클라이언트 위치 및 ASN, 일반적으로 인터넷 서비스 제공업체(ISP))의 인터넷 대상 트래픽에 대한 S3 버킷에 게시하는 옵션이 있습니다. S3에 대한 측정값 게시 여부와 관계없이 Internet Monitor는 각 모니터의 상위 500개(트래픽 볼륨 기준) 도시-네트워크에 대해 5분마다 인터넷 측정값을 CloudWatch 로그에 자동으로 게시합니다.

이 장에서는 S3 로그 파일에 있는 인터넷 측정값에 대해 Athena에서 테이블을 만드는 방법에 대한 단계와 측정값의 다양한 보기를 볼 수 있는 [예 쿼리](#CloudWatch-IM-view-cw-tools.S3_athena.athena-sample-queries)를 제공합니다. 예를 들어 지연 시간 영향에 따라 영향을 받은 상위 10개 도시-네트워크에 대해 쿼리할 수 있습니다.

## Amazon Athena를 사용하여 Internet Monitor에서 인터넷 측정값 테이블 생성
<a name="CloudWatch-IM-view-cw-tools.S3_athena.athena-queries"></a>

Internet Monitor S3 로그 파일과 함께 Athena를 사용하려면 먼저 인터넷 측정값에 대한 테이블을 생성하세요.

이 절차의 단계에 따라 S3 로그 파일을 기반으로 Athena에서 테이블을 생성합니다. 그러면 테이블에서 [다음 예 인터넷 측정값 쿼리](#CloudWatch-IM-view-cw-tools.S3_athena.athena-sample-queries)와 같은 Athena 쿼리를 실행하여 측정에 대한 정보를 얻을 수 있습니다.

**Athena 테이블을 만들려면**

1. [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/)에서 Athena 콘솔을 엽니다.

1. Athena 쿼리 편집기에서 쿼리 문을 입력하여 Internet Monitor 인터넷 측정값이 포함된 테이블을 생성합니다. 위치 파라미터의 값을 Internet Monitor 인터넷 측정값이 저장되는 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/'
```

## Internet Monitor에서 인터넷 측정값에 사용할 샘플 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/)를 참조하세요.