与 AWS SDK或CreateTopicRule一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或CreateTopicRule一起使用 CLI

以下代码示例演示如何使用 CreateTopicRule

C++
SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 CreateTopicRule” 中的。

CLI
AWS CLI

创建发送 Amazon SNS 提醒的规则

以下create-topic-rule示例创建了一条规则,该规则用于在设备影子中发现的土壤湿度读数较低时发送 Amazon SNS 消息。

aws iot create-topic-rule \ --rule-name "LowMoistureRule" \ --topic-rule-payload file://plant-rule.json

该示例要求将以下JSON代码保存到名为的文件中plant-rule.json

{ "sql": "SELECT * FROM '$aws/things/MyRPi/shadow/update/accepted' WHERE state.reported.moisture = 'low'\n", "description": "Sends an alert whenever soil moisture level readings are too low.", "ruleDisabled": false, "awsIotSqlVersion": "2016-03-23", "actions": [{ "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:MyRPiLowMoistureTopic", "roleArn": "arn:aws:iam::123456789012:role/service-role/MyRPiLowMoistureTopicRole", "messageFormat": "RAW" } }] }

此命令不生成任何输出。

有关更多信息,请参阅《AWS 物联网开发人员指南》中的创建AWS 物联网规则

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/** * Creates an IoT rule asynchronously. * * @param roleARN The ARN of the IAM role that grants access to the rule's actions. * @param ruleName The name of the IoT rule. * @param action The ARN of the action to perform when the rule is triggered. * * This method initiates an asynchronous request to create an IoT rule. * If the request is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void createIoTRule(String roleARN, String ruleName, String action) { String sql = "SELECT * FROM '" + TOPIC + "'"; SnsAction action1 = SnsAction.builder() .targetArn(action) .roleArn(roleARN) .build(); // Create the action. Action myAction = Action.builder() .sns(action1) .build(); // Create the topic rule payload. TopicRulePayload topicRulePayload = TopicRulePayload.builder() .sql(sql) .actions(myAction) .build(); // Create the topic rule request. CreateTopicRuleRequest topicRuleRequest = CreateTopicRuleRequest.builder() .ruleName(ruleName) .topicRulePayload(topicRulePayload) .build(); CompletableFuture<CreateTopicRuleResponse> future = getAsyncClient().createTopicRule(topicRuleRequest); future.whenComplete((response, ex) -> { if (response != null) { System.out.println("IoT Rule created successfully."); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to create IoT Rule."); } } }); future.join(); }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateTopicRule” 中的。

Kotlin
SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun createIoTRule( roleARNVal: String?, ruleNameVal: String?, action: String?, ) { val sqlVal = "SELECT * FROM '$TOPIC '" val action1 = SnsAction { targetArn = action roleArn = roleARNVal } val myAction = Action { sns = action1 } val topicRulePayloadVal = TopicRulePayload { sql = sqlVal actions = listOf(myAction) } val topicRuleRequest = CreateTopicRuleRequest { ruleName = ruleNameVal topicRulePayload = topicRulePayloadVal } IotClient { region = "us-east-1" }.use { iotClient -> iotClient.createTopicRule(topicRuleRequest) println("IoT rule created successfully.") } }
  • 有关API详细信息,请参阅CreateTopicRule中的 Kotlin AWS SDK API 参考