问题
I have two result topics where messages arrive from different sources "almost nearly at the same time".
Topic: sensor1/result
--receiving payload--> { "output_from_sensor1": {"result":"OK"} }
Topic: sensor2/result
--receiving payload--> { "output_from_sensor2": {"result":"OK"} }
I would like to create an AWS IoT Rule which scans these two topics "simultaneously in one query" and take an action.
I am not sure if AWS IoT SQL support "scanning multiple topics" in one query. No such references found in AWS docs.
During the way, I have tried these IoT queries (from my knowledge of SQL syntax) but no luck so far :(
SELECT output_from_sensor1.result AS final_output.result FROM ‘sensor1/result’ WHERE (SELECT output_from_sensor2.result FROM ‘sensor2/result’)=‘OK’
(SELECT output_from_sensor1.result FROM 'sensor1/result') UNION (SELECT output_from_sensor2.result FROM 'sensor2/result')
Thanks much!
回答1:
AWS IoT rules are triggered by a single MQTT message and the rule actions only process the message that triggered the rule. So while the +
and #
Wildcards can be used to select from multiple topics, each invocation of the rule only handles one message.
Your assumption that it is possible to 'scan multiple topics' in one query implies that multiple messages are involved (to each topic).
Depending on the problem you are trying to solve, it may make sense to buffer the messages in a queue (e.g. SQS). The processing can then check if multiple messages appear in a given time window to perform a single action on both messages.
I am not sure if AWS IoT SQL support "scanning multiple topics" in one query. No such references found in AWS docs.
I haven't found a definitive statement in the documentation that rules this out. But the wording is consistent with a rule being triggered by one message.
e.g. From the rules tutorial
The rule is triggered when an MQTT message that matches the topic filter is received on a topic.
The FROM Clause subscribes the rule to a topic or topic filter using the MQTT +
and #
wildcards.
There are operators like AND
and OR
but these are not used in the FROM
clause. The operators documentation states:
The following operators can be used in SELECT and WHERE clauses.
来源:https://stackoverflow.com/questions/59737902/query-multiple-topics-at-once-using-iot-rule-sql