

# Athena で JSON でのパーティショニング射影を使用して CloudFront ログ用のテーブルを作成する
<a name="create-cloudfront-table-partition-json"></a>

Athena のパーティション射影機能を使用すると、クエリランタイムを短縮し、パーティション管理を自動化できます。新しいデータが追加されると、パーティション射影は新しいパーティションを自動で追加します。このため、`ALTER TABLE ADD PARTITION` を使用してパーティションを手動で追加する必要がなくなります。

次の CREATE TABLE ステートメント例は、指定した CloudFront 配信から現在までの単一の AWS リージョンの CloudTrail ログでパーティション射影を自動的に使用します。クエリが正常に実行されると、テーブルをクエリできます。

```
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`* は置き換えることができます。任意の名前に変更できます。
+ **場所** – Amazon S3 バケットを指すように `s3://amzn-s3-demo-bucket/AWSLogs/AWS_ACCOUNT_ID/` を変更します。
+ **配信 ID** – `projection.distributionid.values` では、カンマで区切って複数の配信 ID を指定できます。例えば、*<distributionID1>*, *<distributionID2>* のように指定します。
+ **年の範囲** – `projection.year.range` でデータに基づいて年の範囲を定義できます。例えば、**2025 や **2026 などの任意の期間に調整できます。
**注記**  
将来の日付 (例: 2025-2040) など、空のパーティションを含めると、クエリのパフォーマンスに影響が生じる可能性があります。ただし、パーティション射影は将来の日付を効果的に処理するように設計されています。最適なパフォーマンスを維持するには、パーティションを慎重に管理し、可能な場合は空のパーティションの過度な使用を避けてください。
+ **保存場所テンプレート** – 次の CloudFront パーティション構造と S3 パスに基づいて `storage.location.template` を正しく更新する必要があります。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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` を適切に設定することが重要です。