mqtt数据接收并根据设备配置表插入iotdb python代码_iotdb mqtt 服务从${iotdb_home} ${iotdb_conf} iotdbeng

IoTDB连接信息

iotdb_host = “iotdb.example.com”
iotdb_port = 6667
iotdb_username = “your_username”
iotdb_password = “your_password”

设备配置表,用于映射设备字段和IoTDB表

device_config = {
“field1”: “table1”,
“field2”: “table2”,
# 添加更多字段和表的映射关系
}

MQTT消息处理函数

def on_message(client, userdata, msg):
# 解析接收到的消息
data = msg.payload.decode(“utf-8”)
topic = msg.topic

# 根据设备配置表映射字段和表
field = topic.split("/")[-1]
if field in device_config:
    table = device_config[field]

    # 连接到IoTDB数据库
    connection = IoTDBConnection(iotdb_host, iotdb_port, iotdb_username, iotdb_password)
    connection.open()

    # 插入数据到IoTDB
    sql = f"INSERT INTO {table} (timestamp, {field}) VALUES ({msg.timestamp}, {data})"
    connection.execute(sql)

    # 关闭连接
    connection.close()

创建MQTT客户端

client = mqtt.Client()
client.username_pw_set(mqtt_username, mqtt_password)

设置消息处理函数

client.on_message = on_message

连接到MQTT服务器并订阅主题

client.connect(mqtt_broker, mqtt_port)
client.subscribe(mqtt_topic)

循环接收消息

client.loop_forever()
请根据您的实际情况修改代码中的相关参数,包括MQTT连接信息、IoTDB连接信息和设备配置表。确保您已安装所需的依赖库(paho-mqtt和pyiotdb)。

这是一个基本的示例,您可能需要根据您的具体需求进行进一步的修改和优化。希望对您有所帮助!如果您有任何进一步的问题,请随时告诉我。

**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数嵌入式工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/ce0fb4542bdef4f2afe9035bb3a3be00.png)

![img](https://img-blog.csdnimg.cn/img_convert/f2a910ceeafb96b5b7fb7e3ddbd0ef69.jpeg)

![img](https://img-blog.csdnimg.cn/img_convert/ae473e9eaaa4d0672fb1e526ab26f0be.png)

 **既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**

![img](https://img-blog.csdnimg.cn/img_convert/8a5d89435693e8869410a1050e69cdad.png)

![img](https://img-blog.csdnimg.cn/img_convert/e51cd760bf3db5b2d199a44121d3d0a9.png)

 

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以+V:Vip1104z获取!!! (备注:嵌入式)**

<img src="https://img-community.csdnimg.cn/images/73bb5de17851459088c6af944156ee24.jpg" alt="img" style="zoom: 67%;" />



# 最后

**资料整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~**

**你的支持,我的动力;祝各位前程似锦,offer不断,步步高升!!!**

# 最后

**资料整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~**

**你的支持,我的动力;祝各位前程似锦,offer不断,步步高升!!!**

**[更多资料点击此处获qu!!](https://bbs.csdn.net/topics/618376385)**
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码实现从MQTT读取数据并保存到iotdb时序数据库的示例: 1. 引入相关库文件 ``` <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.apache.iotdb</groupId> <artifactId>iotdb-jdbc</artifactId> <version>0.10.0</version> </dependency> ``` 2. 连接MQTT服务器 ``` String brokerUrl = "tcp://localhost:1883"; //MQTT服务器地址 String clientId = "iotdb"; //客户端ID MemoryPersistence persistence = new MemoryPersistence(); MqttClient mqttClient = new MqttClient(brokerUrl, clientId, persistence); mqttClient.connect(); ``` 3. 订阅MQTT主题 ``` String topic = "my/topic"; //订阅的主题 int qos = 1; //消息质量 mqttClient.subscribe(topic, qos); ``` 4. 接收MQTT消息并保存到iotdb时序数据库 ``` String iotdbUrl = "jdbc:iotdb://localhost:6667/"; //iotdb数据库地址 String iotdbUsername = "root"; //iotdb数据库用户名 String iotdbPassword = "root"; //iotdb数据库密码 String iotdbStorageGroup = "root.my"; //iotdb存储组 String iotdbMeasurement = "sensor1"; //iotdb测点名称 String iotdbDataType = "DOUBLE"; //iotdb数据类型 String iotdbEncoding = "PLAIN"; //iotdb编码方式 String iotdbTimeseries = iotdbStorageGroup + "." + iotdbMeasurement; //iotdb时序列名 Class.forName("org.apache.iotdb.jdbc.IoTDBDriver"); Connection connection = DriverManager.getConnection(iotdbUrl, iotdbUsername, iotdbPassword); Statement statement = connection.createStatement(); mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable throwable) { System.out.println("MQTT连接断开"); } @Override public void messageArrived(String topic, MqttMessage mqttMessage) { String message = new String(mqttMessage.getPayload()); System.out.println("收到MQTT消息:" + message); try { String sql = String.format("insert into %s(time,%s) values(%d,%s)", iotdbTimeseries, iotdbDataType, iotdbEncoding, System.currentTimeMillis(), message); statement.execute(sql); System.out.println("保存到iotdb成功"); } catch (SQLException e) { System.out.println("保存到iotdb失败:" + e.getMessage()); } } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); ``` 5. 断开MQTT连接 ``` mqttClient.disconnect(); ``` 完整代码示例: ``` import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class MqttToIotdb { public static void main(String[] args) throws MqttException, ClassNotFoundException, SQLException { String brokerUrl = "tcp://localhost:1883"; //MQTT服务器地址 String clientId = "iotdb"; //客户端ID MemoryPersistence persistence = new MemoryPersistence(); MqttClient mqttClient = new MqttClient(brokerUrl, clientId, persistence); mqttClient.connect(); String topic = "my/topic"; //订阅的主题 int qos = 1; //消息质量 mqttClient.subscribe(topic, qos); String iotdbUrl = "jdbc:iotdb://localhost:6667/"; //iotdb数据库地址 String iotdbUsername = "root"; //iotdb数据库用户名 String iotdbPassword = "root"; //iotdb数据库密码 String iotdbStorageGroup = "root.my"; //iotdb存储组 String iotdbMeasurement = "sensor1"; //iotdb测点名称 String iotdbDataType = "DOUBLE"; //iotdb数据类型 String iotdbEncoding = "PLAIN"; //iotdb编码方式 String iotdbTimeseries = iotdbStorageGroup + "." + iotdbMeasurement; //iotdb时序列名 Class.forName("org.apache.iotdb.jdbc.IoTDBDriver"); Connection connection = DriverManager.getConnection(iotdbUrl, iotdbUsername, iotdbPassword); Statement statement = connection.createStatement(); mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable throwable) { System.out.println("MQTT连接断开"); } @Override public void messageArrived(String topic, MqttMessage mqttMessage) { String message = new String(mqttMessage.getPayload()); System.out.println("收到MQTT消息:" + message); try { String sql = String.format("insert into %s(time,%s) values(%d,%s)", iotdbTimeseries, iotdbDataType, iotdbEncoding, System.currentTimeMillis(), message); statement.execute(sql); System.out.println("保存到iotdb成功"); } catch (SQLException e) { System.out.println("保存到iotdb失败:" + e.getMessage()); } } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); //等待MQTT消息 while (true) { } //断开MQTT连接 //mqttClient.disconnect(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值