

終止支援通知：在 2025 年 12 月 15 日， AWS 將終止對 的支援 AWS IoT Analytics。2025 年 12 月 15 日之後，您將無法再存取 AWS IoT Analytics 主控台或 AWS IoT Analytics 資源。如需詳細資訊，請參閱[AWS IoT Analytics 終止支援](https://docs.aws.amazon.com/iotanalytics/latest/userguide/iotanalytics-end-of-support.html)。

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

# 管道活動
<a name="pipeline-activities"></a>

最簡單的功能性管道是將頻道連接至資料存放區，成為具有兩個活動的管道：一個 `channel` 活動和一個 `datastore` 活動。您可以新增其他活動至管道，以獲得更強大的訊息處理。

您可以使用 [RunPipelineActivity](https://docs.aws.amazon.com/iotanalytics/latest/APIReference/API_RunPipelineActivity.html) 操作來模擬在您提供的訊息承載上執行管道活動的結果。在開發和偵錯管道活動時，這可能相當實用。[RunPipelineActivity 範例](run-pipeline-activity.md)示範如何使用。

# 頻道活動
<a name="pipeline-activities-channel"></a>

管道中的第一個活動必須是決定要處理之訊息來源`channel`的活動。

```
{
    "channel": {
        "name": "MyChannelActivity",
        "channelName": "mychannel",
        "next": "MyLambdaActivity"
    }
}
```

# 資料存放區活動
<a name="pipeline-activities-datastore"></a>

`datastore` 活動指定已處理資料的儲存位置，這是最後一個活動。

```
{
    "datastore": {
        "name": "MyDatastoreActivity",
        "datastoreName": "mydatastore"
    }
}
```

# AWS Lambda 活動
<a name="pipeline-activities-lambda"></a>

您可以使用**`lambda`活動**對訊息執行複雜的處理。例如，您可以使用來自外部 API 操作輸出的資料來充實訊息，或根據來自 Amazon DynamoDB 的邏輯篩選訊息。不過，在進入資料存放區之前，您無法使用此管道活動來新增其他訊息，或移除現有的訊息。

**`lambda` 活動**中使用的 AWS Lambda 函數必須接收並傳回 JSON 物件陣列。如需範例，請參閱「[Lambda 函數範例 1](#pipeline-activities-lambda-ex1)」。

 若要授予叫用 Lambda 函數的 AWS IoT Analytics 許可，您必須新增政策。例如，執行下列 CLI 命令，並將 *exampleFunctionName* 取代為 Lambda 函數的名稱，將 *123456789012* 取代為 AWS 您的帳戶 ID，並使用呼叫指定 Lambda 函數之管道的 Amazon Resource Name (ARN)。

```
aws lambda add-permission --function-name exampleFunctionName --action lambda:InvokeFunction --statement-id iotanalytics --principal iotanalytics.amazonaws.com --source-account 123456789012 --source-arn arn:aws:iotanalytics:us-east-1:123456789012:pipeline/examplePipeline
```

命令會傳回下列項目：

```
{
    "Statement": "{\"Sid\":\"iotanalyticsa\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"iotanalytics.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:aws-region:aws-account:function:exampleFunctionName\",\"Condition\":{\"StringEquals\":{\"AWS:SourceAccount\":\"123456789012\"},\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:iotanalytics:us-east-1:123456789012:pipeline/examplePipeline\"}}}"
}
```

詳情請參閱 *AWS Lambda 開發人員指南*中的[為 AWS Lambda使用資源型政策](https://docs.aws.amazon.com/lambda/latest/dg/access-control-resource-based.html)。

## Lambda 函數範例 1
<a name="pipeline-activities-lambda-ex1"></a>

在此範例中，Lambda 函數會根據原始訊息中的資料新增資訊。裝置會發佈承載類似下列範例的訊息。

```
{
  "thingid": "00001234abcd",
  "temperature": 26,
  "humidity": 29,
  "location": {
    "lat": 52.4332935,
    "lon": 13.231694
  },
  "ip": "192.168.178.54",
  "datetime": "2018-02-15T07:06:01"
}
```

裝置具有下列管道定義。

```
{
    "pipeline": {
        "activities": [
            {
                "channel": {
                    "channelName": "foobar_channel",
                    "name": "foobar_channel_activity",
                    "next": "lambda_foobar_activity"
                }
            },
            {
                "lambda": {
                    "lambdaName": "MyAnalyticsLambdaFunction",
                    "batchSize": 5,
                    "name": "lambda_foobar_activity",
                    "next": "foobar_store_activity"
                }
            },
            {
                "datastore": {
                    "datastoreName": "foobar_datastore",
                    "name": "foobar_store_activity"
                }
            }
        ],
        "name": "foobar_pipeline",
        "arn": "arn:aws:iotanalytics:eu-west-1:123456789012:pipeline/foobar_pipeline"
    }
}
```

下列 Lambda Python 函數 (`MyAnalyticsLambdaFunction`) 會將 GMaps URL 和以華氏為單位的溫度新增至訊息。

```
import logging
import sys

# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
streamHandler = logging.StreamHandler(stream=sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

def c_to_f(c):
    return 9.0/5.0 * c + 32

def lambda_handler(event, context):
    logger.info("event before processing: {}".format(event))
    maps_url = 'N/A'

    for e in event:
        #e['foo'] = 'addedByLambda'
        if 'location' in e:
            lat = e['location']['lat']
            lon = e['location']['lon']
            maps_url = "http://maps.google.com/maps?q={},{}".format(lat,lon)

        if 'temperature' in e:
            e['temperature_f'] = c_to_f(e['temperature'])

        logger.info("maps_url: {}".format(maps_url))
        e['maps_url'] = maps_url

    logger.info("event after processing: {}".format(event))

    return event
```

## Lambda 函數範例 2
<a name="pipeline-activities-lambda-ex2"></a>

有用的技巧就是壓縮和序列化訊息承載，以降低傳輸和存放成本。在此第二個範例中，Lambda 函數假設訊息承載代表已壓縮的 JSON 原始檔，然後以 base64 編碼 （序列化） 做為字串。它會傳回原始 JSON。

```
import base64
import gzip
import json
import logging
import sys

# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
streamHandler = logging.StreamHandler(stream=sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

def decode_to_bytes(e):
    return base64.b64decode(e)

def decompress_to_string(binary_data):
    return gzip.decompress(binary_data).decode('utf-8')

def lambda_handler(event, context):
    logger.info("event before processing: {}".format(event))

    decompressed_data = []

    for e in event:
        binary_data = decode_to_bytes(e)
        decompressed_string = decompress_to_string(binary_data)

        decompressed_data.append(json.loads(decompressed_string))

    logger.info("event after processing: {}".format(decompressed_data))

    return decompressed_data
```

# AddAttributes 活動
<a name="pipeline-activities-add-attributes"></a>

`addAttributes` 活動根據訊息中的現有屬性來新增屬性。這可讓您在儲存訊息之前變更訊息的形狀。例如，您可以使用 `addAttributes`，標準化來自不同世代之裝置韌體的資料。

請考慮下列輸入訊息。

```
{
    "device": {
        "id": "device-123",
        "coord": [ 47.6152543, -122.3354883 ]
    }
}
```

`addAttributes` 活動如下所示。

```
{
    "addAttributes": {
        "name": "MyAddAttributesActivity",
        "attributes": {
            "device.id": "id",
            "device.coord[0]": "lat",
            "device.coord[1]": "lon"
        },
        "next": "MyRemoveAttributesActivity"
    }
}
```

此活動會將裝置 ID 移至根層級，並擷取`coord`陣列中的值，將它們提升為名為 `lat`和 的頂層屬性`lon`。由於此活動，輸入訊息會轉換為下列範例。

```
{
    "device": {
        "id": "device-123",
        "coord": [ 47.6, -122.3 ]
    },
    "id": "device-123",
    "lat": 47.6,
    "lon": -122.3
}
```

原始裝置屬性仍然存在。您如果想要將其移除，可以使用 `removeAttributes` 活動。

# RemoveAttributes 活動
<a name="pipeline-activities.removeattributes"></a>

`removeAttributes` 活動從訊息移除屬性。例如，假設訊息是`addAttributes`活動的結果。

```
{
    "device": {
        "id": "device-123",
        "coord": [ 47.6, -122.3 ]
    },
    "id": "device-123",
    "lat": 47.6,
    "lon": -122.3
}
```

若要將訊息標準化，使其只包含根層級的必要資料，請使用下列`removeAttributes`活動。

```
{
    "removeAttributes": {
        "name": "MyRemoveAttributesActivity",
        "attributes": [
            "device"
        ],
        "next": "MyDatastoreActivity"
    }
}
```

這會導致下列訊息沿著管道流動。

```
{
    "id": "device-123",
    "lat": 47.6,
    "lon": -122.3
}
```

# SelectAttributes 活動
<a name="pipeline-activities-selectattributes"></a>

`selectAttributes` 活動僅會使用原始訊息的指定屬性來建立新訊息，而其他每個屬性皆會遭捨棄。此外，`selectAttributes` 只會在訊息的根目錄下建立新屬性。因此，假定此訊息：

```
{
    "device": {
        "id": "device-123",
        "coord": [ 47.6152543, -122.3354883 ],
        "temp": 50,
        "hum": 40
    },
    "light": 90
}
```

與此活動：

```
{
    "selectAttributes": {
        "name": "MySelectAttributesActivity",
        "attributes": [
            "device.temp",
            "device.hum",
            "light"
        ],
        "next": "MyDatastoreActivity"
    }
}
```

結果是以下訊息流經管道。

```
{
    "temp": 50,
    "hum": 40,
    "light": 90
}
```

同樣地，`selectAttributes` 只能建立根層級的物件。

# 篩選活動
<a name="pipeline-activities-filter"></a>

`filter` 活動會根據其屬性篩選訊息。此活動中使用的表達式看起來像 SQL `WHERE`子句，必須傳回布林值。

```
{
    "filter": {
        "name": "MyFilterActivity",
        "filter": "temp > 40 AND hum < 20",
        "next": "MyDatastoreActivity"
    }
}
```

# DeviceRegistryEnrich 活動
<a name="pipeline-activities-deviceregistryenrich"></a>

`deviceRegistryEnrich` 活動可讓您將資料從 AWS IoT 裝置登錄檔新增至訊息承載。舉例而言，若為以下的 訊息：

```
{
    "temp": 50,
    "hum": 40,
    "device" {
        "thingName": "my-thing"
    }
}
```

`deviceRegistryEnrich` 活動如下所示：

```
{
    "deviceRegistryEnrich": {
        "name": "MyDeviceRegistryEnrichActivity",
        "attribute": "metadata",
        "thingName": "device.thingName",
        "roleArn": "arn:aws:iam::<your-account-number>:role:MyEnrichRole",
        "next": "MyDatastoreActivity"
    }
}
```

輸出訊息現在看起來像這個範例。

```
{
    "temp" : 50,
    "hum" : 40,
    "device" {
        "thingName" : "my-thing"
    },
    "metadata" : {
        "defaultClientId": "my-thing",
        "thingTypeName": "my-thing",
        "thingArn": "arn:aws:iot:us-east-1:<your-account-number>:thing/my-thing",
        "version": 1,
        "thingName": "my-thing",
        "attributes": {},
        "thingId": "aaabbbccc-dddeeef-gghh-jjkk-llmmnnoopp"
    }
}
```

您必須在活動定義的 `roleArn` 欄位中指定角色，且該角色需連接適當許可。角色必須具有如下所示的許可政策。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:DescribeThing"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:thing/your-thingName"
            ]
        }
    ]
}
```

------

該角色的信任政策則如下所示：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": "iotanalytics.amazonaws.com"
            },
            "Action": [
                "sts:AssumeRole"
            ]
        }
    ]
}
```

