时序数据库IoTDB安装

安装

在ubuntu系统下的安装:下载地址lotdb 选择一个合适的邦本,下载解压成功,cd 进入解压文件夹的sbin目录,然后执行
在这里插入图片描述

启动服务

启动服务命令

在这里插入图片描述

./start-server.sh

启动成功
在这里插入图片描述

操作IoTDB

#使用Cli工具
IoTDB为用户提供多种与服务器交互的方式,在此我们介绍使用Cli工具进行写入、查询数据的基本步骤。

初始安装后的IoTDB中有一个默认用户:root,默认密码为root。用户可以使用该用户运行Cli工具操作IoTDB。Cli工具启动脚本为sbin文件夹下的start-cli脚本。启动脚本时需要指定运行ip、port、username和password。若脚本未给定对应参数,则默认参数为"-h 127.0.0.1 -p 6667 -u root -pw -root"

以下启动语句为服务器在本机运行,且用户未更改运行端口号的示例。

Ubuntu系统启动命令如下:
在服务器启动后,启动lotdb,还是在sbin目录

 ./start-cli.sh -h 127.0.0.1 -p 6667 -u root -pw root

启动成功
在这里插入图片描述

基本数据类型

IoTDB支持:

  • BOOLEAN(布尔值)
  • INT32(整型)
  • INT64(长整型)
  • FLOAT(单精度浮点数)
  • DOUBLE(双精度浮点数)
  • TEXT(字符串)
    一共六种数据类型。

编码方式

为了提高数据的存储效率,需要在数据写入的过程中对数据进行编码,从而减少磁盘空间的使用量。在写数据以及读数据的过程中都能够减少I/O操作的数据量从而提高性能。IoTDB支持四种针对不同类型的数据的编码方法:
PLAIN编码(PLAIN)
PLAIN编码,默认的编码方式,即不编码,支持多种数据类型,压缩和解压缩的时间效率较高,但空间存储效率较低。

二阶差分编码(TS_2DIFF)
二阶差分编码,比较适合编码单调递增或者递减的序列数据,不适合编码波动较大的数据。

游程编码(RLE)
游程编码,比较适合存储某些整数值连续出现的序列,不适合编码大部分情况下前后值不一样的序列数据。

游程编码也可用于对浮点数进行编码,但在创建时间序列的时候需指定保留小数位数(MAX_POINT_NUMBER,具体指定方式参见本文本文第5.4节)。比较适合存储某些浮点数值连续出现的序列数据,不适合存储对小数点后精度要求较高以及前后波动较大的序列数据。

GORILLA编码(GORILLA)
GORILLA编码,比较适合编码前后值比较接近的浮点数序列,不适合编码前后波动较大的数据。

定频数据编码 (REGULAR)
定频数据编码,仅适用于整形(INT32)和长整型(INT64)的定频数据,且允许数据中有一些点缺失,使用此方法编码定频数据优于二阶差分编码(TS_2DIFF)。

定频数据编码无法用于非定频数据,建议使用二阶差分编码(TS_2DIFF)进行处理。

数据类型与编码的对应关系
前文介绍的四种编码适用于不同的数据类型,若对应关系错误,则无法正确创建时间序列。数据类型与支持其编码的编码方式对应关系总结
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值