

# JSON 사용 파티션 프로젝션을 통해 Athena에서 CloudFront 로그용 테이블 생성
<a name="create-cloudfront-table-partition-json"></a>

Athena 파티션 프로젝션 기능을 사용하여 쿼리 런타임을 줄이고 파티션 관리를 자동화할 수 있습니다. 새 데이터가 추가되면 파티션 프로젝션은 자동으로 새 파티션을 추가합니다. 따라서 `ALTER TABLE ADD PARTITION`을 사용해 파티션을 수동으로 추가할 필요가 없습니다.

다음 CREATE TABLE 문 예제에서는 하나의 AWS 리전에 대해 지정된 날짜부터 현재까지의 CloudFront 로그에 파티션 프로젝션을 자동으로 사용합니다. 쿼리가 성공적으로 실행되면 테이블을 쿼리할 수 있습니다.

```
CREATE EXTERNAL TABLE `cloudfront_logs_pp`(
  `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)
  PARTITIONED BY(
         distributionid string,
         year int,
         month int,
         day int,
         hour int )
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/AWSLogs/AWS_ACCOUNT_ID/CloudFront/'
TBLPROPERTIES (
  'projection.distributionid.type'='enum',
  'projection.distributionid.values'='E2Oxxxxxxxxxxx',
  'projection.day.range'='01,31', 
  'projection.day.type'='integer', 
  'projection.day.digits'='2', 
  'projection.enabled'='true', 
  'projection.month.range'='01,12', 
  'projection.month.type'='integer', 
  'projection.month.digits'='2', 
  'projection.year.range'='2025,2026', 
  'projection.year.type'='integer', 
  'projection.hour.range'='00,23',
  'projection.hour.type'='integer',
  'projection.hour.digits'='2',
  'storage.location.template'='s3://amzn-s3-demo-bucket/AWSLogs/AWS_ACCOUNT_ID/CloudFront/${distributionid}/${year}/${month}/${day}/${hour}/')
```

다음은 이전 예제에서 사용된 속성에 대한 몇 가지 고려 사항입니다.
+ **테이블 이름** - 테이블 이름 *`cloudfront_logs_pp`*을 바꿀 수 있습니다. 원하는 이름으로 변경할 수 있습니다.
+ **위치** - `s3://amzn-s3-demo-bucket/AWSLogs/AWS_ACCOUNT_ID/`가 Amazon S3 버킷을 가리키도록 수정합니다.
+ **배포 ID** - `projection.distributionid.values`의 경우 여러 배포 ID를 쉼표로 구분하여 지정할 수 있습니다. 예: *<distributionID1>*, *<distributionID2>*.
+ **연도 범위** - `projection.year.range`에서 데이터를 기반으로 연도 범위를 정의할 수 있습니다. 예를 들어 *2025년*, *2026년*과 같은 기간으로 조정할 수 있습니다.
**참고**  
미래 날짜(예: 2025\$12040)와 같은 빈 파티션을 포함하면 쿼리 성능에 영향을 미칠 수 있습니다. 그러나 파티션 프로젝션은 향후 날짜를 효과적으로 처리하도록 설계되었습니다. 최적의 성능을 유지하려면 파티션을 신중하게 관리하고 가능한 경우 과도한 빈 파티션을 피해야 합니다.
+ **스토리지 위치 템플릿** - 다음 CloudFront 파티셔닝 구조 및 S3 경로를 기반으로 `storage.location.template`를 올바르게 업데이트해야 합니다.  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/athena/latest/ug/create-cloudfront-table-partition-json.html)

  CloudFront 파티셔닝 구조와 S3 구조가 필요한 패턴과 일치하는지 확인한 후 `storage.location.template`을 다음과 같이 업데이트합니다.

  ```
  'storage.location.template'='s3://amzn-s3-demo-bucket/AWSLogs/account_id/CloudFront/${distributionid}/folder2/${year}/${month}/${day}/${hour}/folder3/'
  ```
**참고**  
올바른 데이터 스토리지 및 검색을 보장하려면 `storage.location.template`을 적절히 구성하는 것이 중요합니다.