------

# DeviceShadowEnrich 活動
<a name="pipeline-activities-deviceshadowenrich"></a>

`deviceShadowEnrich` 活動會將 AWS IoT Device Shadow 服務中的資訊新增至訊息。例如，假定訊息：

```
{
    "temp": 50,
    "hum": 40,
    "device": { "thingName": "my-thing" }
}
```

以及下列 `deviceShadowEnrich` 活動：

```
{
    "deviceShadowEnrich": {
        "name": "MyDeviceShadowEnrichActivity",
        "attribute": "shadow",
        "thingName": "device.thingName",
        "roleArn": "arn:aws:iam::<your-account-number>:role:MyEnrichRole",
        "next": "MyDatastoreActivity"
    }
}
```

結果是類似下列範例的訊息。

```
{
    "temp": 50,
    "hum": 40,
    "device": {
        "thingName": "my-thing"
    },
    "shadow": {
        "state": {
            "desired": {
                "attributeX": valueX, ...
            },
            "reported": {
                "attributeX": valueX, ...
            },
            "delta": {
                "attributeX": valueX, ...
            }
        },
        "metadata": {
            "desired": {
                "attribute1": {
                    "timestamp": timestamp
                }, ...
            },
            "reported": ": {
                "attribute1": {
                    "timestamp": timestamp
                }, ...
            }
        },
        "timestamp": timestamp,
        "clientToken": "token",
        "version": version
    }
}
```

