

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

# 使用手動分割為整個組織的追蹤建立資料表
<a name="create-cloudtrail-table-org-wide-trail"></a>

若要在 Athena 為整個組織的 CloudTrail 日誌檔建立資料表，請依循[使用手動分割在 Athena 中建立 CloudTrail 日誌的資料表](create-cloudtrail-table.md)所述的步驟在下列程序中修改。

**為整個組織的 CloudTrail 日誌建立 Athena 資料表**

1. 在 `CREATE TABLE` 陳述式中，修改 `LOCATION` 子句以加入組織 ID，如下列範例所示：

   ```
   LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/organization_id/'
   ```

1. 在 `PARTITIONED BY` 子句中，以字串形式新增帳戶 ID 項目，如下列範例所示：

   ```
   PARTITIONED BY (account string, region string, year string, month string, day string)
   ```

   上述步驟的合併結果如以下範例所示：

   ```
   ...
   
   PARTITIONED BY (account string, region string, year string, month string, day string) 
   ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
   STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat'
   OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
   LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/organization_id/Account_ID/CloudTrail/'
   ```

1. 在 `ALTER TABLE` 陳述式中的 `ADD PARTITION` 子句內加入帳戶 ID，如下列範例所示：

   ```
   ALTER TABLE table_name ADD
   PARTITION (account='111122223333',
   region='us-east-1',
   year='2022',
   month='08',
   day='08')
   ```

1. 在 `ALTER TABLE` 陳述式中的 `LOCATION` 子句內加入組織 ID、帳戶 ID，以及您要新增的分割區，如下列範例所示：

   ```
   LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/organization_id/Account_ID/CloudTrail/us-east-1/2022/08/08/'
   ```

   上述步驟的合併結果如以下 `ALTER TABLE` 陳述式範例所示：

   ```
   ALTER TABLE table_name ADD
   PARTITION (account='111122223333',
   region='us-east-1',
   year='2022',
   month='08',
   day='08')
   LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/organization_id/111122223333/CloudTrail/us-east-1/2022/08/08/'
   ```

請注意，在大型組織中，使用此方法來手動為每個組織帳戶 ID 新增和維護分割區可能會很麻煩。在這種情況下，請考慮使用 CloudTrail Lake 而不是 Athena。在這種情況下，CloudTrail Lake 可提供下列優勢：
+ 自動彙總整個組織的日誌
+ 不需要設定或維護分割區或 Athena 資料表
+ 查詢會在 CloudTrail 主控台中直接執行
+ 使用 SQL 相容查詢語言

如需詳細資訊，請參閱*AWS CloudTrail 《 使用者指南*》中的[使用 AWS CloudTrail Lake](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake.html)。