

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

# 以 Apache Parquet 格式為流程日誌建立資料表
<a name="vpc-flow-logs-parquet"></a>

以下程序會以 Parquet 格式建立 Amazon VPC 流程日誌的 Amazon VPC 資料表。

**為 Amazon VPC 流量日誌以 Parquet 格式建立 Athena 資料表**

1. 在 Athena 主控台查詢編輯器中輸入類似下列的 DDL 陳述式，遵循 [考量和限制](vpc-flow-logs.md#vpc-flow-logs-common-considerations) 區段中的準則。陳述式範例會建立擁有 Amazon VPC 流程日誌版本 2 至 5 (如 Parquet 格式、Hive 每小時已分割的[流程日誌記錄](https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html#flow-log-records)所記載) 之資料欄的資料表。如果您沒有小時分割區，請從 `PARTITIONED BY` 子句中移除 `hour`。

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS vpc_flow_logs_parquet (
     version int,
     account_id string,
     interface_id string,
     srcaddr string,
     dstaddr string,
     srcport int,
     dstport int,
     protocol bigint,
     packets bigint,
     bytes bigint,
     start bigint,
     `end` bigint,
     action string,
     log_status string,
     vpc_id string,
     subnet_id string,
     instance_id string,
     tcp_flags int,
     type string,
     pkt_srcaddr string,
     pkt_dstaddr string,
     region string,
     az_id string,
     sublocation_type string,
     sublocation_id string,
     pkt_src_aws_service string,
     pkt_dst_aws_service string,
     flow_direction string,
     traffic_path int
   )
   PARTITIONED BY (
     `aws-account-id` string,
     `aws-service` string,
     `aws-region` string,
     `year` string, 
     `month` string, 
     `day` string,
     `hour` string
   )
   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/prefix/AWSLogs/'
   TBLPROPERTIES (
     'EXTERNAL'='true', 
     'skip.header.line.count'='1'
     )
   ```

1. 修改 `LOCATION 's3://amzn-s3-demo-bucket/prefix/AWSLogs/'` 來指向包含日誌資料的 Amazon S3 路徑。

1. 在 Athena 主控台中執行查詢。

1. 如果您的資料採用 Hive 相容的格式，請在 Athena 主控台中執行以下命令來更新和載入中繼存放區中的 Hive 分割區。查詢完成後，您可以在 `vpc_flow_logs_parquet` 資料表中查詢資料。

   ```
   MSCK REPAIR TABLE vpc_flow_logs_parquet
   ```

   如果您沒有使用 Hive 相容的資料，請執行 [ALTER TABLE ADD PARTITION](alter-table-add-partition.md) 來載入分割區。

如需有關使用 Athena 查詢 Parquet 格式的 Amazon VPC 流程日誌的詳細資訊，請參閱 *AWS 大數據部落格*中的[使用 Apache Parquet 格式的 VPC 流程日誌最佳化效能並降低網路分析成本](https://aws.amazon.com/blogs/big-data/optimize-performance-and-reduce-costs-for-network-analytics-with-vpc-flow-logs-in-apache-parquet-format/)。