AWS Lambda - 如何获取来自AWS IOT的数据主题名称

ali*_*rat 4 scala amazon-web-services mqtt aws-lambda aws-iot

我正在使用AWS IOT源测试AWS Lambda.我的mqtt客户端正在发布不同的主题:设备A将数据发布到streaming/A,设备B将数据发布到streaming/BAWS Lambda中,因此我定义了一个SQL规则,选择来自主题的所有设备streaming/+.问题是,现在我没有设备源的信息,因为我只有一个Array[Byte]]额外的信息.如果有人有解决方案来访问带有主题信息的mqtt有效负载,我会接受它!

import java.io.{ByteArrayOutputStream, InputStream, OutputStream}
import com.amazonaws.services.lambda.runtime.{Context, RequestStreamHandler}
/**
  * Created by alifirat on 24/04/17.
  */
class IOTConsumer extends RequestStreamHandler {

  val BUFFER_SIZE = 1024 * 4

  override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = {
    val bytes = toByteArray(input)
    val logger= context.getLogger
    logger.log("Receive following thing :"  + new String(bytes))
    output.write(bytes)
  }

   /**
     * Reads and returns the rest of the given input stream as a byte array.
     * Caller is responsible for closing the given input stream.
     */
   def toByteArray(is : InputStream) : Array[Byte] = {
     val output = new ByteArrayOutputStream()
     try {
       val b = new Array[Byte](BUFFER_SIZE);
       var n = 0
       var flag = true
       while(flag) {
         n = is.read(b)
         if(n == -1) flag = false
         else {
           output.write(b, 0, n)
         }
       }
       output.toByteArray();
     } finally {
       output.close();
       Array[Byte]()
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*pio 12

我一直在寻找同样的东西,有办法实现这一目标.在构建SQL时,您可以使用topic()函数来获取发送消息的主题.这样你可以放入属性部分

*, topic() as topic
Run Code Online (Sandbox Code Playgroud)

所以你的最终SQL看起来像:

SELECT *, topic() as topic FROM one/of/my/+/topics
Run Code Online (Sandbox Code Playgroud)

然后,您的有效负载将包含一个可以在lambda函数中解析的新属性主题.