

AWS Data Pipeline 不再提供給新客戶。的現有客戶 AWS Data Pipeline 可以繼續正常使用服務。[進一步了解](https://aws.amazon.com/blogs/big-data/migrate-workloads-from-aws-data-pipeline/)

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

# 使用 將資料複製到 Amazon Redshift AWS Data Pipeline
將資料複製到 Amazon Redshift

本教學課程將逐步引導您建立管道，以定期使用 AWS Data Pipeline 主控台中的複製到 Redshift 範本，或使用 CLI 將資料從 Amazon S3 移至 AWS Data Pipeline Amazon Redshift 的管道定義檔案。 ****

Amazon S3 是一種 Web 服務，可讓您將資料存放在雲端。如需詳細資訊，請參閱 [Amazon Simple Storage Service 使用者指南](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)。

Amazon Redshift 是雲端中的資料倉儲服務。如需詳細資訊，請參閱 [Amazon Redshift 管理指南](https://docs.aws.amazon.com/redshift/latest/mgmt/)。

本教學有數項事前準備。完成下列步驟後，您可以使用主控台或 CLI 繼續教學。

**Topics**
+ [

# 在您開始之前：設定 COPY 選項並載入資料
](dp-learn-copy-redshift.md)
+ [

# 設定管道、建立安全群組和建立 Amazon Redshift 叢集
](dp-copydata-redshift-prereq.md)
+ [

# 使用命令列將資料複製到 Amazon Redshift
](dp-copydata-redshift-cli.md)

# 在您開始之前：設定 COPY 選項並載入資料
在您開始之前：設定 COPY 選項

在 內將資料複製到 Amazon Redshift 之前 AWS Data Pipeline，請確定您：
+ 從 Amazon S3 載入資料。
+ 在 Amazon Redshift 中設定`COPY`活動。

一旦讓這些選項開始運作並成功完成資料載入，請將這些選項傳輸至 AWS Data Pipeline，在其中執行複製。

 如需`COPY`選項，請參閱《Amazon Redshift *資料庫開發人員指南*》中的 [COPY](https://docs.aws.amazon.com/redshift/latest/dg/r_COPY.html)。

如需從 Amazon S3 載入資料的步驟，請參閱《Amazon Redshift *資料庫開發人員指南*》中的[從 Amazon S3 載入資料](https://docs.aws.amazon.com/redshift/latest/dg/t_Loading-data-from-S3.html)。

例如，Amazon Redshift 中的下列 SQL 命令會建立新的資料表，名為 `LISTING`，並從 Amazon S3 中公開可用的儲存貯體複製範例資料。

以您自己的值取代 `<iam-role-arn>` 和區域。

如需此範例的詳細資訊，請參閱[《Amazon Redshift 入門指南》中的從 Amazon S3 載入範例資料](https://docs.aws.amazon.com/redshift/latest/gsg/rs-gsg-create-sample-db.html)。 **

```
create table listing(
	listid integer not null distkey,
	sellerid integer not null,
	eventid integer not null,
	dateid smallint not null  sortkey,
	numtickets smallint not null,
	priceperticket decimal(8,2),
	totalprice decimal(8,2),
	listtime timestamp);

copy listing from 's3://awssampledbuswest2/tickit/listings_pipe.txt' 
credentials 'aws_iam_role=<iam-role-arn>' 
delimiter '|' region 'us-west-2';
```

# 設定管道、建立安全群組和建立 Amazon Redshift 叢集
在您開始之前：設定管道、安全性和叢集

**針對教學設定**

1. 完成 [設定 的 AWS Data Pipeline](dp-get-setup.md) 中的任務。

1. 建立安全群組。

   1. 開啟 Amazon EC2 主控台。

   1. 在導覽窗格中，按一下 **Security Groups (安全群組)**。

   1. 按一下 **Create Security Group (建立安全群組)**。

   1. 指定安全群組的名稱和描述。

   1. 【EC2-Classic】 選取 `No VPC` **VPC**。

   1. [EC2-VPC] 針對 **VPC** 選取您 VPC 的 ID。

   1. 按一下 **Create (建立)**。

1. 【EC2-Classic】 建立 Amazon Redshift 叢集安全群組，並指定 Amazon EC2 安全群組。

   1. 開啟 Amazon Redshift 主控台。

   1. 在導覽窗格中，按一下 **Security Groups (安全群組)**。

   1. 按一下 **Create Cluster Security Group (建立叢集安全群組)**。

   1. 在 **Create Cluster Security Group (建立叢集安全群組)** 對話方塊中，指定叢集安全群組的名稱和描述。

   1. 按一下新叢集安全群組的名稱。

   1. 按一下 **Add Connection Type (新增連線類型)**。

   1. 在**新增連線類型**對話方塊中，從**連線類型**中選取 **EC2 安全群組**，選取您從 **EC2 安全群組名稱建立的安全群組**，然後按一下**授權**。

1. 【EC2-VPC】 建立 Amazon Redshift 叢集安全群組，並指定 VPC 安全群組。

   1. 開啟 Amazon EC2 主控台。

   1. 在導覽窗格中，按一下 **Security Groups (安全群組)**。

   1. 按一下 **Create Security Group (建立安全群組)**。

   1. 在 **Create Security Group (建立安全群組)** 對話方塊中，指定安全群組的名稱和描述，然後針對 **VPC** 選取 VPC 的 ID。

   1. 按一下 **Add Rule (新增規則)**。指定類型、協定和連接埠範圍，然後在 **Source (來源)** 中開始輸入安全群組的 ID。選取您在第二個步驟中建立的安全群組。

   1. 按一下 **Create (建立)**。

1. 下列是這些步驟的摘要。

   如果您有現有的 Amazon Redshift 叢集，請記下叢集 ID。

   若要建立新的叢集並載入範例資料，請遵循 [Amazon Redshift 入門](https://docs.aws.amazon.com/redshift/latest/gsg/getting-started.html)中的步驟。如需建立叢集的詳細資訊，請參閱《*Amazon Redshift 管理指南*》中的[建立叢集](https://docs.aws.amazon.com/redshift/latest/mgmt/managing-clusters-console.html#create-cluster)。

   1. 開啟 Amazon Redshift 主控台。

   1. 按一下 **Launch Cluster (啟動叢集)**。

   1. 為您的叢集提供所需的詳細資訊，然後按一下 **Continue (繼續)**。

   1. 提供節點組態，然後按一下 **Continue (繼續)**。

   1. 在額外組態資訊頁面中，選取您建立的叢集安全群組，然後按一下 **Continue (繼續)**。

   1. 檢閱您叢集的規格，然後按一下 **Launch Cluster (啟動叢集)**。

# 使用命令列將資料複製到 Amazon Redshift
使用 CLI

本教學課程示範如何將資料從 Amazon S3 複製到 Amazon Redshift。您將在 Amazon Redshift 中建立新的資料表，然後使用 從公有 Amazon S3 儲存貯體 AWS Data Pipeline 傳輸資料至此資料表，其中包含 CSV 格式的範例輸入資料。日誌會儲存到您擁有的 Amazon S3 儲存貯體。

Amazon S3 是一種 Web 服務，可讓您將資料存放在雲端。如需詳細資訊，請參閱 [Amazon Simple Storage Service 使用者指南](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)。Amazon Redshift 是雲端中的資料倉儲服務。如需詳細資訊，請參閱 [Amazon Redshift 管理指南](https://docs.aws.amazon.com/redshift/latest/mgmt/)。

**先決條件**

開始之前，您必須完成下列步驟：

1. 安裝和設定命令列界面 (CLI)。如需詳細資訊，請參閱[存取 AWS Data Pipeline](what-is-datapipeline.md#accessing-datapipeline)。

1. 確保名為 **DataPipelineDefaultRole** 和 **DataPipelineDefaultResourceRole** 的 IAM 角色存在。 AWS Data Pipeline 主控台會自動為您建立這些角色。如果您至少尚未使用 AWS Data Pipeline 主控台一次，則必須手動建立這些角色。如需詳細資訊，請參閱[的 IAM 角色 AWS Data Pipeline](dp-iam-roles.md)。

1. 在 Amazon Redshift 中設定 `COPY`命令，因為當您在其中執行複製時，將需要使用這些相同的選項 AWS Data Pipeline。如需相關資訊，請參閱[在您開始之前：設定 COPY 選項並載入資料](dp-learn-copy-redshift.md)。

1. 設定 Amazon Redshift 資料庫。如需詳細資訊，請參閱[設定管道、建立安全群組和建立 Amazon Redshift 叢集](dp-copydata-redshift-prereq.md)。

**Topics**
+ [

# 以 JSON 格式定義管道
](dp-copydata-redshift-define-pipeline-cli.md)
+ [

# 上傳和啟用管道定義
](dp-copydata-redshift-upload-cli.md)

# 以 JSON 格式定義管道


此範例案例示範如何將資料從 Amazon S3 儲存貯體複製到 Amazon Redshift。

這是完整的管道定義 JSON 檔案，後面接著說明其每個部分。建議您使用文字編輯器，協助您驗證 JSON 格式檔案的語法，並使用 `.json` 副檔名命名檔案。

```
{
  "objects": [
    {
      "id": "CSVId1",
      "name": "DefaultCSV1",
      "type": "CSV"
    },
    {
      "id": "RedshiftDatabaseId1",
      "databaseName": "dbname",
      "username": "user",
      "name": "DefaultRedshiftDatabase1",
      "*password": "password",
      "type": "RedshiftDatabase",
      "clusterId": "redshiftclusterId"
    },
    {
      "id": "Default",
      "scheduleType": "timeseries",
      "failureAndRerunMode": "CASCADE",
      "name": "Default",
      "role": "DataPipelineDefaultRole",
      "resourceRole": "DataPipelineDefaultResourceRole"
    },
    {
      "id": "RedshiftDataNodeId1",
      "schedule": {
        "ref": "ScheduleId1"
      },
      "tableName": "orders",
      "name": "DefaultRedshiftDataNode1",
      "createTableSql": "create table StructuredLogs (requestBeginTime CHAR(30) PRIMARY KEY DISTKEY SORTKEY, requestEndTime CHAR(30), hostname CHAR(100), requestDate varchar(20));",
      "type": "RedshiftDataNode",
      "database": {
        "ref": "RedshiftDatabaseId1"
      }
    },
    {
      "id": "Ec2ResourceId1",
      "schedule": {
        "ref": "ScheduleId1"
      },
      "securityGroups": "MySecurityGroup",
      "name": "DefaultEc2Resource1",
      "role": "DataPipelineDefaultRole",
      "logUri": "s3://myLogs",
      "resourceRole": "DataPipelineDefaultResourceRole",
      "type": "Ec2Resource"
    },
    {
      "id": "ScheduleId1",
      "startDateTime": "yyyy-mm-ddT00:00:00",
      "name": "DefaultSchedule1",
      "type": "Schedule",
      "period": "period",
      "endDateTime": "yyyy-mm-ddT00:00:00"
    },
    {
      "id": "S3DataNodeId1",
      "schedule": {
        "ref": "ScheduleId1"
      },
      "filePath": "s3://datapipeline-us-east-1/samples/hive-ads-samples.csv",
      "name": "DefaultS3DataNode1",
      "dataFormat": {
        "ref": "CSVId1"
      },
      "type": "S3DataNode"
    },
    {
      "id": "RedshiftCopyActivityId1",
      "input": {
        "ref": "S3DataNodeId1"
      },
      "schedule": {
        "ref": "ScheduleId1"
      },
      "insertMode": "KEEP_EXISTING",
      "name": "DefaultRedshiftCopyActivity1",
      "runsOn": {
        "ref": "Ec2ResourceId1"
      },
      "type": "RedshiftCopyActivity",
      "output": {
        "ref": "RedshiftDataNodeId1"
      }
    }
  ]
}
```

如需這些物件的詳細資訊，請參閱下列文件。

**Topics**
+ [

# 資料節點
](dp-copydata-redshift-node-cli.md)
+ [

# 資源
](dp-copydata-redshift-resource-cli.md)
+ [

# 活動
](dp-copydata-redshift-activity-cli.md)

# 資料節點


此範例使用輸入資料節點、輸出資料節點和資料庫。

**輸入資料節點**  
輸入`S3DataNode`管道元件會定義 Amazon S3 中輸入資料的位置，以及輸入資料的資料格式。如需詳細資訊，請參閱[S3DataNode](dp-object-s3datanode.md)。

此輸入元件是由下列欄位定義：

```
{
  "id": "S3DataNodeId1",
  "schedule": {
    "ref": "ScheduleId1"
  },
  "filePath": "s3://datapipeline-us-east-1/samples/hive-ads-samples.csv",
  "name": "DefaultS3DataNode1",
  "dataFormat": {
    "ref": "CSVId1"
  },
  "type": "S3DataNode"
},
```

`id`  
使用者定義的 ID，這是僅供您參考的標籤。

`schedule`  
對排程元件的參考。

`filePath`  
與資料節點相關聯資料的路徑，在此範例中是 CSV 輸入檔。

`name`  
使用者定義的名稱，這是僅供您參考的標籤。

`dataFormat`  
對要處理之活動資料格式的參考。

**輸出資料節點**  
輸出`RedshiftDataNode`管道元件會定義輸出資料的位置；在此情況下，Amazon Redshift 資料庫中的資料表。如需詳細資訊，請參閱[RedshiftDataNode](dp-object-redshiftdatanode.md)。此輸出元件是由下列欄位定義：

```
{
  "id": "RedshiftDataNodeId1",
  "schedule": {
    "ref": "ScheduleId1"
  },
  "tableName": "orders",
  "name": "DefaultRedshiftDataNode1",
  "createTableSql": "create table StructuredLogs (requestBeginTime CHAR(30) PRIMARY KEY DISTKEY SORTKEY, requestEndTime CHAR(30), hostname CHAR(100), requestDate varchar(20));",
  "type": "RedshiftDataNode",
  "database": {
    "ref": "RedshiftDatabaseId1"
  }
},
```

`id`  
使用者定義的 ID，這是僅供您參考的標籤。

`schedule`  
對排程元件的參考。

`tableName`  
Amazon Redshift 資料表的名稱。

`name`  
使用者定義的名稱，這是僅供您參考的標籤。

`createTableSql`  
在資料庫建立資料表的 SQL 表達式。

`database`  
Amazon Redshift 資料庫的參考。

**資料庫**  
`RedshiftDatabase` 元件是由下列欄位定義。如需詳細資訊，請參閱[RedshiftDatabase](dp-object-redshiftdatabase.md)。

```
{
  "id": "RedshiftDatabaseId1",
  "databaseName": "dbname",
  "username": "user",
  "name": "DefaultRedshiftDatabase1",
  "*password": "password",
  "type": "RedshiftDatabase",
  "clusterId": "redshiftclusterId"
},
```

`id`  
使用者定義的 ID，這是僅供您參考的標籤。

`databaseName`  
邏輯資料庫的名稱。

`username`  
連線至資料庫的使用者名稱。

`name`  
使用者定義的名稱，這是僅供您參考的標籤。

`password`  
連線至資料庫的密碼。

`clusterId`  
Redshift 叢集的 ID。

# 資源


這是執行複製操作的運算資源定義。在此範例中， AWS Data Pipeline 應該自動建立 EC2 執行個體來執行複製任務，並在任務完成後終止執行個體。此處定義的欄位會控制完成此工作之執行個體的建立和功能。如需詳細資訊，請參閱[Ec2Resource](dp-object-ec2resource.md)。

`Ec2Resource` 是由下列欄位定義：

```
{
  "id": "Ec2ResourceId1",
  "schedule": {
    "ref": "ScheduleId1"
  },
  "securityGroups": "MySecurityGroup",
  "name": "DefaultEc2Resource1",
  "role": "DataPipelineDefaultRole",
  "logUri": "s3://myLogs",
  "resourceRole": "DataPipelineDefaultResourceRole",
  "type": "Ec2Resource"
},
```

`id`  
使用者定義的 ID，這是僅供您參考的標籤。

`schedule`  
建立此運算資源所依據的排程。

`securityGroups`  
在資源集區中供執行個體使用的安全群組。

`name`  
使用者定義的名稱，這是僅供您參考的標籤。

`role`  
存取 資源之帳戶的 IAM 角色，例如存取 Amazon S3 儲存貯體以擷取資料。

`logUri`  
從 備份任務執行器日誌的 Amazon S3 目的地路徑`Ec2Resource`。

`resourceRole`  
建立資源的帳戶 IAM 角色，例如代您建立和設定 EC2 執行個體。Role 和 ResourceRole 可以是相同的角色，但不同的角色可提高您安全組態的精細程度。

# 活動


JSON 檔案的最後部分是代表所要執行工作的活動定義。在此情況下，我們使用`RedshiftCopyActivity`元件將資料從 Amazon S3 複製到 Amazon Redshift。如需詳細資訊，請參閱[RedshiftCopyActivity](dp-object-redshiftcopyactivity.md)。

`RedshiftCopyActivity` 元件是由下列欄位定義：

```
{
  "id": "RedshiftCopyActivityId1",
  "input": {
    "ref": "S3DataNodeId1"
  },
  "schedule": {
    "ref": "ScheduleId1"
  },
  "insertMode": "KEEP_EXISTING",
  "name": "DefaultRedshiftCopyActivity1",
  "runsOn": {
    "ref": "Ec2ResourceId1"
  },
  "type": "RedshiftCopyActivity",
  "output": {
    "ref": "RedshiftDataNodeId1"
  }
},
```

`id`  
使用者定義的 ID，這是僅供您參考的標籤。

`input`  
Amazon S3 來源檔案的參考。

`schedule`  
執行此活動所依據的排程。

`insertMode`  
插入類型 (`KEEP_EXISTING`、`OVERWRITE_EXISTING` 或 `TRUNCATE`)。

`name`  
使用者定義的名稱，這是僅供您參考的標籤。

`runsOn`  
執行此活動所定義工作的運算資源。

`output`  
Amazon Redshift 目的地資料表的參考。

# 上傳和啟用管道定義


您必須上傳管道定義並啟用管道。在下列範例命令中，將 *pipeline\$1name* 取代為管道的標籤，並將 *pipeline\$1file* 取代為管道定義`.json`檔案的完整路徑。

**AWS CLI**

若要建立管道定義並啟用管道，請使用下列 [create-pipeline](https://docs.aws.amazon.com/cli/latest/reference/datapipeline/create-pipeline.html) 命令。請注意管道的 ID，因為您會將此值與大多數 CLI 命令搭配使用。

```
aws datapipeline create-pipeline --name pipeline_name --unique-id token
{
    "pipelineId": "df-00627471SOVYZEXAMPLE"
}
```

若要上傳管道定義，請使用下列 [put-pipeline-definition](https://docs.aws.amazon.com/cli/latest/reference/datapipeline/put-pipeline-definition.html) 命令。

```
aws datapipeline put-pipeline-definition --pipeline-id df-00627471SOVYZEXAMPLE --pipeline-definition file://MyEmrPipelineDefinition.json
```

如果您管道驗證成功， `validationErrors` 欄位會是空的。您應該檢閱任何警告。

若要啟用管道，請使用下列 [activate-pipeline](https://docs.aws.amazon.com/cli/latest/reference/datapipeline/activate-pipeline.html) 命令。

```
aws datapipeline activate-pipeline --pipeline-id df-00627471SOVYZEXAMPLE
```

您可以使用下列 [list-pipelines](https://docs.aws.amazon.com/cli/latest/reference/datapipeline/list-pipelines.html) 命令，驗證管道是否顯示在管道清單中。

```
aws datapipeline list-pipelines
```