

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

# AWS Data Pipeline 使用 的範例 AWS CLI
<a name="cli_data-pipeline_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 來執行動作和實作常見案例 AWS Data Pipeline。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `activate-pipeline`
<a name="data-pipeline_ActivatePipeline_cli_topic"></a>

以下程式碼範例顯示如何使用 `activate-pipeline`。

**AWS CLI**  
**啟用管道**  
此範例會啟用指定的管道：  

```
aws datapipeline activate-pipeline --pipeline-id df-00627471SOVYZEXAMPLE
```
若要在特定日期和時間啟用管道，請使用下列命令：  

```
aws datapipeline activate-pipeline --pipeline-id df-00627471SOVYZEXAMPLE --start-timestamp 2015-04-07T00:00:00Z
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ActivatePipeline](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/activate-pipeline.html)。

### `add-tags`
<a name="data-pipeline_AddTags_cli_topic"></a>

以下程式碼範例顯示如何使用 `add-tags`。

**AWS CLI**  
**將標籤新增至管道**  
此範例會將指定的標籤新增至指定的管道：  

```
aws datapipeline add-tags --pipeline-id df-00627471SOVYZEXAMPLE --tags key=environment,value=production key=owner,value=sales
```
若要檢視標籤，請使用 describe-pipelines 命令。例如，範例命令中新增的標籤會顯示在 describe-pipelines 的輸出中，如下所示：  

```
{
    ...
        "tags": [
            {
                "value": "production",
                "key": "environment"
            },
            {
                "value": "sales",
                "key": "owner"
            }
        ]
    ...
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [AddTags](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/add-tags.html)。

### `create-pipeline`
<a name="data-pipeline_CreatePipeline_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-pipeline`。

**AWS CLI**  
**建立管道**  
此範例會建立管道：  

```
aws datapipeline create-pipeline --name my-pipeline --unique-id my-pipeline-token
```
下列為範例輸出：  

```
{
    "pipelineId": "df-00627471SOVYZEXAMPLE"
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreatePipeline](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/create-pipeline.html)。

### `deactivate-pipeline`
<a name="data-pipeline_DeactivatePipeline_cli_topic"></a>

以下程式碼範例顯示如何使用 `deactivate-pipeline`。

**AWS CLI**  
**停用管道**  
此範例會停用指定的管道：  

```
aws datapipeline deactivate-pipeline --pipeline-id df-00627471SOVYZEXAMPLE
```
若要在所有執行中的活動完成後再停用管道，請使用以下的命令：  

```
aws datapipeline deactivate-pipeline --pipeline-id df-00627471SOVYZEXAMPLE --no-cancel-active
```
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考》*中的 [DeactivatePipeline](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/deactivate-pipeline.html)。

### `delete-pipeline`
<a name="data-pipeline_DeletePipeline_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-pipeline`。

**AWS CLI**  
**刪除管道**  
此範例會刪除指定的管道：  

```
aws datapipeline delete-pipeline --pipeline-id df-00627471SOVYZEXAMPLE
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeletePipeline](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/delete-pipeline.html)。

### `describe-pipelines`
<a name="data-pipeline_DescribePipelines_cli_topic"></a>

以下程式碼範例顯示如何使用 `describe-pipelines`。

**AWS CLI**  
**描述您的管道**  
此範例描述指定的管道：  

```
aws datapipeline describe-pipelines --pipeline-ids df-00627471SOVYZEXAMPLE
```
下列為範例輸出：  

```
{
  "pipelineDescriptionList": [
      {
          "fields": [
              {
                  "stringValue": "PENDING",
                  "key": "@pipelineState"
              },
              {
                  "stringValue": "my-pipeline",
                  "key": "name"
              },
              {
                  "stringValue": "2015-04-07T16:05:58",
                  "key": "@creationTime"
              },
              {
                  "stringValue": "df-00627471SOVYZEXAMPLE",
                  "key": "@id"
              },
              {
                  "stringValue": "123456789012",
                  "key": "pipelineCreator"
              },
              {
                  "stringValue": "PIPELINE",
                  "key": "@sphere"
              },
              {
                  "stringValue": "123456789012",
                  "key": "@userId"
              },
              {
                  "stringValue": "123456789012",
                  "key": "@accountId"
              },
              {
                  "stringValue": "my-pipeline-token",
                  "key": "uniqueId"
              }
          ],
          "pipelineId": "df-00627471SOVYZEXAMPLE",
          "name": "my-pipeline",
          "tags": []
      }
  ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribePipelines](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/describe-pipelines.html)。

### `get-pipeline-definition`
<a name="data-pipeline_GetPipelineDefinition_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-pipeline-definition`。

**AWS CLI**  
**取得管道定義**  
以下範例會取得指定管道的管道定義：  

```
aws datapipeline get-pipeline-definition --pipeline-id df-00627471SOVYZEXAMPLE
```
下列為範例輸出：  

```
{
  "parameters": [
      {
          "type": "AWS::S3::ObjectKey",
          "id": "myS3OutputLoc",
          "description": "S3 output folder"
      },
      {
          "default": "s3://us-east-1.elasticmapreduce.samples/pig-apache-logs/data",
          "type": "AWS::S3::ObjectKey",
          "id": "myS3InputLoc",
          "description": "S3 input folder"
      },
      {
          "default": "grep -rc \"GET\" ${INPUT1_STAGING_DIR}/* > ${OUTPUT1_STAGING_DIR}/output.txt",
          "type": "String",
          "id": "myShellCmd",
          "description": "Shell command to run"
      }
  ],
  "objects": [
      {
          "type": "Ec2Resource",
          "terminateAfter": "20 Minutes",
          "instanceType": "t1.micro",
          "id": "EC2ResourceObj",
          "name": "EC2ResourceObj"
      },
      {
          "name": "Default",
          "failureAndRerunMode": "CASCADE",
          "resourceRole": "DataPipelineDefaultResourceRole",
          "schedule": {
              "ref": "DefaultSchedule"
          },
          "role": "DataPipelineDefaultRole",
          "scheduleType": "cron",
          "id": "Default"
      },
      {
          "directoryPath": "#{myS3OutputLoc}/#{format(@scheduledStartTime, 'YYYY-MM-dd-HH-mm-ss')}",
          "type": "S3DataNode",
          "id": "S3OutputLocation",
          "name": "S3OutputLocation"
      },
      {
          "directoryPath": "#{myS3InputLoc}",
          "type": "S3DataNode",
          "id": "S3InputLocation",
          "name": "S3InputLocation"
      },
      {
          "startAt": "FIRST_ACTIVATION_DATE_TIME",
          "name": "Every 15 minutes",
          "period": "15 minutes",
          "occurrences": "4",
          "type": "Schedule",
          "id": "DefaultSchedule"
      },
      {
          "name": "ShellCommandActivityObj",
          "command": "#{myShellCmd}",
          "output": {
              "ref": "S3OutputLocation"
          },
          "input": {
              "ref": "S3InputLocation"
          },
          "stage": "true",
          "type": "ShellCommandActivity",
          "id": "ShellCommandActivityObj",
          "runsOn": {
              "ref": "EC2ResourceObj"
          }
      }
  ],
  "values": {
      "myS3OutputLoc": "s3://amzn-s3-demo-bucket/",
      "myS3InputLoc": "s3://us-east-1.elasticmapreduce.samples/pig-apache-logs/data",
      "myShellCmd": "grep -rc \"GET\" ${INPUT1_STAGING_DIR}/* > ${OUTPUT1_STAGING_DIR}/output.txt"
  }
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetPipelineDefinition](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/get-pipeline-definition.html)。

### `list-pipelines`
<a name="data-pipeline_ListPipelines_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-pipelines`。

**AWS CLI**  
**列出管道**  
此範例列出您的管道：  

```
aws datapipeline list-pipelines
```
下列為範例輸出：  

```
{
  "pipelineIdList": [
      {
          "id": "df-00627471SOVYZEXAMPLE",
          "name": "my-pipeline"
      },
      {
          "id": "df-09028963KNVMREXAMPLE",
          "name": "ImportDDB"
      },
      {
          "id": "df-0870198233ZYVEXAMPLE",
          "name": "CrossRegionDDB"
      },
      {
          "id": "df-00189603TB4MZEXAMPLE",
          "name": "CopyRedshift"
      }
  ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListPipelines](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/list-pipelines.html)。

### `list-runs`
<a name="data-pipeline_ListRuns_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-runs`。

**AWS CLI**  
**範例 1：列出您的管道執行**  
下列 `list-runs` 範例列出指定管道的執行。  

```
aws datapipeline list-runs --pipeline-id df-00627471SOVYZEXAMPLE
```
輸出：  

```
    Name                       Scheduled Start        Status                     ID                                              Started                Ended
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.  EC2ResourceObj             2015-04-12T17:33:02    CREATING                   @EC2ResourceObj_2015-04-12T17:33:02             2015-04-12T17:33:10
2.  S3InputLocation            2015-04-12T17:33:02    FINISHED                   @S3InputLocation_2015-04-12T17:33:02            2015-04-12T17:33:09    2015-04-12T17:33:09
3.  S3OutputLocation           2015-04-12T17:33:02    WAITING_ON_DEPENDENCIES    @S3OutputLocation_2015-04-12T17:33:02           2015-04-12T17:33:09
4.  ShellCommandActivityObj    2015-04-12T17:33:02    WAITING_FOR_RUNNER         @ShellCommandActivityObj_2015-04-12T17:33:02    2015-04-12T17:33:09
```
**範例 2：列出指定日期之間的管道執行**  
下列 `list-runs` 範例使用 `--start-interval` 指定要包含在輸出中的日期。  

```
aws datapipeline list-runs --pipeline-id df-01434553B58A2SHZUKO5 --start-interval 2017-10-07T00:00:00,2017-10-08T00:00:00
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListRuns](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/list-runs.html)。

### `put-pipeline-definition`
<a name="data-pipeline_PutPipelineDefinition_cli_topic"></a>

以下程式碼範例顯示如何使用 `put-pipeline-definition`。

**AWS CLI**  
**上傳管道定義**  
此範例將指定的管道定義上傳至指定的管道：  

```
aws datapipeline put-pipeline-definition --pipeline-id df-00627471SOVYZEXAMPLE --pipeline-definition file://my-pipeline-definition.json
```
下列為範例輸出：  

```
{
  "validationErrors": [],
  "errored": false,
  "validationWarnings": []
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [PutPipelineDefinition](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/put-pipeline-definition.html)。

### `remove-tags`
<a name="data-pipeline_RemoveTags_cli_topic"></a>

以下程式碼範例顯示如何使用 `remove-tags`。

**AWS CLI**  
**從管道移除標籤**  
此範例會從指定的管道移除指定的標籤：  

```
aws datapipeline remove-tags --pipeline-id df-00627471SOVYZEXAMPLE --tag-keys environment
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [RemoveTags](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/datapipeline/remove-tags.html)。