

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

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

# 管道表達式和函數
<a name="dp-expressions-functions"></a>

本節說明在管道中使用表達式和函數的語法，包括相關資料類型。

## 簡單資料類型
<a name="dp-pipeline-datatypes"></a>

您可以將以下類型的資料設為欄位值。

**Topics**
+ [DateTime](#dp-datatype-datetime)
+ [數值](#dp-datatype-numeric)
+ [物件參考](#dp-datatype-object-reference)
+ [Period](#dp-datatype-period)
+ [String](#dp-datatype-section)

### DateTime
<a name="dp-datatype-datetime"></a>

 AWS Data Pipeline 僅支援 UTC/GMT 格式的「YYYY-MM-DDTHH：MM：SS」日期和時間。下列範例會將 `Schedule` 物件的 `startDateTime` 欄位設為 UTC/GMT 時區的 `1/15/2012, 11:59 p.m.`。

```
"startDateTime" : "2012-01-15T23:59:00"
```

### 數值
<a name="dp-datatype-numeric"></a>

 AWS Data Pipeline 同時支援整數和浮點值。

### 物件參考
<a name="dp-datatype-object-reference"></a>

管道定義中的物件。這可以是目前物件、在管道的其他位置定義的物件名稱，或在欄位中列出目前物件的物件，並以 `node` 關鍵字參考。如需 `node` 的相關資訊，請參閱 [參考欄位和物件](dp-pipeline-expressions.md#dp-pipeline-expressions-reference)。如需管道物件類型的詳細資訊，請參閱[管道物件參考](dp-pipeline-objects.md)。

### Period
<a name="dp-datatype-period"></a>

 表示排程事件的執行頻率。這會以 "*N* [`years`\$1`months`\$1`weeks`\$1`days`\$1`hours`\$1`minutes`]" 格式表示，其中 *N* 是正整數值。

最短期間為 15 分鐘，而最長期間為 3 年。

下列範例會將 `Schedule` 物件的 `period` 欄位設為 3 小時。這會建立每隔三小時執行一次的排程。

```
"period" : "3 hours"
```

### String
<a name="dp-datatype-section"></a>

 標準字串值。字串必須以雙引號 (") 括住。您可以使用反斜線字元 (\$1) 來逸出字串中的字元。不支援多行字串。

下列範例示範 `id` 欄位的有效字串值。

```
"id" : "My Data Object"

"id" : "My \"Data\" Object"
```

字串也可以包含評估為字串值的表達式。這些表達式會插入字串，並以 "\$1\$1" 和 "\$1" 分隔。下列範例使用表達式來將目前物件的名稱插入路徑。

```
"filePath" : "s3://amzn-s3-demo-bucket/#{name}.csv"
```

如需使用表達式的詳細資訊，請參閱[參考欄位和物件](dp-pipeline-expressions.md#dp-pipeline-expressions-reference)和[表達式評估](dp-pipeline-expressions.md#dp-datatype-functions)。

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

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

表達式是以 "\$1\$1" 和 "\$1" 分隔。您可以在字串合法的任何管道定義物件中使用表達式。如果位置參考了其中一個類型 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)"
```

# 數學函數
<a name="dp-pipeline-reference-functions-math"></a>

下列函數可用於處理數值。


****  

| 函式 | Description | 
| --- | --- | 
|  \$1  |  加法。 範例：`#{1 + 2}` 結果：`3`  | 
|  -  |  減法。 範例：`#{1 - 2}` 結果：`-1`  | 
|  \$1  |  乘法。 範例：`#{1 * 2}` 結果：`2`  | 
|  /  |  除法。如果您將兩個整數相除，結果會捨去小數部分。 範例：`#{1 / 2}`，結果：`0` 範例：`#{1.0 / 2}`，結果：`.5`  | 
|  ^  |  指數。 範例：`#{2 ^ 2}` 結果：`4.0`  | 

# 字串函數
<a name="dp-pipeline-reference-functions-string"></a>

 下列函數可用於處理字串值。


****  

| 函式 | Description | 
| --- | --- | 
|  \$1  |  串連。非字串值會先轉換成字串。 範例：`#{"hel" + "lo"}` 結果：`"hello"`  | 

# 日期和時間函數
<a name="dp-pipeline-reference-functions-datetime"></a>

 下列函數可用於處理 DateTime 值。例如，`myDateTime` 的值為 `May 24, 2011 @ 5:10 pm GMT`。

**注意**  
的日期/時間格式 AWS Data Pipeline 是 Joda Time，這是 Java 日期和時間類別的替代項目。如需詳細資訊，請參閱 [Joda Time - Class DateTimeFormat](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html)。


****  

| 函式 | Description | 
| --- | --- | 
|  `int day(DateTime myDateTime)`  |  取得 DateTime 值的日 (以整數表示)。 範例：`#{day(myDateTime)}` 結果：`24`  | 
|  `int dayOfYear(DateTime myDateTime)`  |  取得 DateTime 值的年度日 (以整數表示)。 範例：`#{dayOfYear(myDateTime)}` 結果：`144`  | 
|  `DateTime firstOfMonth(DateTime myDateTime)`  |  在指定的 DateTime 中建立月份起始的 DateTime 物件。 範例：`#{firstOfMonth(myDateTime)}` 結果：`"2011-05-01T17:10:00z"`  | 
|  `String format(DateTime myDateTime,String format)`  |  建立字串物件，這是使用指定格式字串轉換指定 DateTime 的結果。 範例：`#{format(myDateTime,'YYYY-MM-dd HH:mm:ss z')}` 結果：`"2011-05-24T17:10:00 UTC"`  | 
|  `int hour(DateTime myDateTime)`  |  取得 DateTime 值的小時 (以整數表示)。 範例：`#{hour(myDateTime)}` 結果：`17`  | 
|  `DateTime makeDate(int year,int month,int day)`  |  使用指定的年、月和日，建立自午夜起採用 UTC 的 DateTime 物件。 範例：`#{makeDate(2011,5,24)}` 結果：`"2011-05-24T0:00:00z"`  | 
|  `DateTime makeDateTime(int year,int month,int day,int hour,int minute)`  |  使用指定的年、月、日、小時和分鐘，建立採用 UTC 的 DateTime 物件。 範例：`#{makeDateTime(2011,5,24,14,21)}` 結果：`"2011-05-24T14:21:00z"`  | 
|  `DateTime midnight(DateTime myDateTime)`  |  建立相對於指定 DateTime，目前午夜的 DateTime 物件。例如，`MyDateTime` 為 `2011-05-25T17:10:00z`，結果如下： 範例：`#{midnight(myDateTime)}` 結果：`"2011-05-25T0:00:00z"`  | 
|  `DateTime minusDays(DateTime myDateTime,int daysToSub)`  |  建立 DateTime 物件，這是從指定 DateTime 減去指定天數的結果。 範例：`#{minusDays(myDateTime,1)}` 結果：`"2011-05-23T17:10:00z"`  | 
|  `DateTime minusHours(DateTime myDateTime,int hoursToSub)`  |  建立 DateTime 物件，這是從指定 DateTime 減去指定時數的結果。 範例：`#{minusHours(myDateTime,1)}` 結果：`"2011-05-24T16:10:00z"`  | 
|  `DateTime minusMinutes(DateTime myDateTime,int minutesToSub)`  |  建立 DateTime 物件，這是從指定 DateTime 減去指定分鐘數的結果。 範例：`#{minusMinutes(myDateTime,1)}` 結果：`"2011-05-24T17:09:00z"`  | 
|  `DateTime minusMonths(DateTime myDateTime,int monthsToSub)`  |  建立 DateTime 物件，這是從指定 DateTime 減去指定月數的結果。 範例：`#{minusMonths(myDateTime,1)}` 結果：`"2011-04-24T17:10:00z"`  | 
|  `DateTime minusWeeks(DateTime myDateTime,int weeksToSub)`  |  建立 DateTime 物件，這是從指定 DateTime 減去指定週數的結果。 範例：`#{minusWeeks(myDateTime,1)}` 結果：`"2011-05-17T17:10:00z"`  | 
|  `DateTime minusYears(DateTime myDateTime,int yearsToSub)`  |  建立 DateTime 物件，這是從指定 DateTime 減去指定年數的結果。 範例：`#{minusYears(myDateTime,1)}` 結果：`"2010-05-24T17:10:00z"`  | 
|  `int minute(DateTime myDateTime)`  |  取得 DateTime 值的分鐘 (以整數表示)。 範例：`#{minute(myDateTime)}` 結果：`10`  | 
|  `int month(DateTime myDateTime)`  |  取得 DateTime 值的月 (以整數表示)。 範例：`#{month(myDateTime)}` 結果：`5`  | 
|  `DateTime plusDays(DateTime myDateTime,int daysToAdd)`  |  建立 DateTime 物件，這是將指定天數加上指定 DateTime 的結果。 範例：`#{plusDays(myDateTime,1)}` 結果：`"2011-05-25T17:10:00z"`  | 
|  `DateTime plusHours(DateTime myDateTime,int hoursToAdd)`  |  建立 DateTime 物件，這是將指定時數加上指定 DateTime 的結果。 範例：`#{plusHours(myDateTime,1)}` 結果：`"2011-05-24T18:10:00z"`  | 
|  `DateTime plusMinutes(DateTime myDateTime,int minutesToAdd)`  |  建立 DateTime 物件，這是將指定分鐘數加上指定 DateTime 的結果。 範例：`#{plusMinutes(myDateTime,1)}` 結果：`"2011-05-24 17:11:00z"`  | 
|  `DateTime plusMonths(DateTime myDateTime,int monthsToAdd)`  |  建立 DateTime 物件，這是將指定月數加上指定 DateTime 的結果。 範例：`#{plusMonths(myDateTime,1)}` 結果：`"2011-06-24T17:10:00z"`  | 
|  `DateTime plusWeeks(DateTime myDateTime,int weeksToAdd)`  |  建立 DateTime 物件，這是將指定週數加上指定 DateTime 的結果。 範例：`#{plusWeeks(myDateTime,1)}` 結果：`"2011-05-31T17:10:00z"`  | 
|  `DateTime plusYears(DateTime myDateTime,int yearsToAdd)`  |  建立 DateTime 物件，這是將指定年數加上指定 DateTime 的結果。 範例：`#{plusYears(myDateTime,1)}` 結果：`"2012-05-24T17:10:00z"`  | 
|  `DateTime sunday(DateTime myDateTime)`  |  建立相對於指定 DateTime，上週日的 DateTime 物件。如果指定的 DateTime 為星期日，結果為指定的 DateTime。 範例：`#{sunday(myDateTime)}` 結果：`"2011-05-22 17:10:00 UTC"`  | 
|  `int year(DateTime myDateTime)`  |  取得 DateTime 值的年 (以整數表示)。 範例：`#{year(myDateTime)}` 結果：`2011`  | 
|  `DateTime yesterday(DateTime myDateTime)`  |  建立相對於指定 DateTime，昨天的 DateTime 物件。結果與 minusDays(1) 相同。 範例：`#{yesterday(myDateTime)}` 結果：`"2011-05-23T17:10:00z"`  | 

# 特殊字元
<a name="dp-pipeline-characters"></a>

AWS Data Pipeline 使用管道定義中具有特殊意義的特定字元，如下表所示。


****  

| 特殊字元 | Description | 範例 | 
| --- | --- | --- | 
| @ | 執行時間欄位。此字元是欄位的欄位名稱前綴，只能在管道執行時使用。 | @actualStartTime @failureReason @resourceStatus | 
| \$1 | 表達式。運算式以 "\$1\$1" 和 "\$1" 分隔，括號的內容則由 評估 AWS Data Pipeline。如需詳細資訊，請參閱[表達式](dp-pipeline-expressions.md)。 | \$1\$1format(myDateTime,'YYYY-MM-dd hh:mm:ss')\$1 s3：//amzn-s3-demo-bucket/\$1\$1id\$1.csv | 
| \$1 | 加密欄位。此字元是欄位名稱字首，表示 AWS Data Pipeline 應該加密主控台或 CLI 與 AWS Data Pipeline 服務之間傳輸的此欄位內容。 | \$1password | 