

# 为 CloudFront 实时日志创建表
<a name="create-cloudfront-table-real-time-logs"></a>

**为 CloudFront 实时日志文件字段创建表**

1. 将以下示例 DDL 语句复制并粘贴到 Athena 控制台的查询编辑器中。该示例语句使用了《Amazon CloudFront Developer Guide》**的 [Real-time logs](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/real-time-logs.html) 章节中记录的日志文件字段。修改用于存储日志的 Amazon S3 存储桶的 `LOCATION`。有关使用查询编辑器的信息，请参阅 [开始使用](getting-started.md)。

   此查询指定了 `ROW FORMAT DELIMITED` 和 `FIELDS TERMINATED BY '\t'`，表示字段由制表符分隔。对于 `ROW FORMAT DELIMITED`，Athena 默认使用 [LazySimpleSerDe](lazy-simple-serde.md)。列 `timestamp` 使用反引号 (`) 转义，因为它是 Athena 中的保留字。有关信息，请参阅[转义查询中的保留关键字](reserved-words.md)。

   以下示例包含所有可用字段。若有不需要的字段，可将相应字段注释掉或者删除掉。

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS cloudfront_real_time_logs ( 
   `timestamp` STRING,
   c_ip STRING,
   time_to_first_byte BIGINT,
   sc_status BIGINT,
   sc_bytes BIGINT,
   cs_method STRING,
   cs_protocol STRING,
   cs_host STRING,
   cs_uri_stem STRING,
   cs_bytes BIGINT,
   x_edge_location STRING,
   x_edge_request_id STRING,
   x_host_header STRING,
   time_taken BIGINT,
   cs_protocol_version STRING,
   c_ip_version STRING,
   cs_user_agent STRING,
   cs_referer STRING,
   cs_cookie STRING,
   cs_uri_query STRING,
   x_edge_response_result_type STRING,
   x_forwarded_for STRING,
   ssl_protocol STRING,
   ssl_cipher STRING,
   x_edge_result_type STRING,
   fle_encrypted_fields STRING,
   fle_status STRING,
   sc_content_type STRING,
   sc_content_len BIGINT,
   sc_range_start STRING,
   sc_range_end STRING,
   c_port BIGINT,
   x_edge_detailed_result_type STRING,
   c_country STRING,
   cs_accept_encoding STRING,
   cs_accept STRING,
   cache_behavior_path_pattern STRING,
   cs_headers STRING,
   cs_header_names STRING,
   cs_headers_count BIGINT,
   primary_distribution_id STRING,
   primary_distribution_dns_name STRING,
   origin_fbl STRING,
   origin_lbl STRING,
   asn STRING
   )
   ROW FORMAT DELIMITED 
   FIELDS TERMINATED BY '\t'
   LOCATION 's3://amzn-s3-demo-bucket/'
   TBLPROPERTIES ( 'skip.header.line.count'='2' )
   ```

1. 在 Athena 控制台中运行查询。查询完成后，Athena 将注册 `cloudfront_real_time_logs` 表，使其中的数据可以供您发出查询。