

# JSON을 사용한 수동 파티셔닝을 통해 Athena에서 CloudFront 로그용 테이블 생성
<a name="create-cloudfront-table-manual-json"></a>

**JSON 형식을 사용해 CloudFront 표준 로그 파일 필드의 테이블 생성**

1. 다음 예제 DDL 문을 복사하여 Athena 콘솔의 쿼리 편집기에 붙여 넣습니다. 예제 명령문은 **Amazon CloudFront 개발자 안내서의 [표준 로그 파일 필드](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/AccessLogs.html#BasicDistributionFileFormat) 섹션에 설명된 로그 파일 필드를 사용합니다. 로그를 저장하는 Amazon S3 버킷의 `LOCATION`을 수정합니다.

   이 쿼리는 다음 SerDe 속성과 함께 OpenX JSON SerDe를 사용하여 Athena에서 JSON 필드를 올바르게 읽습니다.

   ```
   CREATE EXTERNAL TABLE `cf_logs_manual_partition_json`(
     `date` string , 
     `time` string , 
     `x-edge-location` string , 
     `sc-bytes` string , 
     `c-ip` string , 
     `cs-method` string , 
     `cs(host)` string , 
     `cs-uri-stem` string , 
     `sc-status` string , 
     `cs(referer)` string , 
     `cs(user-agent)` string , 
     `cs-uri-query` string , 
     `cs(cookie)` string , 
     `x-edge-result-type` string , 
     `x-edge-request-id` string , 
     `x-host-header` string , 
     `cs-protocol` string , 
     `cs-bytes` string , 
     `time-taken` string , 
     `x-forwarded-for` string , 
     `ssl-protocol` string , 
     `ssl-cipher` string , 
     `x-edge-response-result-type` string , 
     `cs-protocol-version` string , 
     `fle-status` string , 
     `fle-encrypted-fields` string , 
     `c-port` string , 
     `time-to-first-byte` string , 
     `x-edge-detailed-result-type` string , 
     `sc-content-type` string , 
     `sc-content-len` string , 
     `sc-range-start` string , 
     `sc-range-end` string )
   ROW FORMAT SERDE 
     'org.openx.data.jsonserde.JsonSerDe' 
   WITH SERDEPROPERTIES ( 
     'paths'='c-ip,c-port,cs(Cookie),cs(Host),cs(Referer),cs(User-Agent),cs-bytes,cs-method,cs-protocol,cs-protocol-version,cs-uri-query,cs-uri-stem,date,fle-encrypted-fields,fle-status,sc-bytes,sc-content-len,sc-content-type,sc-range-end,sc-range-start,sc-status,ssl-cipher,ssl-protocol,time,time-taken,time-to-first-byte,x-edge-detailed-result-type,x-edge-location,x-edge-request-id,x-edge-response-result-type,x-edge-result-type,x-forwarded-for,x-host-header') 
   STORED AS INPUTFORMAT 
     'org.apache.hadoop.mapred.TextInputFormat' 
   OUTPUTFORMAT 
     'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
   LOCATION
     's3://amzn-s3-demo-bucket/'
   ```

1. Athena 콘솔에서 쿼리를 실행합니다. 쿼리가 완료된 후 Athena는 `cf_logs_manual_partition_json` 테이블을 등록하여 쿼리를 실행할 수 있도록 데이터를 준비합니다.

## 예제 쿼리
<a name="query-examples-cloudfront-logs-manual-json"></a>

다음 쿼리는 2025년 1월 15일까지 CloudFront가 제공한 바이트 수를 합산합니다.

```
SELECT sum(cast("sc-bytes" as BIGINT)) as sc
FROM cf_logs_manual_partition_json
WHERE "date"='2025-01-15'
```

쿼리 결과에서 중복 행(예: 중복된 빈 행)을 제거하려면 다음 예제와 같이 `SELECT DISTINCT` 문을 사용할 수 있습니다.

```
SELECT DISTINCT * FROM cf_logs_manual_partition_json
```