本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
建立使用串流管理員的自訂元件
在自訂 Greengrass 元件中使用串流管理員來儲存、處理和匯出 IoT 裝置資料。使用本節中的程序和範例,建立可搭配串流管理員使用的元件方法、成品和應用程式。如需如何開發和測試元件的詳細資訊,請參閱建立 AWS IoT Greengrass 元件。
定義使用流管理器的組件配方
若要在自訂元件中使用串流管理員,您必須將aws.greengrass.StreamManager
元件定義為相依性。您也必須提供「串流管理員」SDK。完成下列任務,以您選擇的語言下載並使用「串流管理員SDK」。
Java 的串流管理員SDK可做為JAR檔案使用,您可以用來編譯元件。然後,您可以建立包含串流管理員JAR的應用程式SDK、將應用程式定義JAR為元件人工因素,並在元件生命週期JAR中執行應用程式。
若要使用 Java 的串流管SDK理員
-
下載 Java JAR 文件的流管理器SDK。
-
執行下列其中一項動作,從 Java 應用程式和「串流管理員」SDK JAR 檔案建立元件成品:
您的組件配方可能如下列範例所示。此元件會執行 StreamManagerS3.java 範例的修改版本,其中StreamManagerS3.jar
包含「串流管理員」SDK JAR。
- JSON
-
{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.example.StreamManagerS3Java",
"ComponentVersion": "1.0.0",
"ComponentDescription": "Uses stream manager to upload a file to an S3 bucket.",
"ComponentPublisher": "Amazon",
"ComponentDependencies": {
"aws.greengrass.StreamManager": {
"VersionRequirement": "^2.0.0"
}
},
"Manifests": [
{
"Lifecycle": {
"run": "java -jar {artifacts:path}/StreamManagerS3.jar"
},
"Artifacts": [
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Java/1.0.0/StreamManagerS3.jar"
}
]
}
]
}
- YAML
-
---
RecipeFormatVersion: '2020-01-25'
ComponentName: com.example.StreamManagerS3Java
ComponentVersion: 1.0.0
ComponentDescription: Uses stream manager to upload a file to an S3 bucket.
ComponentPublisher: Amazon
ComponentDependencies:
aws.greengrass.StreamManager:
VersionRequirement: "^2.0.0"
Manifests:
- Lifecycle:
run: java -jar {artifacts:path}/StreamManagerS3.jar
Artifacts:
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Java/1.0.0/StreamManagerS3.jar
如需如何開發和測試元件的詳細資訊,請參閱建立 AWS IoT Greengrass 元件。
Python 的串流管理員SDK可做為原始程式碼使用,您可以包含在元件中。建立串流管理員的ZIP檔案SDK、將ZIP檔案定義為元件加工品,然後SDK在元件生命週期中安裝需求。
若要使用 Python 的串流管SDK理員
-
克隆或下載 aws-greengrass-stream-manager-sdk- 蟒蛇存儲庫。
git clone git@github.com:aws-greengrass/aws-greengrass-stream-manager-sdk-python.git
-
創建一個包含ZIP文件stream_manager
夾的文件,其中包含 Python 的流管理器SDK的源代碼。您可以提供此ZIP檔案做為 AWS IoT Greengrass 核心軟體在安裝元件時解壓縮的元件加工品。請執行下列操作:
-
開啟包含您在上一個步驟中複製或下載的存放庫的資料夾。
cd aws-greengrass-stream-manager-sdk-python
-
將stream_manager
資料夾壓縮到名為的ZIP檔案中stream_manager_sdk.zip
。
- Linux or Unix
-
zip -rv stream_manager_sdk.zip stream_manager
- Windows Command Prompt (CMD)
-
tar -acvf stream_manager_sdk.zip stream_manager
- PowerShell
-
Compress-Archive stream_manager stream_manager_sdk.zip
-
確認stream_manager_sdk.zip
檔案包含資stream_manager
料夾及其內容。執行下列命令以列出ZIP檔案的內容。
- Linux or Unix
-
unzip -l stream_manager_sdk.zip
- Windows Command Prompt (CMD)
-
tar -tf stream_manager_sdk.zip
輸出應看起來如下列內容。
Archive: aws-greengrass-stream-manager-sdk-python/stream_manager.zip
Length Date Time Name
--------- ---------- ----- ----
0 02-24-2021 20:45 stream_manager/
913 02-24-2021 20:45 stream_manager/__init__.py
9719 02-24-2021 20:45 stream_manager/utilinternal.py
1412 02-24-2021 20:45 stream_manager/exceptions.py
1004 02-24-2021 20:45 stream_manager/util.py
0 02-24-2021 20:45 stream_manager/data/
254463 02-24-2021 20:45 stream_manager/data/__init__.py
26515 02-24-2021 20:45 stream_manager/streammanagerclient.py
--------- -------
294026 8 files
-
將串流管理員SDK成品複製到元件的成品資料夾。除了串流管理員SDKZIP檔案之外,您的元件還使用SDK的requirements.txt
檔案來安裝串流管理員的相依性SDK。Replace (取代) ~/greengrass-components
與您用於本地開發的文件夾的路徑。
- Linux or Unix
-
cp {stream_manager_sdk.zip,requirements.txt} ~/greengrass-components
/artifacts/com.example.StreamManagerS3Python/1.0.0/
- Windows Command Prompt (CMD)
-
robocopy . %USERPROFILE%\greengrass-components
\artifacts\com.example.StreamManagerS3Python\1.0.0 stream_manager_sdk.zip
robocopy . %USERPROFILE%\greengrass-components
\artifacts\com.example.StreamManagerS3Python\1.0.0 requirements.txt
- PowerShell
-
cp .\stream_manager_sdk.zip,.\requirements.txt ~\greengrass-components
\artifacts\com.example.StreamManagerS3Python\1.0.0\
-
建立您的元件配方。在配方中,執行以下操作:
-
定義stream_manager_sdk.zip
並requirements.txt
作為人工因素。
-
將您的 Python 應用程式定義為成品。
-
在安裝生命週期中,從中安裝串流管理員SDK需求requirements.txt
。
-
在執行生命週期中,將串流管理員附加SDK至PYTHONPATH
,然後執行您的 Python 應用程式。
您的組件配方可能如下列範例所示。此元件會執行 stream_manager_s3.py 範例。
- JSON
-
{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.example.StreamManagerS3Python",
"ComponentVersion": "1.0.0",
"ComponentDescription": "Uses stream manager to upload a file to an S3 bucket.",
"ComponentPublisher": "Amazon",
"ComponentDependencies": {
"aws.greengrass.StreamManager": {
"VersionRequirement": "^2.0.0"
}
},
"Manifests": [
{
"Platform": {
"os": "linux"
},
"Lifecycle": {
"install": "pip3 install --user -r {artifacts:path}/requirements.txt",
"run": "export PYTHONPATH=$PYTHONPATH:{artifacts:decompressedPath}/stream_manager_sdk; python3 {artifacts:path}/stream_manager_s3.py"
},
"Artifacts": [
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_sdk.zip",
"Unarchive": "ZIP"
},
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_s3.py"
},
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/requirements.txt"
}
]
},
{
"Platform": {
"os": "windows"
},
"Lifecycle": {
"install": "pip3 install --user -r {artifacts:path}/requirements.txt",
"run": "set \"PYTHONPATH=%PYTHONPATH%;{artifacts:decompressedPath}/stream_manager_sdk\" & py -3 {artifacts:path}/stream_manager_s3.py"
},
"Artifacts": [
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_sdk.zip",
"Unarchive": "ZIP"
},
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_s3.py"
},
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/requirements.txt"
}
]
}
]
}
- YAML
-
---
RecipeFormatVersion: '2020-01-25'
ComponentName: com.example.StreamManagerS3Python
ComponentVersion: 1.0.0
ComponentDescription: Uses stream manager to upload a file to an S3 bucket.
ComponentPublisher: Amazon
ComponentDependencies:
aws.greengrass.StreamManager:
VersionRequirement: "^2.0.0"
Manifests:
- Platform:
os: linux
Lifecycle:
install: pip3 install --user -r {artifacts:path}/requirements.txt
run: |
export PYTHONPATH=$PYTHONPATH:{artifacts:decompressedPath}/stream_manager_sdk
python3 {artifacts:path}/stream_manager_s3.py
Artifacts:
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_sdk.zip
Unarchive: ZIP
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_s3.py
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/requirements.txt
- Platform:
os: windows
Lifecycle:
install: pip3 install --user -r {artifacts:path}/requirements.txt
run: |
set "PYTHONPATH=%PYTHONPATH%;{artifacts:decompressedPath}/stream_manager_sdk"
py -3 {artifacts:path}/stream_manager_s3.py
Artifacts:
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_sdk.zip
Unarchive: ZIP
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/stream_manager_s3.py
- URI: s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3Python/1.0.0/requirements.txt
如需如何開發和測試元件的詳細資訊,請參閱建立 AWS IoT Greengrass 元件。
的「串流管理員SDK」可做為原始程式碼使用,您可以將其包含在元件中。 JavaScript 建立串流管理員的ZIP檔案SDK、將ZIP檔案定義為元件加工品,然後在元件生命週期SDK中安裝。
若要使用串流管理SDK員 JavaScript
-
克隆或下載 aws-greengrass-stream-manager-sdk- js 存儲庫。
git clone git@github.com:aws-greengrass/aws-greengrass-stream-manager-sdk-js.git
-
建立包含aws-greengrass-stream-manager-sdk
資料夾的ZIP檔案,其中包含「串流管理員SDK」的原始程式碼 JavaScript。您可以提供此ZIP檔案做為 AWS IoT Greengrass 核心軟體在安裝元件時解壓縮的元件加工品。請執行下列操作:
-
開啟包含您在上一個步驟中複製或下載的存放庫的資料夾。
cd aws-greengrass-stream-manager-sdk-js
-
將aws-greengrass-stream-manager-sdk
資料夾壓縮到名為的ZIP檔案中stream-manager-sdk.zip
。
- Linux or Unix
-
zip -rv stream-manager-sdk.zip aws-greengrass-stream-manager-sdk
- Windows Command Prompt (CMD)
-
tar -acvf stream-manager-sdk.zip aws-greengrass-stream-manager-sdk
- PowerShell
-
Compress-Archive aws-greengrass-stream-manager-sdk stream-manager-sdk.zip
-
確認stream-manager-sdk.zip
檔案包含資aws-greengrass-stream-manager-sdk
料夾及其內容。執行下列命令以列出ZIP檔案的內容。
- Linux or Unix
-
unzip -l stream-manager-sdk.zip
- Windows Command Prompt (CMD)
-
tar -tf stream-manager-sdk.zip
輸出應看起來如下列內容。
Archive: stream-manager-sdk.zip
Length Date Time Name
--------- ---------- ----- ----
0 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/
369 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/package.json
1017 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/util.js
8374 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/utilInternal.js
1937 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/exceptions.js
0 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/data/
353343 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/data/index.js
22599 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/client.js
216 02-24-2021 22:36 aws-greengrass-stream-manager-sdk/index.js
--------- -------
387855 9 files
-
將串流管理員加SDK工品複製到元件的成品資料夾。Replace (取代) ~/greengrass-components
與您用於本地開發的文件夾的路徑。
- Linux or Unix
-
cp stream-manager-sdk.zip ~/greengrass-components
/artifacts/com.example.StreamManagerS3JS/1.0.0/
- Windows Command Prompt (CMD)
-
robocopy . %USERPROFILE%\greengrass-components
\artifacts\com.example.StreamManagerS3JS\1.0.0 stream-manager-sdk.zip
- PowerShell
-
cp .\stream-manager-sdk.zip ~\greengrass-components
\artifacts\com.example.StreamManagerS3JS\1.0.0\
-
建立您的元件配方。在配方中,執行以下操作:
-
定義stream-manager-sdk.zip
為人工因素。
-
將您的 JavaScript 應用程式定義為成品。
-
在安裝生命週期中,SDK從stream-manager-sdk.zip
成品安裝串流管理員。這個npm
install
命令會建立包node_modules
含串流管理員SDK及其相依性的資料夾。
-
在執行生命週期中,將node_modules
資料夾附加至NODE_PATH
,然後執行 JavaScript 應用程式。
您的組件配方可能如下列範例所示。此元件執行 StreamManagerS3 範例。
- JSON
-
{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.example.StreamManagerS3JS",
"ComponentVersion": "1.0.0",
"ComponentDescription": "Uses stream manager to upload a file to an S3 bucket.",
"ComponentPublisher": "Amazon",
"ComponentDependencies": {
"aws.greengrass.StreamManager": {
"VersionRequirement": "^2.0.0"
}
},
"Manifests": [
{
"Platform": {
"os": "linux"
},
"Lifecycle": {
"install": "npm install {artifacts:decompressedPath}/stream-manager-sdk/aws-greengrass-stream-manager-sdk",
"run": "export NODE_PATH=$NODE_PATH:{work:path}/node_modules; node {artifacts:path}/index.js"
},
"Artifacts": [
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3JS/1.0.0/stream-manager-sdk.zip",
"Unarchive": "ZIP"
},
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3JS/1.0.0/index.js"
}
]
},
{
"Platform": {
"os": "windows"
},
"Lifecycle": {
"install": "npm install {artifacts:decompressedPath}/stream-manager-sdk/aws-greengrass-stream-manager-sdk",
"run": "set \"NODE_PATH=%NODE_PATH%;{work:path}/node_modules\" & node {artifacts:path}/index.js"
},
"Artifacts": [
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3JS/1.0.0/stream-manager-sdk.zip",
"Unarchive": "ZIP"
},
{
"URI": "s3://amzn-s3-demo-bucket/artifacts/com.example.StreamManagerS3JS/1.0.0/index.js"
}
]
}
]
}
- YAML
-
---
RecipeFormatVersion: '2020-01-25'
ComponentName: com.example.StreamManagerS3JS
ComponentVersion: 1.0.0
ComponentDescription: Uses stream manager to upload a file to an S3 bucket.
ComponentPublisher: Amazon
ComponentDependencies:
aws.greengrass.StreamManager:
VersionRequirement: "^2.0.0"
Manifests:
- Platform:
os: linux
Lifecycle:
install: npm install {artifacts:decompressedPath}/stream-manager-sdk/aws-greengrass-stream-manager-sdk
run: |
export NODE_PATH=$NODE_PATH:{work:path}/node_modules
node {artifacts:path}/index.js
Artifacts:
- URI: s3://DOC-EXAMPLE-BUCKET/artifacts/com.example.StreamManagerS3JS/1.0.0/stream-manager-sdk.zip
Unarchive: ZIP
- URI: s3://DOC-EXAMPLE-BUCKET/artifacts/com.example.StreamManagerS3JS/1.0.0/index.js
- Platform:
os: windows
Lifecycle:
install: npm install {artifacts:decompressedPath}/stream-manager-sdk/aws-greengrass-stream-manager-sdk
run: |
set "NODE_PATH=%NODE_PATH%;{work:path}/node_modules"
node {artifacts:path}/index.js
Artifacts:
- URI: s3://DOC-EXAMPLE-BUCKET/artifacts/com.example.StreamManagerS3JS/1.0.0/stream-manager-sdk.zip
Unarchive: ZIP
- URI: s3://DOC-EXAMPLE-BUCKET/artifacts/com.example.StreamManagerS3JS/1.0.0/index.js
如需如何開發和測試元件的詳細資訊,請參閱建立 AWS IoT Greengrass 元件。
在應用程式程式碼中 Connect 至串流管
若要在應用程式中連線至串流管理員,請StreamManagerClient
從串流管理員建立的執行個體SDK。此用戶端會連線至串流管理員元件的預設連接埠 8088 或您指定的連接埠。如需建立執行個體StreamManagerClient
後如何使用的詳細資訊,請參閱用 StreamManagerClient 於使用串流。
範例:使用預設 Connect 埠連線至串流管理員
- Java
-
import com.amazonaws.greengrass.streammanager.client.StreamManagerClient;
public class MyStreamManagerComponent {
void connectToStreamManagerWithDefaultPort() {
StreamManagerClient client = StreamManagerClientFactory.standard().build();
// Use the client.
}
}
- Python
-
from stream_manager import (
StreamManagerClient
)
def connect_to_stream_manager_with_default_port():
client = StreamManagerClient()
# Use the client.
- JavaScript
-
const {
StreamManagerClient
} = require('aws-greengrass-stream-manager-sdk');
function connectToStreamManagerWithDefaultPort() {
const client = new StreamManagerClient();
// Use the client.
}
範例:使用非預設 Connect 埠連線至串流管理員
如果您使用預設值以外的連接埠設定串流管理員,則必須使用處理序間通訊,從元件組態擷取連接埠。
組port
態參數包含您在部署串流管理員STREAM_MANAGER_SERVER_PORT
時所指定的值。
- Java
-
void connectToStreamManagerWithCustomPort() {
EventStreamRPCConnection eventStreamRpcConnection = IPCUtils.getEventStreamRpcConnection();
GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(eventStreamRpcConnection);
List<String> keyPath = new ArrayList<>();
keyPath.add("port");
GetConfigurationRequest request = new GetConfigurationRequest();
request.setComponentName("aws.greengrass.StreamManager");
request.setKeyPath(keyPath);
GetConfigurationResponse response =
greengrassCoreIPCClient.getConfiguration(request, Optional.empty()).getResponse().get();
String port = response.getValue().get("port").toString();
System.out.print("Stream Manager is running on port: " + port);
final StreamManagerClientConfig config = StreamManagerClientConfig.builder()
.serverInfo(StreamManagerServerInfo.builder().port(Integer.parseInt(port)).build()).build();
StreamManagerClient client = StreamManagerClientFactory.standard().withClientConfig(config).build();
// Use the client.
}
- Python
-
import awsiot.greengrasscoreipc
from awsiot.greengrasscoreipc.model import (
GetConfigurationRequest
)
from stream_manager import (
StreamManagerClient
)
TIMEOUT = 10
def connect_to_stream_manager_with_custom_port():
# Use IPC to get the port from the stream manager component configuration.
ipc_client = awsiot.greengrasscoreipc.connect()
request = GetConfigurationRequest()
request.component_name = "aws.greengrass.StreamManager"
request.key_path = ["port"]
operation = ipc_client.new_get_configuration()
operation.activate(request)
future_response = operation.get_response()
response = future_response.result(TIMEOUT)
stream_manager_port = str(response.value["port"])
# Use port to create a stream manager client.
stream_client = StreamManagerClient(port=stream_manager_port)
# Use the client.