

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

# 使用 AWS CLI 重新建立 AWS Glue 資料庫及其資料表
<a name="glue-recreate-db-and-tables-cli"></a>

無法直接重新命名 AWS Glue 資料庫，但您可以複製其定義、修改定義，並使用定義以不同名稱重新建立資料庫。同樣地，您可以複製舊資料庫中資料表的定義、修改定義，並使用修改後的定義重新建立新資料庫中的資料表。

**注意**  
 顯示的方法不會複製資料表分割區。

下列 Windows 程序假設您的 AWS CLI 已設定為 JSON 輸出。若要變更 中的預設輸出格式 AWS CLI，請執行 `aws configure`。

**使用 複製 AWS Glue 資料庫 AWS CLI**

1. 在命令提示字元中，執行下列 AWS CLI 命令來擷取您要複製的 AWS Glue 資料庫定義。

   ```
   aws glue get-database --name {{database_name}}
   ```

   如需有關 `get-database` 命令的詳細資訊，請參閱 [get-database](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/glue/get-database.html)。

1. 將 JSON 輸出儲存到桌面上的檔案中，該檔案包含新資料庫的名稱 (例如 `{{new_database_name}}.json`)。

1. 在文字編輯器中開啟 `{{new_database_name}}.json` 檔案。

1. 在 JSON 檔案中，執行下列步驟：

   1. 移除檔案結尾的外部 `{ "Database":` 項目和對應的大括弧 `}`。

   1. 將 `Name` 項目變更為新的資料庫名稱。

   1. 移除 `CatalogId` 欄位。

1. 儲存檔案。

1. 在命令提示字元中，執行下列 AWS CLI 命令來使用修改過的資料庫定義檔案，以新名稱建立資料庫。

   ```
   aws glue create-database --database-input "file://~/Desktop\{{new_database_name}}.json"
   ```

   如需有關 `create-database` 命令的詳細資訊，請參閱 [create-database](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/glue/create-database.html)。如需有關從檔案載入 AWS CLI 參數的資訊，請參閱*AWS Command Line Interface 《 使用者指南*》中的[從檔案載入 AWS CLI 參數](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-file.html)。

1. 若要驗證是否已在 中建立新資料庫 AWS Glue，請執行下列命令：

   ```
   aws glue get-database --name {{new_database_name}}
   ```

現在，您已準備好取得要複製到新資料庫的資料表的定義、修改定義，並使用修改後的定義在新資料庫中重新建立資料表。此程序不會變更資料表名稱。

**使用 複製 AWS Glue 資料表 AWS CLI**

1. 在命令提示字元中，執行下列 AWS CLI 命令。

   ```
   aws glue get-table --database-name {{database_name}} --name {{table_name}}
   ```

   如需有關 `get-table` 命令的詳細資訊，請參閱 [get-table](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/glue/get-table.html)。

1. 將 JSON 輸出儲存到 Windows 桌面上的檔案中，該檔案包含資料表名稱 (例如 {{table\_name}}.json)。

1. 在文字編輯器中開啟該檔案。

1. 在 JSON 檔案中，移除檔案結尾的外部 `{"Table": ` 項目和對應的大括弧 `}`。

1. 在 JSON 檔案中，移除下列項目及其值：
   + `DatabaseName` – 因為 `create-table` CLI 命令使用 `--database-name` 參數，所以不需要此項目。
   + `CreateTime`
   + `UpdateTime`
   + `CreatedBy`
   + `IsRegisteredWithLakeFormation`
   + `CatalogId`
   + `VersionId`

1. 儲存資料表定義檔案。

1. 在命令提示字元中，執行下列 AWS CLI 命令以在新資料庫中重新建立資料表：

   ```
   aws glue create-table --database-name {{new_database_name}} --table-input "file://~/Desktop\{{table_name}}.json"     
   ```

   如需有關 `create-table` 命令的詳細資訊，請參閱 [create-table](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/glue/create-table.html)。

   資料表現在會出現在 中的新資料庫中 AWS Glue ，並且可以從 Athena 查詢。

1. 重複步驟，以將每個其他資料表複製到 AWS Glue中的新資料庫。