ioTdb时序数据库Jdbc连接(一)

IoTDB时序数据库连接教程
本文详细介绍了如何在项目中使用IoTDB时序数据库,包括在pom文件中添加依赖,创建数据库连接,初始化数据库,以及测试和关闭连接的过程。提供了从配置到测试的完整步骤。

ioTdb时序数据库连接:

1.首先我们在pom文件添加iotdb的依赖包:

注:我这里使用的是0.10.0的版本

2.我们创建一个iotdbUtil(本来打算用springboot在配置文件创建连接的,结果失败了,求大神指教),然后定义数据库驱动用户名密码等。

3.初始化数据库,创建数据库连接

4.测试链接

我们可以看到控制台打印链接成功

5.关闭数据库链接

参考官方文档:http://iotdb.incubator.apache.org/zh/

以下是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(); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_33780243

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值