

# 使用分区投影在 Athena 中为使用 Parquet 的 CloudFront 日志创建表
<a name="create-cloudfront-table-partition-parquet"></a>

以下示例 CREATE TABLE 语句将为单个 Amazon Web Services 区域中截至当前时间的，来自指定 CloudFront 分配的 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。例如，{{<distributionID1>}}, {{<distributionID2>}}。
+ **年份范围**–在 `projection.year.range` 中，您可以根据自己的数据设置年份范围。您可以将其调整为任何期间，例如 *2025*、*2026*。
**注意**  
如果包含空分区，例如针对未来日期的分区（例如：2025-2040），则可能会影响查询性能。但是，分区投影旨在有效处理未来日期。为保持最佳性能，请务必要谨慎管理分区，尽可能避免过多空分区。
+ **存储位置模板** – 您必须确保根据以下 CloudFront 分区结构和 S3 路径正确更新 `storage.location.template`。  
****    
[See the AWS documentation website for more details](http://docs.amazonaws.cn/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` 对于确保正确的数据存储和检索至关重要。