您必須在活動定義的 `roleArn` 欄位中指定角色，且該角色需連接適當許可。角色必須具有如下所示的許可政策。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:GetThingShadow"
            ],
            "Resource": [
            "arn:aws:iot:us-east-1:123456789012:thing/your-thingName"
            ]
        }
    ]
}
```

------

該角色的信任政策則如下所示：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": "iotanalytics.amazonaws.com"
            },
            "Action": [
                "sts:AssumeRole"
            ]
        }
    ]
}
```

------

# 數學活動
<a name="pipeline-activities-math"></a>

`math` 活動會透過訊息的屬性來運算數學表達式。表達式必須傳回數字。舉例而言，假定是以下輸入訊息：

```
{
    "tempF": 50,
}
```

透過以下 `math` 活動處理後：

```
{
    "math": {
        "name": "MyMathActivity",
        "math": "(tempF - 32) / 2",
        "attribute": "tempC",
        "next": "MyDatastoreActivity"
    }
}
```

產生的訊息看起來如下：

```
{
    "tempF" : 50,
    "tempC": 9
}
```

# 數學活動運算子和函數
<a name="math-operators-functions"></a>

您可以在 `math` 活動中使用以下運算子：


|  |  | 
| --- |--- |
|  \$1  |  加法  | 
|  -  |  減法  | 
|  \$1  |  乘法  | 
|  /  |  除法  | 
|  %  |  模數  | 

