

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# AWS DMS 使用 AWS SDKs的基本範例
<a name="database-migration-service_code_examples_basics"></a>

下列程式碼範例示範如何 AWS Database Migration Service 搭配 AWS SDKs 使用 的基本概念。

**Contents**
+ [動作](database-migration-service_code_examples_actions.md)
  + [`CreateReplicationTask`](database-migration-service_example_database-migration-service_CreateReplicationTask_section.md)

# AWS DMS 使用 AWS SDKs的動作
<a name="database-migration-service_code_examples_actions"></a>

下列程式碼範例示範如何使用 AWS SDKs執行個別 AWS DMS 動作。每個範例均包含 GitHub 的連結，您可以在連結中找到設定和執行程式碼的相關說明。

 下列範例僅包含最常使用的動作。如需完整清單，請參閱《[AWS Database Migration Service API 參考](https://docs.aws.amazon.com/dms/latest/APIReference/Welcome.html)》。

**Topics**
+ [`CreateReplicationTask`](database-migration-service_example_database-migration-service_CreateReplicationTask_section.md)

# 搭配使用 `CreateReplicationTask` 與 CLI
<a name="database-migration-service_example_database-migration-service_CreateReplicationTask_section"></a>

下列程式碼範例示範如何使用 `CreateReplicationTask`。

------
#### [ CLI ]

**AWS CLI**  
**建立複寫任務**  
下列 `create-replication-task` 範例會建立複寫任務。  

```
aws dms create-replication-task \
    --replication-task-identifier movedata \
    --source-endpoint-arn arn:aws:dms:us-east-1:123456789012:endpoint:6GGI6YPWWGAYUVLKIB732KEVWA \
    --target-endpoint-arn arn:aws:dms:us-east-1:123456789012:endpoint:EOM4SFKCZEYHZBFGAGZT3QEC5U \
    --replication-instance-arn $RI_ARN \
    --migration-type full-load \
    --table-mappings file://table-mappings.json
```
`table-mappings.json` 的內容：  

```
{
    "rules": [
        {
            "rule-type": "selection",
            "rule-id": "1",
            "rule-name": "1",
            "object-locator": {
                "schema-name": "prodrep",
                "table-name": "%"
            },
            "rule-action": "include",
            "filters": []
        }
    ]
}
```
輸出：  

```
{
    "ReplicationTask": {
        "ReplicationTaskIdentifier": "moveit2",
        "SourceEndpointArn": "arn:aws:dms:us-east-1:123456789012:endpoint:6GGI6YPWWGAYUVLKIB732KEVWA",
        "TargetEndpointArn": "arn:aws:dms:us-east-1:123456789012:endpoint:EOM4SFKCZEYHZBFGAGZT3QEC5U",
        "ReplicationInstanceArn": "arn:aws:dms:us-east-1:123456789012:rep:T3OM7OUB5NM2LCVZF7JPGJRNUE",
        "MigrationType": "full-load",
        "TableMappings": ...output omitted... ,
        "ReplicationTaskSettings": ...output omitted... ,
        "Status": "creating",
        "ReplicationTaskCreationDate": 1590524772.505,
        "ReplicationTaskArn": "arn:aws:dms:us-east-1:123456789012:task:K55IUCGBASJS5VHZJIINA45FII"
    }
}
```
如需詳細資訊，請參閱《 Database Migration Service [AWS 使用者指南》中的使用 DMS 任務](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Tasks.html)。 *AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateReplicationTask](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/dms/create-replication-task.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：此範例會建立新的 AWS Database Migration Service 複寫任務，使用 CdcStartTime 而非 CdcStartPosition。MigrationType 設定為 "full-load-and-cdc"，意思是目標資料表必須為空白。新任務會加上具有 Stage 索引鍵和 Test 鍵值的標籤。如需此 cmdlet 所用值的詳細資訊，請參閱《 AWS Database Migration Service 使用者指南》中的建立任務 (https：//https://docs.aws.amazon.com/dms/latest/userguide/CHAP\$1Tasks.Creating.html)。**  

```
New-DMSReplicationTask -ReplicationInstanceArn "arn:aws:dms:us-east-1:123456789012:rep:EXAMPLE66XFJUWATDJGBEXAMPLE"`
  -CdcStartTime "2019-08-08T12:12:12"`
  -CdcStopPosition "server_time:2019-08-09T12:12:12"`
  -MigrationType "full-load-and-cdc"`
  -ReplicationTaskIdentifier "task1"`
  -ReplicationTaskSetting ""`
  -SourceEndpointArn "arn:aws:dms:us-east-1:123456789012:endpoint:EXAMPLEW5UANC7Y3P4EEXAMPLE"`
  -TableMapping "file:////home/testuser/table-mappings.json"`
  -Tag @{"Key"="Stage";"Value"="Test"}`
  -TargetEndpointArn "arn:aws:dms:us-east-1:123456789012:endpoint:EXAMPLEJZASXWHTWCLNEXAMPLE"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CreateReplicationTask](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：此範例會建立新的 AWS Database Migration Service 複寫任務，使用 CdcStartTime 而非 CdcStartPosition。MigrationType 設定為 "full-load-and-cdc"，意思是目標資料表必須為空白。新任務會加上具有 Stage 索引鍵和 Test 鍵值的標籤。如需此 cmdlet 使用之值的詳細資訊，請參閱《 AWS Database Migration Service 使用者指南》中的建立任務 (https：//https://docs.aws.amazon.com/dms/latest/userguide/CHAP\$1Tasks.Creating.html)。**  

```
New-DMSReplicationTask -ReplicationInstanceArn "arn:aws:dms:us-east-1:123456789012:rep:EXAMPLE66XFJUWATDJGBEXAMPLE"`
  -CdcStartTime "2019-08-08T12:12:12"`
  -CdcStopPosition "server_time:2019-08-09T12:12:12"`
  -MigrationType "full-load-and-cdc"`
  -ReplicationTaskIdentifier "task1"`
  -ReplicationTaskSetting ""`
  -SourceEndpointArn "arn:aws:dms:us-east-1:123456789012:endpoint:EXAMPLEW5UANC7Y3P4EEXAMPLE"`
  -TableMapping "file:////home/testuser/table-mappings.json"`
  -Tag @{"Key"="Stage";"Value"="Test"}`
  -TargetEndpointArn "arn:aws:dms:us-east-1:123456789012:endpoint:EXAMPLEJZASXWHTWCLNEXAMPLE"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateReplicationTask](https://docs.aws.amazon.com/powershell/v5/reference)。

------