

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

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

下列 CREATE TABLE 陳述式範例會從指定 CloudFront 分佈到現在，對單一 AWS 區域的 Parquet 格式 CloudFront 日誌自動使用分割區投影。成功執行查詢之後，您可以查詢資料表。

```
CREATE EXTERNAL TABLE `{{cloudfront_logs_parquet_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.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' 
STORED AS INPUTFORMAT 
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat' 
OUTPUTFORMAT 
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
's3://{{amzn-s3-demo-bucket}}/AWSLogs/{{AWS_ACCOUNT_ID}}/CloudFront/'
TBLPROPERTIES (
'projection.distributionid.type'='enum',
'projection.distributionid.values'='E3OK0LPUNWWO3',
'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'='2019,2025',
'projection.year.type'='integer',
'projection.hour.range'='01,12',
'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-parquet.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` 的適當組態對於確保正確的資料儲存和擷取至關重要。