您可以在 `math` 活動中使用以下函數：
+ [abs(Decimal)](math-abs.md)
+ [acos(Decimal)](math-acos.md)
+ [asin(Decimal)](math-asin.md)
+ [atan(Decimal)](math-atan.md)
+ [atan2(Decimal, Decimal)](math-atan2.md)
+ [ceil(Decimal)](math-ceil.md)
+ [cos(Decimal)](math-cos.md)
+ [cosh(Decimal)](math-cosh.md)
+ [exp(Decimal)](math-exp.md)
+ [ln(Decimal)](math-ln.md)
+ [log(Decimal)](math-log.md)
+ [mod(Decimal, Decimal)](math-mod.md)
+ [power(Decimal, Decimal)](math-power.md)
+ [round(Decimal)](math-round.md)
+ [sign(Decimal)](math-sign.md)
+ [sin(Decimal)](math-sin.md)
+ [sinh(Decimal)](math-sinh.md)
+ [sqrt(Decimal)](math-sqrt.md)
+ [tan(Decimal)](math-tan.md)
+ [tanh(Decimal)](math-tanh.md)
+ [trunc(Decimal，整數）](math-trunc.md)

# abs(Decimal)
<a name="math-abs"></a>

傳回某個數字的絕對值。

範例：`abs(-5)`傳回 5。


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Int`，引數的絕對值。  | 
|  `Decimal`  |  `Decimal`，引數的絕對值  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal`。結果為引數的絕對值。如果字串無法轉換，則結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# acos(Decimal)
<a name="math-acos"></a>

以弧度傳回數字的反餘弦值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `acos(0)` = 1.5707963267948966


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的反向餘弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的反向餘弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` （使用雙精度） 引數的反餘弦。如果字串無法轉換，則結果為 `Undefined`。傳回的虛數結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# asin(Decimal)
<a name="math-asin"></a>

以弧度傳回數字的反正弦值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `asin(0)` = 0.0


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的反向正弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的反向正弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的反向正弦值。如果字串無法轉換，則結果為 `Undefined`。傳回的虛數結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# atan(Decimal)
<a name="math-atan"></a>

以弧度傳回數字的反正切值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `atan(0)` = 0.0


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的反向正切值。傳回的虛數結果為 `Undefined`。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的反向正切值。傳回的虛數結果為 `Undefined`。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的反向正切值。如果字串無法轉換，則結果為 `Undefined`。傳回的虛數結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# atan2(Decimal, Decimal)
<a name="math-atan2"></a>

以弧度傳回 x 軸正軸和兩個引數所定義的 (x, y) 點之間的角度。逆時針角度的角度為正值 （上半平面，y > 0)，順時針角度引`Decimal`數的負值在套用函數之前會四捨五入為雙精確度。

範例：`atan(1, 0)`= 1.5707963267948966


| 引數類型 | 引數類型 | 結果 | 
| --- | --- | --- | 
|  `Int` / `Decimal`  |  `Int` / `Decimal`  |  `Decimal` （使用雙精度），x 軸與指定 (x，y) 點之間的角度  | 
|  `Int` / `Decimal` / `String`  |  `Int` / `Decimal` / `String`  |  `Decimal`，所述之點的反向正切值。如果字串無法轉換，則結果為 `Undefined`。  | 
|  其他值  |  其他值  |  `Undefined`.  | 

# ceil(Decimal)
<a name="math-ceil"></a>

將指定的 `Decimal` 無條件進位至最近的 `Int`。

範例：

`ceil(1.2)` = 2

`ceil(11.2)` = -1


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Int`，引數值。  | 
|  `Decimal`  |  `Int`，字串會轉換為 `Decimal`，並四捨五入至最接近的 `Int`。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  其他值  |  `Undefined`.  | 

# cos(Decimal)
<a name="math-cos"></a>

以弧度傳回數字的餘弦值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `cos(0)` = 1


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的餘弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的餘弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的餘弦值。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。傳回的虛數結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# cosh(Decimal)
<a name="math-cosh"></a>

以弧度傳回數字的雙曲餘弦值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `cosh(2.3)` = 5.037220649268761


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的雙曲餘弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的雙曲餘弦值。傳回的虛數結果為 `Undefined`。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的雙曲餘弦值。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。傳回的虛數結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# exp(Decimal)
<a name="math-exp"></a>

傳回`e`增加到十進位引數。 `Decimal`引數在套用函數之前會四捨五入到雙精確度。

範例： `exp(1)` = 1


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` （具有雙精度）、e^argument。  | 
|  `Decimal`  |  `Decimal` （使用雙精度）、e^argument  | 
|  `String`  |  `Decimal` （具有雙精度）、e^argument。如果 `String` 無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  其他值  |  `Undefined`.  | 

# ln(Decimal)
<a name="math-ln"></a>

傳回引數的自然對數。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `ln(e)` = 1


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的自然對數。  | 
|  `Decimal`  |  `Decimal` （使用雙精度），引數的自然日誌  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的自然對數。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# log(Decimal)
<a name="math-log"></a>

傳回引數以 10 為底的對數。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `log(100)` = 2.0


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數以 10 為底的對數。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數以 10 為底的對數。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數以 10 為底的對數。如果 `String` 無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  Array  |  `Undefined`.  | 
|  物件  |  `Undefined`.  | 
|  Null  |  `Undefined`.  | 
|  未定義  |  `Undefined`.  | 

# mod(Decimal, Decimal)
<a name="math-mod"></a>

傳回第二個引數中第一個引數分割的剩餘部分。您也可以使用 `%`做為相同模數功能的固定運算子。

範例： `mod(8, 3)` = 2


| 左運算元 | 右運算元 | 輸出 | 
| --- | --- | --- | 
|  `Int`  |  `Int`  |  `Int`，第二個引數的第一個引數模數。  | 
|  `Int` / `Decimal`  |  `Int` / `Decimal`  |  `Decimal`，第二個引數的第一個引數模數。  | 
|  `String` / `Int` / `Decimal`  |  `String` / `Int` / `Decimal`  |  如果所有字串都轉換為 `Decimals`，則第一個引數模數為第二個引數時的結果。否則為 `Undefined`。  | 
|  其他值  |  其他值  |  `Undefined`.  | 

# power(Decimal, Decimal)
<a name="math-power"></a>

傳回第一個引數次方的第二個引數值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `power(2, 5)` = 32.0


| 引數類型 1 | 引數類型 2 | 輸出 | 
| --- | --- | --- | 
|  `Int` / `Decimal`  |  `Int` / `Decimal`  |  `Decimal` (使用雙精度)，第一個引數次方的第二個引數值。  | 
|  `Int` / `Decimal` / `String`  |  `Int` / `Decimal` / `String`  |  `Decimal` (使用雙精度)，第一個引數次方的第二個引數值。任何字串都會轉換為 `Decimals`。如果任何 `String` 無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  其他值  |  其他值  |  `Undefined`.  | 

# round(Decimal)
<a name="math-round"></a>

將指定的 `Decimal` 無條件進位至最接近的 `Int`。如果 `Decimal` 與兩個 `Int` 值 (例如，0.5) 等距，則 `Decimal` 會無條件進位。

範例：

`Round(1.2)` = 1

`Round(1.5)` = 2

`Round(1.7)` = 2

`Round(-1.1)` = -1

`Round(-1.5)` = -2


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  引數  | 
|  `Decimal`  |  `Decimal` 是要向下捨入到最接近的 `Int`。  | 
|  `String`  |  `Decimal` 是要向下捨入到最接近的 `Int`。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  其他值  |  `Undefined`.  | 

# sign(Decimal)
<a name="math-sign"></a>

傳回所給數字的符號。當引數的符號為正值時，傳回 1。當引數的符號為負值時，傳回 -1。如果引數為 0，傳回 0。

範例：

`sign(-7)` = -1

`sign(0)` = 0

`sign(13)` = 1


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Int`，`Int` 值的符號。  | 
|  `Decimal`  |  `Int`，`Decimal` 值的符號。  | 
|  `String`  |  `Int`，`Decimal` 值的符號。如果 轉換為`Decimal`值，則會傳回`Decimal`值的符號。如果 `String` 無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  其他值  |  `Undefined`.  | 

# sin(Decimal)
<a name="math-sin"></a>

以弧度傳回數字的正弦值。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `sin(0)` = 0.0


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的正弦值。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的正弦值。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal`，引數的正弦。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  `Array`  |  `Undefined`.  | 
|  `Object`  |  `Undefined`.  | 
|  `Null`  |  `Undefined`.  | 
|  `Undefined`  |  `Undefined`.  | 

# sinh(Decimal)
<a name="math-sinh"></a>

以弧度傳回數字的雙曲正弦值。`Decimal` 值在套用函數前會四捨五入至雙精度。結果為雙精度的 `Decimal` 值。

範例： `sinh(2.3)` = 4.936961805545957


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的雙曲正弦值。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的雙曲正弦值。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal`，引數的雙曲正弦。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  `Array`  |  `Undefined`.  | 
|  `Object`  |  `Undefined`.  | 
|  `Null`  |  `Undefined`.  | 
|  `Undefined`  |  `Undefined`.  | 

# sqrt(Decimal)
<a name="math-sqrt"></a>

傳回數字的平方根。`Decimal` 引數在套用函數前會四捨五入至雙精度。

範例： `sqrt(9)` = 3.0


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  引數的平方根。  | 
|  `Decimal`  |  引數的平方根。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  引數的平方根。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  `Array`  |  `Undefined`.  | 
|  `Object`  |  `Undefined`.  | 
|  `Null`  |  `Undefined`.  | 
|  `Undefined`  |  `Undefined`.  | 

# tan(Decimal)
<a name="math-tan"></a>

以弧度傳回數字的正切值。`Decimal` 值在套用函數前會四捨五入至雙精度。

範例： `tan(3)` = -0.1425465430742778


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的正切值。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的正切值。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的正切值。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  `Array`  |  `Undefined`.  | 
|  `Object`  |  `Undefined`.  | 
|  `Null`  |  `Undefined`.  | 
|  `Undefined`  |  `Undefined`.  | 

# tanh(Decimal)
<a name="math-tanh"></a>

以弧度傳回數字的雙曲正切值。`Decimal` 值在套用函數前會四捨五入至雙精度。

範例： `tanh(2.3)` = 0.9800963962661914


| 引數類型 | 結果 | 
| --- | --- | 
|  `Int`  |  `Decimal` (使用雙精度)，引數的雙曲正切值。  | 
|  `Decimal`  |  `Decimal` (使用雙精度)，引數的雙曲正切值。  | 
|  `Boolean`  |  `Undefined`.  | 
|  `String`  |  `Decimal` (使用雙精度)，引數的雙曲正切值。如果字串無法轉換為 `Decimal`，則結果為 `Undefined`。  | 
|  `Array`  |  `Undefined`.  | 
|  `Object`  |  `Undefined`.  | 
|  `Null`  |  `Undefined`.  | 
|  `Undefined`  |  `Undefined`.  | 

# trunc(Decimal，整數）
<a name="math-trunc"></a>

將第一個引數截為第二的引數所指定的 `Decimal` 位數。如果第二個引數小於零，則會設定為零。如果第二個引數大於 34，則會設定為 34。追蹤零會從結果中分割。

範例：

`trunc(2.3, 0)` = 2

`trunc(2.3123, 2)` = 2.31

`trunc(2.888, 2)` = 2.88

`trunc(2.00, 5)` = 2


| 引數類型 1 | 引數類型 2 | 結果 | 
| --- | --- | --- | 
|  `Int`  |  `Int`  |  來源值。  | 
|  `Int` / `Decimal` / `String`  |  `Int` / `Decimal`  |  第一個引數會截短為第二個引數所指定的長度。第二個引數若非 `Int`，會無條件捨去至最接近的 `Int`。字串會轉換為`Decimal`值。如果字串轉換失敗，則結果為 `Undefined`。  | 
|  其他值  |    |  未定義。  | 

# RunPipelineActivity
<a name="run-pipeline-activity"></a>

以下是如何使用 `RunPipelineActivity`命令來測試管道活動的範例。在此範例中，我們會測試數學活動。

1. 建立 `maths.json` 檔案，其中包含您要測試之管道活動的定義。

   ```
   {
       "math": {
           "name": "MyMathActivity",
           "math": "((temp - 32) * 5.0) / 9.0",
           "attribute": "tempC"
       }
   }
   ```

1. 建立 `payloads.json` 檔案，其中包含用於測試管道活動的範例承載。

   ```
   [
       "{\"humidity\": 52, \"temp\": 68 }",
       "{\"humidity\": 52, \"temp\": 32 }"
   ]
   ```

1. 從命令列呼叫 `RunPipelineActivities`操作。

   ```
   aws iotanalytics run-pipeline-activity  --pipeline-activity file://maths.json  --payloads file://payloads.json --cli-binary-format raw-in-base64-out
   ```

   這會產生下列結果。

   ```
   {
       "logResult": "",
       "payloads": [
           "eyJodW1pZGl0eSI6NTIsInRlbXAiOjY4LCJ0ZW1wQyI6MjB9",
           "eyJodW1pZGl0eSI6NTIsInRlbXAiOjMyLCJ0ZW1wQyI6MH0="
       ]
   }
   ```

   結果中列出的承載是 Base64-encoded字串。當這些字串解碼時，您會得到以下結果。

   ```
   {"humidity":52,"temp":68,"tempC":20}
   {"humidity":52,"temp":32,"tempC":0}
   ```