

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

# 在 AWS Glue 中將 ETL 程序轉換為 AWS Schema Conversion Tool
<a name="CHAP-converting-aws-glue-api-process"></a>

在下列各節中，您可以找到在 Python 中呼叫 AWS Glue API 操作的轉換說明。如需詳細資訊，請參閱《 開發人員指南》中的在 [Python 中程式設計 AWS Glue ETL 指令碼](https://docs.aws.amazon.com//glue/latest/dg/aws-glue-programming-python.html)。* AWS Glue *

**Topics**
+ [步驟 1：建立資料庫](#CHAP-converting-aws-glue-step-api-create-db)
+ [步驟 2：建立連線](#CHAP-converting-aws-glue-step-api-connection)
+ [步驟 3：建立 AWS Glue 爬蟲程式](#CHAP-converting-aws-glue-step-api-crawler)

## 步驟 1：建立資料庫
<a name="CHAP-converting-aws-glue-step-api-create-db"></a>

第一步是使用 [AWS SDK API](https://docs.aws.amazon.com/glue/latest/webapi/API_CreateDatabase.html) 在 AWS Glue Data Catalog 中建立新的資料庫。當您在資料目錄中定義資料表時，就會將資料表加入資料庫。資料庫用於組織其中的資料表 AWS Glue。

下列範例示範 Python API 的 `create_database`方法 AWS Glue。

```
response = client.create_database(
    DatabaseInput={
        'Name': '{{database_name}}’,
        'Description': '{{description}}',
        'LocationUri': '{{string}}',
        'Parameters': {         
            '{{parameter-name}}': '{{parameter value}}'
        }
    }
)
```

如果您使用的是 Amazon Redshift，資料庫名稱的格式如下所示。

```
{redshift_cluster_name}_{redshift_database_name}_{redshift_schema_name}
```

此範例的 Amazon Redshift 叢集完整名稱如下。

```
rsdbb03.apq1mpqso.us-west-2.redshift.amazonaws.com
```

以下顯示正確格式的資料庫名稱範例。在這種情況下，`rsdbb03` 是名稱，這是叢集端點完整名稱的第一部分。資料庫名為 `dev` 且結構描述為 `ora_glue`。

```
rsdbb03_dev_ora_glue
```

## 步驟 2：建立連線
<a name="CHAP-converting-aws-glue-step-api-connection"></a>

使用 [AWS SDK API](https://docs.aws.amazon.com/glue/latest/webapi/API_CreateConnection.html) 在 Data Catalog 中建立新的連線。

下列範例示範如何使用 Python API 的 [https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-catalog-connections.html](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-catalog-connections.html)方法 AWS Glue。

 

```
response = client.create_connection(
    ConnectionInput={
        'Name': '{{Redshift_abcde03.aabbcc112233.us-west-2.redshift.amazonaws.com_dev}}',
        'Description': {{'Created from SCT}}',
        'ConnectionType': '{{JDBC}}',
        'ConnectionProperties': {
            {{'JDBC_CONNECTION_URL': 'jdbc:redshift://aabbcc03.aabbcc112233.us-west-2.redshift.amazonaws.com:5439/dev',
            'USERNAME': 'user_name',
            'PASSWORD': 'password'}}
        },
        'PhysicalConnectionRequirements': {
            'AvailabilityZone': '{{us-west-2c}}',
            'SubnetId': '{{subnet-a1b23c45}}',
            'SecurityGroupIdList': [
                '{{sg-000a2b3c}}', '{{sg-1a230b4c}}', '{{sg-aba12c3d}}', '{{sg-1abb2345}}'
            ]
        }
    }
)
```

`create_connection` 中使用的參數如下：
+ `Name` (UTF-8 字串） – 必要。對於 Amazon Redshift，連線名稱的格式如下：`Redshift_{{<Endpoint-name>}}_{{<redshift-database-name>}}`，例如：` Redshift_abcde03_dev`
+ `Description` (UTF-8 字串） – 您的連線描述。
+ `ConnectionType` (UTF-8 字串） – 必要。連線的類型。目前，只支援 JDBC，不支援 SFTP。
+ `ConnectionProperties` (dict) – 必要。做為此連線參數的鍵值對清單，包括 JDBC 連線 URL、使用者名稱和密碼。
+ `PhysicalConnectionRequirements` (dict) – 實體連線需求，包括下列項目：
  + `SubnetId` (UTF-8 字串） – 連線所使用的子網路 ID。
  + `SecurityGroupIdList` （清單） – 連線使用的安全群組 ID 清單。
  + `AvailabilityZone` (UTF-8 字串） – 必要。包含端點的可用區域。此參數已棄用。

## 步驟 3：建立 AWS Glue 爬蟲程式
<a name="CHAP-converting-aws-glue-step-api-crawler"></a>

接著，您可以建立 AWS Glue 爬蟲程式來填入 AWS Glue 目錄。如需詳細資訊，請參閱《 開發人員指南》中的[使用爬蟲程式編目資料表](https://docs.aws.amazon.com/glue/latest/dg/add-crawler.html)。 *AWS Glue *

新增爬蟲程式的第一步是使用 [AWS SDK API](https://docs.aws.amazon.com//glue/latest/webapi/API_CreateCrawler.html) 在 Data Catalog 中建立新的資料庫。開始之前，請務必先使用 `delete_crawler`操作刪除其任何先前版本。

建立爬蟲程式時，請納入幾個考量：
+ 對於爬蟲程式名稱，請使用格式 `{{<redshift_node_name>}}_{{<redshift_database_name>}}_{{<redshift_shema_name>}}`，例如： `abcde03_dev_ora_glue`
+ 使用已存在的 IAM 角色。如需建立 IAM 角色的詳細資訊，請參閱《[IAM 使用者指南》中的建立 IAM 角色](https://docs.aws.amazon.com//IAM/latest/UserGuide/id_roles_create.html)*。*
+ 使用您在上一個步驟建立的資料庫名稱。
+ 使用 `ConnectionName` 參數 (此為必要)。
+ 對於 `path` 參數，請使用 JDBC 目標的路徑，例如：`dev/ora_glue/%`

以下範例會刪除現有的爬蟲程式，然後使用 AWS Glue適用的 Python API 建立新的爬蟲程式。

```
response = client.delete_crawler(
    Name='{{crawler_name}}'
)

response = client.create_crawler(
    Name='{{crawler_name}}',
    Role= ‘{{IAM_role}}’,
    DatabaseName='{{database_name}}’,
    Description='{{string}}',
    Targets={
        'S3Targets': [
            {
                'Path': '{{string}}',
                'Exclusions': [
                    '{{string}}',
                ]
            },
        ],
        'JdbcTargets': [
            {
                'ConnectionName': ‘{{ConnectionName}}’,
                'Path': ‘{{Include_path}}’,
                'Exclusions': [
                    '{{string}}',
                ]
            },
        ]
    },
    Schedule='{{string}}',
    Classifiers=[
        '{{string}}',
    ],
    TablePrefix='{{string}}',
    SchemaChangePolicy={
        'UpdateBehavior': {{'LOG'|'UPDATE_IN_DATABASE'}},
        'DeleteBehavior': {{'LOG'|'DELETE_FROM_DATABASE'|'DEPRECATE_IN_DATABASE'}}
    },
    Configuration='{{string}}'
)
```

建立連接到一或多個資料存放區的爬蟲程式並加以執行，判斷資料結構，然後將資料表寫入資料目錄。您可以依排程執行您的爬蟲程式，如下所示。

```
response = client.start_crawler(
    Name='string'
)
```

此範例使用 Amazon Redshift 做為目標。在爬蟲程式執行之後，Amazon Redshift 資料類型會以下列方式對應至 AWS Glue 資料類型。


|  |  | 
| --- |--- |
| Amazon Redshift 資料類型 | AWS Glue 資料類型 | 
| smallint | smallint | 
| integer | int | 
| bigint | bigint | 
| decimal | decimal(18,0) | 
| decimal(p,s) | decimal(p,s) | 
| real | double | 
| double precision | double | 
| 布林值 | 布林值 | 
| char | string | 
| varchar | string | 
| varchar(n) | string | 
| 日期 | 日期 | 
| timestamp | timestamp | 
| timestamptz | timestamp | 