scala – AWS Lambda – 如何获取来自AWS IOT的数据主题名称
发布时间:2020-12-16 18:19:14 所属栏目:安全 来源:网络整理
导读:我正在使用AWS IOT源测试AWS Lambda.我的mqtt客户端正在发布不同的主题:设备A将数据发布到流/ A,设备B将数据发布到流/ B中,因此在AWS Lambda中我定义了一个SQL规则,选择来自主题流/的主题的所有设备.问题是,现在我没有设备源的信息,因为我只有一个带有额外
我正在使用AWS IOT源测试AWS Lambda.我的mqtt客户端正在发布不同的主题:设备A将数据发布到流/ A,设备B将数据发布到流/ B中,因此在AWS Lambda中我定义了一个SQL规则,选择来自主题流/的主题的所有设备.问题是,现在我没有设备源的信息,因为我只有一个带有额外信息的数组[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,n) } } output.toByteArray(); } finally { output.close(); Array[Byte]() } } } 解决方法
我一直在寻找同样的东西,有办法实现这一目标.在构建SQL时,您可以使用topic()函数来获取发送消息的主题.这样你可以放入属性部分
*,topic() as topic 所以你的最终SQL看起来像: SELECT *,topic() as topic FROM one/of/my/+/topics 然后,您的有效负载将包含一个可以在lambda函数中解析的新属性主题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Shell脚本——按行读取文件
- 语法 – scala将多个函数调用参数合并为一个元组 – 这可以
- unix – 为什么你不能fsck一个已安装的分区?
- angularjs – 从Angular 1.5中的父组件调用子组件函数
- 动态调用WebService(C#)
- Angular 2错误:无法绑定到’innerhtml’,因为它不是已知的
- angularjs – 如何在rxjs中使用Observable.interval多个htt
- angularjs – 使用nghandsontable访问handsontable方法/属性
- Getting Started with AngularJS 1.5 and ES6: part1
- 警告:无法解析UsersPermissionsService的所有参数这将成为