aws iot rule sql

0

AWS IoT Rule Engine can handle MQTT Payload values ​​through SQL statements. When MQTT Client sends a message by putting a byte array value in the payload, can the byte array value be decoded and selected through SQL statements? For example,

Publish

  • topic : test/payload
  • payload : [72,101,108,108,111,119,111,114,108,100] (( Hello World )) SQL SQL SELECT * FROM 'test/payload' I wonder if it is possible to SELECT 'Hello World' by converting the above payload.
已提問 1 年前檢視次數 423 次
2 個答案
0

Hi,

there are no native functions in the SQL supported by Rules engine to decode binary data, but you can pass the data to a Lambda function that can return the decoded payload. Since Lambda functions do not support binary input, you need to convert the input to base64.

SELECT aws_lambda("arn:aws:lambda:us-east-1:account_id:function:lambda_function", {"message":encode(*, 'base64'}) as output FROM 'test/payload'
AWS
專家
已回答 1 年前
0

Hi rePost-User-1228067,

Thanks for your question! If I understand correctly:

  • You have a Byte array as your MQTT payload
  • You are trying to perform a filter on the decoded content from the binary array to know if the decoded (plain text) equals 'Hello World' for example. Then sending it out to some downstream services. Is that correct?

If that's the case, then your answer is yes (provided that your binary data is Base64 or Protobuf encoded). The native function is 'decode' and the syntax is decode(value, decodingScheme) where 'value' is the encoded string payload and decodingScheme is either 'base64' or 'proto'.

In your specific example, depending on the client flexibility, you'll either ensure first that your payload is a Base64 binary (or Protobuf) encoded value instead of a Byte Array, or have a lambda function cloud side to transform Byte Array to Base64 binary and republish to a new topic for processing. Then do the following:

SELECT decode(payload,"base64") AS myfilterresult from 'test/payload' where decode(payload,"base64") = 'Hello World' ; or anything similar, depending on the filtering conditions you'd like to apply.

Here are some documentation links for reference:

https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-sql-decode-base64.

https://docs.aws.amazon.com/iot/latest/developerguide/binary-payloads.html

profile pictureAWS
專家
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南