

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

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

# 表達式
<a name="dp-pipeline-expressions"></a>

表達式可讓您在相關物件之間共享一個值。 AWS Data Pipeline Web 服務會在執行時間處理表達式，確保所有表達式都以表達式的值取代。

表達式是以 "\#{" 和 "}" 分隔。您可以在字串合法的任何管道定義物件中使用表達式。如果位置參考了其中一個類型 ID (NAME、TYPE、SPHERE)，則不會評估其值並依原狀使用。

下列表達式會呼叫其中一個 AWS Data Pipeline 函數。如需詳細資訊，請參閱[表達式評估](#dp-datatype-functions)。

```
#{format(myDateTime,'YYYY-MM-dd hh:mm:ss')}
```

## 參考欄位和物件
<a name="dp-pipeline-expressions-reference"></a>

表達式可以使用存在表達式的目前物件欄位，或參考所連結的另一個物件欄位。

位置格式包含建立時間，後面接著物件建立時間，例如 `@S3BackupLocation_2018-01-31T11:05:33`。

 您也可以參考管道定義中指定的確切槽 ID，例如 Amazon S3 備份位置的槽 ID。若要參考位置 ID，請使用 `#{parent.@id}`。

在下列範例中，`filePath` 欄位參考了相同物件中的 `id` 欄位，以形成檔案名稱。`filePath` 得出的值為 "`s3://amzn-s3-demo-bucket/ExampleDataNode.csv`"。

```
{
  "id" : "ExampleDataNode",
  "type" : "S3DataNode",
  "schedule" : {"ref" : "ExampleSchedule"},
  "filePath" : "s3://amzn-s3-demo-bucket/#{parent.@id}.csv",
  "precondition" : {"ref" : "ExampleCondition"},
  "onFail" : {"ref" : "FailureNotify"}
}
```

若要使用存在於參考所連結另一個物件上的欄位，請使用 `node` 關鍵字。此關鍵字只適用於警示和先決條件物件。

繼續進行上一個範例，`SnsAlarm` 中的表達式可以參考 `Schedule` 中的日期和時間範圍，因為 `S3DataNode` 會參考兩者。

 特別是 `FailureNotify` 的 `message` 欄位可以使用 `ExampleSchedule` 的 `@scheduledStartTime` 和 `@scheduledEndTime` 執行時間欄位，因為 `ExampleDataNode` 的 `onFail` 欄位參考 `FailureNotify` 且其 `schedule` 欄位參考 `ExampleSchedule`。

```
{  
    "id" : "FailureNotify",
    "type" : "SnsAlarm",
    "subject" : "Failed to run pipeline component",
    "message": "Error for interval #{node.@scheduledStartTime}..#{node.@scheduledEndTime}.",
    "topicArn":"arn:aws:sns:us-east-1:28619EXAMPLE:ExampleTopic"
},
```

**注意**  
您可以建立包含相依性的管道，例如您管道中相依於其他系統或任務工作的任務。如果您的管道需要特定資源，請使用資料節點和任務的相關先決條件，將這些相依性新增至管道。這可讓您的管道更輕鬆地進行除錯且彈性更高。此外，請盡可能將您的相依性保留在單一管道內，因為跨管道故障診斷並不容易。

## 巢狀表達式
<a name="dp-datatype-nested"></a>

 AWS Data Pipeline 可讓您巢狀值以建立更複雜的表達式。例如，若要執行時間計算 (從 `scheduledStartTime` 減去 30 分鐘)，並格式化結果以用於管道定義，您可以在活動中使用下列表達式：

```
#{format(minusMinutes(@scheduledStartTime,30),'YYYY-MM-dd hh:mm:ss')}
```

 如果表達式為 SnsAlarm 或 Precondition 的一部分，也請使用 `node` 前綴：

```
#{format(minusMinutes(node.@scheduledStartTime,30),'YYYY-MM-dd hh:mm:ss')}
```

## 清單
<a name="dp-datatype-list-function"></a>

您可以評估清單上的表達式和清單上的函數。例如，假設清單定義如下：`"myList":["one","two"]`。如果此清單用於表達式 `#{'this is ' + myList}`，則會評估為 `["this is one", "this is two"]`。如果您有兩個清單，Data Pipeline 最終會在其評估中將其壓平合併。例如，如果 `myList1` 定義為 `[1,2]`，而 `myList2` 定義為 `[3,4]`，則表達式 `[#{myList1}, #{myList2}]` 會評估為 `[1,2,3,4]`。

## 節點表達式
<a name="dp-datatype-node"></a>

 AWS Data Pipeline 在 `SnsAlarm`或 中使用`#{node.*}`表達式，`PreCondition`以回溯參考管道元件的父物件。由於 `SnsAlarm` 和 `PreCondition` 是由不具反向參考的活動或資源參考，因此 `node` 提供方法來參考此參考者。例如，下列管道定義示範故障通知如何使用 `node` 來參考其父系 (在本例中是 `ShellCommandActivity`)，並在 `SnsAlarm` 訊息中包含父系的排程開始和結束時間。ShellCommandActivity 的 scheduledStartTime 參考不需要 `node` 前綴，因為 scheduledStartTime 會自我參考。

**注意**  
前面加上 @ 符號的欄位表示這些欄位是執行時間欄位。

```
{
  "id" : "ShellOut",
  "type" : "ShellCommandActivity",
  "input" : {"ref" : "HourlyData"},
  "command" : "/home/userName/xxx.sh #{@scheduledStartTime} #{@scheduledEndTime}",   
  "schedule" : {"ref" : "HourlyPeriod"},
  "stderr" : "/tmp/stderr:#{@scheduledStartTime}",
  "stdout" : "/tmp/stdout:#{@scheduledStartTime}",
  "onFail" : {"ref" : "FailureNotify"},
},
{  
  "id" : "FailureNotify",
  "type" : "SnsAlarm",
  "subject" : "Failed to run pipeline component",
  "message": "Error for interval #{node.@scheduledStartTime}..#{node.@scheduledEndTime}.",
  "topicArn":"arn:aws:sns:us-east-1:28619EXAMPLE:ExampleTopic"
},
```

AWS Data Pipeline 支援使用者定義欄位的傳輸參考，但不支援執行時間欄位。轉移參考是兩個管道元件之間的參考，需要另一個管道元件做為媒介。下列範例顯示轉移使用者定義欄位的參考和非轉移執行時間欄位的參考，這兩者皆有效。如需詳細資訊，請參閱[使用者定義的欄位](dp-writing-pipeline-definition.md#dp-userdefined-fields)。

```
{
  "name": "DefaultActivity1",
  "type": "CopyActivity",
  "schedule": {"ref": "Once"},
  "input": {"ref": "s3nodeOne"},  
  "onSuccess": {"ref": "action"},
  "workerGroup": "test",  
  "output": {"ref": "s3nodeTwo"}
},
{
  "name": "action",
  "type": "SnsAlarm",
  "message": "S3 bucket '#{node.output.directoryPath}' succeeded at #{node.@actualEndTime}.",
  "subject": "Testing",  
  "topicArn": "arn:aws:sns:us-east-1:28619EXAMPLE:ExampleTopic",
  "role": "DataPipelineDefaultRole"
}
```

## 表達式評估
<a name="dp-datatype-functions"></a>

 AWS Data Pipeline 提供一組函數，您可以用來計算欄位的值。下列範例使用 `makeDate` 函數，將 `Schedule` 物件的 `startDateTime` 欄位設為 `"2011-05-24T0:00:00"` GMT/UTC。

```
"startDateTime" : "makeDate(2011,5,24)"
```