

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

# 使用分割區投影 (JSON) 在 Athena 中建立 CloudFront 日誌的資料表
<a name="create-cloudfront-table-partition-json"></a>

您可以使用 Athena 分割區投影功能減少查詢執行時期並自動化分割區管理。分割區投影會在新增資料時自動新增分割區。因此您無需使用 `ALTER TABLE ADD PARTITION` 手動新增分割區。

下列 CREATE TABLE 陳述式範例會從指定 CloudFront 分佈到現在，對單一 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，則可以指定多個分佈 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/zh_tw/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` 的適當組態對於確保正確的資料儲存和擷取至關重要。