当前位置:网站首页>阿里云IoT流转到postgresql数据库方案
阿里云IoT流转到postgresql数据库方案
2022-04-23 03:52:00 【虚幻私塾】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
之前写过一篇如使用阿里云上部署.NET 3.1自定义运行时的文章,吐槽一下,虽然现在已经2022年了,但是阿里云函数计算的支持依然停留在.NET Core 2.1,更新缓慢,由于程序解包大小的限制,也不能放太复杂的东西的上去,虽然现在.NET 6裁剪包能挺好地解决这个问题,但是心里还是不爽。
需求
言归正传,有这么一个情景:发送数据想接入阿里云的IoT平台,然后直接插入postgresQL数据库中。正常来说,只要数据发送到了IoT平台,然后定义转发到RDS就可以了,不过阿里云有几个限制:
- 数据流转到RDS数据库,只能支持
mysql与sql server - 数据流转只支持
json形式的数据流转,如果发送的是透传的数据,那么发送不了(更新:现在新版的数据流转已经支持了。)
思前想后,可能只能掏出阿里云的函数计算服务了,运用函数计算作为中转,将透传的数据流转给函数计算,然后在函数计算中执行sql语句。
IoT平台接收设置
阿里云的物联网平台,设置了基本的产品和设备之后,如果是物模型的话,那么自行设置好对应的物模型。对于透传就比较简单了,支持MQTT的设备方只需要定义:
- 透传的消息发送到/{productKey}/{deviceName}/user/update
- 订阅阿里云的/{productKey}/{deviceName}/user/get
- 设置阿里云的Mqtt IoT实例终端节点:({YourProductKey}.iot-as-mqtt.{YourRegionId}.aliyuncs.com:1883
- 设置设备的ProductKey和ProductSecret
设置好之后,即可传输数据到阿里云IoT端,数据传输过来,看下日志,如果能看到:
那说明就已经发送OK了,接收到的是普通的字符串(不是json),需要进行进一步解析。
IoT流转设置
在云产品流转中,新建解析器,设置好数据源,数据目的选择函数计算:

解析器脚本比较简单:
var data = payload();
writeFc(1000, data);
注意,payload函数payload(textEncoding)是內建的函数:
- 不传入参数:默认按照UTF-8编码转换为字符串,即payload()等价于payload(‘utf-8’)。
- ‘json’:将payload数据转换成Map格式变量。如果payload不是JSON格式,则返回异常。
- ‘binary’:将payload数据转换成二进制变量进行透传。
这里我使用文本透传的形式,将数据转成UTF8文本传输。writeFc指将转换的内容传递给1000编号的目标函数。详情见文档。
当然还可以使用更为复杂的脚本,实现对脚本数据的初步处理,由于我这里后面还有函数计算,我就直接将数据转到下一个节点。
函数计算配置
按照官方文档新建函数,请注意不需要新建触发器!我们这里的函数使用python语言,通过psycopg2实现数据插入到postgres数据库中。
由于函数计算中,默认并没有该包,需要手动添加引用,官方建议使用Serverless Devs工具安装部署,这个玩意非常不好用,嗯,我不接受他的建议。推荐大家使用vscode,安装阿里云serverless的插件,这样其实更加方便。
按照插件的文档,自己建立好服务与函数,默认会给一个函数入口:
# To enable the initializer feature (https://help.aliyun.com/document\_detail/158208.html)
# please implement the initializer function as below:
# def initializer(context):
# logger = logging.getLogger()
# logger.info('initializing')
def handler(event, context):
logger = logging.getLogger()
logger.info(event)
return 'hello world'
我们首先在函数上右键,然后选择Install Package,选择pip安装psycopg2,依赖就自动被安装上了,这个非常方便。
请注意,通过IOT流转过来的字符串,是b'data'这样的形式的形式,需要先decode一下,然后在处理,修改函数为:
# -*- coding: utf-8 -*-
import logging
import psycopg2
import uuid
import time
def insert\_database(device\_id,data):
timest = int(time.time()*1000)
guid = str(uuid.uuid1())
conn = psycopg2.connect(database="", user="", password="", host="", port="")
cur = conn.cursor()
sql = 'INSERT INTO "data"("Id","DeviceId","Timestamp", "DataArray") VALUES (\'{id}\', \'{deviceid}\', \'{timestamp}\', array{data})'
sql = sql.format(id= guid, deviceid= device_id, timestamp= timest, data= data)
cur.execute(sql)
conn.commit()
print(" Records inserted successfully")
conn.close()
def extract\_string\_array(data: bytes):
arr = data.decode().strip().split(' ')
# 写自己的逻辑
return deviceid, resu
def handler(event, context):
logger = logging.getLogger()
logger.info(event)
device_id, result = extract_string_array(event)
insert_database(device_id, result)
return 'OK'
保存,然后在vscode中deploy即可。
提示:vscode中也可以进行本地的debug,还是比较方便的,不过这些功能依赖docker,所以还是提前装好比较好。
弄完了之后,应该是能看见这样的画面:
至此,数据就正常流转成功。
要点
版权声明
本文为[虚幻私塾]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u013190417/article/details/124358214
边栏推荐
- 標識符、關鍵字、數據類型
- AI CC 2019 installation tutorial under win10 (super detailed - small white version)
- The latest price trend chart and trading points of London Silver
- 伦敦银最新价格走势图与买卖点
- Raspberry pie 3B logs into the wired end of Ruijie campus network through mentohust, creates WiFi (open hotspot) for other devices, and realizes self startup at the same time
- Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
- 基于PHP的代步工具购物商城
- Summary of knowledge map (I)
- Installation and configuration of MinGW under win10
- 变量、常量、运算符
猜你喜欢
![[latex] differences in the way scores are written](/img/77/3aebc8812f3e2d239187b95d3bd49f.png)
[latex] differences in the way scores are written

Mechanical design knowledge point planning

基于PHP的代步工具购物商城

Installation and configuration of MinGW under win10

Opencv4 QR code recognition test

将编译安装的mysql加入PATH环境变量

Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?

OpenCV----YOLACT实例分割模型推理

抽象类、接口、常用关键字

Add the compiled and installed Mysql to the path environment variable
随机推荐
ROS series (III): introduction to ROS architecture
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022
什么是软件验收测试,第三方软件检测机构进行验收测试有什么好处?
Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
标识符、关键字、数据类型
VS Studio 修改C语言scanf等报错
On the principle of concurrent programming and the art of notify / Park
The art of concurrent programming (5): the use of reentrantlock
Concepts of objects and classes
(valid for personal testing) compilation guide of paddedetection on Jetson
The great gods in acmer like mathematics very much
Solve the technical problems in seq2seq + attention machine translation
使用大华设备开发行AI人流量统计出现时间不正确的原因分析
ROS series (I): rapid installation of ROS
Use the thread factory to set the thread name in the thread pool
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
The art of concurrent programming (6): explain the principle of reentrantlock in detail
Vs studio modifies C language scanf and other errors
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程

