Apache-iotdb物联网数据库的安装及使用
Apache-iotdb物联网数据库的安装及使用
一、简介
>Apache IoTDB (Database for Internet of Things) is an IoT native database with high performance for data management and analysis, deployable on the edge and the cloud. Due to its light-weight architecture, high performance and rich feature set together with its deep integration with Apache Hadoop, Spark and Flink, Apache IoTDB can meet the requirements of massive data storage, high-speed data ingestion and complex data analysis in the IoT industrial fields.
>Apache IoTDB(物联网数据库)是一款专为物联网设计的高性能数据库,适用于数据管理和分析,既可在边缘部署,也可在云端部署。由于其轻量级架构、高性能以及丰富的功能集,加上与Apache Hadoop、Spark和Flink的深度集成,Apache IoTDB可以满足物联网工业领域对海量数据存储、高速数据摄取和复杂数据分析的需求。
二、安装(单机版)
官方文档参考:https://iotdb.apache.org/UserGuide/latest/QuickStart/QuickStart.html
1.安装前置条件
Java >= 1.8 (Please make sure the environment path has been set)
Set the max open files num as 65535 to avoid “too many open files” problem。
#jdk 安装1.8或以上,略
#修改文件句柄数量
##临时设置
sudo sysctl -w net.core.somaxconn=65535
##永久设置
vi /etc/sysctl.conf 后加入参数
net.core.somaxconn=65535
然后执行
sysctl -p
下面的这些我为了保险也设置了一下
####
cat >> /etc/security/limits.conf << EOF
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
EOF
####
sed -i 's/^* soft nproc 65536$/* soft nproc 65536/' /etc/security/limits.d/20-nproc.conf
####
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=655360
EOF
sysctl -p
1.下载及解压到安装目录
下载:
>wget https://www.apache.org/dyn/closer.cgi/iotdb/1.3.0/apache-iotdb-1.3.0-all-bin.zip
解压:
#解压到/home/testuser目录下
unzip apache-iotdb-1.3.0-all-bin.zip -d /home/testuser
2.启动
# Unix/OS X
> cd /home/testuser/apache-iotdb-1.3.0-all-bin
> bash sbin/start-standalone.sh
# Windows
> sbin\start-standalone.bat
3.停止
# Unix/OS X
> bash sbin/stop-standalone.sh
# Windows
> sbin\stop-standalone.bat
二、安装(集群版)
官网文档:https://iotdb.apache.org/UserGuide/latest/QuickStart/ClusterQuickStart.html
1.集群安装规划
服务安装规划
IP | Role |
---|---|
192.168.56.101 | ConfigNode,DataNode |
192.168.56.102 | ConfigNode,DataNode |
192.168.56.103 | ConfigNode,DataNode |
端口规划
Service | ConfigNode | DataNode |
---|---|---|
Port | 10710,10720 | 6667,10730,10740,10750,10760 |
2.前置条件(所有机器上执行)
Java >= 1.8 (Please make sure the environment path has been set)
Set the max open files num as 65535 to avoid “too many open files” problem。
#jdk 安装1.8或以上,略
#修改文件句柄数量
##临时设置
sudo sysctl -w net.core.somaxconn=65535
##永久设置
vi /etc/sysctl.conf 后加入参数
net.core.somaxconn=65535
然后执行
sysctl -p
下面的这些我为了保险也设置了一下
####
cat >> /etc/security/limits.conf << EOF
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
EOF
####
sed -i 's/^* soft nproc 65536$/* soft nproc 65536/' /etc/security/limits.d/20-nproc.conf
####
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=655360
EOF
sysctl -p
3.下载及解压(所有机器上执行)
下载:
>wget https://www.apache.org/dyn/closer.cgi/iotdb/1.3.0/apache-iotdb-1.3.0-all-bin.zip
解压:
#解压到/home/testuser目录下
unzip apache-iotdb-1.3.0-all-bin.zip -d /home/testuser
4.配置
(1)192.168.56.101
iotdb-confignode.properties:
cn_internal_address=192.168.56.101
cn_seed_config_node=192.168.56.101:10710
iotdb-datanode.properties:
dn_rpc_address=192.168.56.101
dn_internal_address=192.168.56.101
dn_seed_config_node=192.168.56.101:10710
(2)192.168.56.102
iotdb-confignode.properties:
cn_internal_address=192.168.56.102
cn_seed_config_node=192.168.56.101:10710
iotdb-datanode.properties:
dn_rpc_address=192.168.56.102
dn_internal_address=192.168.56.102
dn_seed_config_node=192.168.56.101:10710
(3)192.168.56.103
iotdb-confignode.properties:
cn_internal_address=192.168.56.103
cn_seed_config_node=192.168.56.101:10710
iotdb-datanode.properties:
dn_rpc_address=192.168.56.103
dn_internal_address=192.168.56.103
dn_seed_config_node=192.168.56.101:10710
注意:上面的配置cn_target_config_node_list,cn_seed_config_node,三台上使用的都是种子节点的IP:192.168.56.101
5.启动集群
(1)防火墙开放
需要开放6667,10710,10720,10730,10740,10750,10760 这些端口的访问权限,我这里使用的是firewalld,你也可以使用iptables
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="6667" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="10710" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="10720" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="10730" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="10740" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="10750" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" port protocol="tcp" port="10760" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="6667" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="10710" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="10720" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="10730" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="10740" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="10750" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" port protocol="tcp" port="10760" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="6667" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="10710" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="10720" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="10730" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="10740" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="10750" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.103" port protocol="tcp" port="10760" accept"
firewall-cmd --reload
(2)启动集群
启动第一个节点(start the first node,192.168.56.101):
#1.启动第一个节点(start the first node,192.168.56.101)
cd /home/testuser/apache-iotdb-1.3.0-all-bin
sbin/start-standalone.sh
tail -f logs/log_datanode_all.log
启动其他节点start service ConfigNode and DataNode in other nodes,192.168.56.101,192.168.56.103
cd /home/testuser/apache-iotdb-1.3.0-all-bin
sbin/start-standalone.sh
tail -f logs/log_datanode_all.log
(3)检查集群
#随便找哪一台集群机器,执行命令,下面的 -h 后的ip可以是集群中的任何一个ip
cd /home/testuser/apache-iotdb-1.3.0-all-bin
./sbin/start-cli.sh -h 192.168.56.101
#查看集群状态
show cluster;
IoTDB> show cluster;
+------+----------+-------+---------------+------------+-------+---------+
|NodeID| NodeType| Status|InternalAddress|InternalPort|Version|BuildInfo|
+------+----------+-------+---------------+------------+-------+---------+
| 0|ConfigNode|Running| 192.168.56.101| 10710| 1.3.0| fe787ca|
| 2|ConfigNode|Running| 192.168.56.102| 10710| 1.3.0| fe787ca|
| 4|ConfigNode|Running| 192.168.56.103| 10710| 1.3.0| fe787ca|
| 1| DataNode|Running| 192.168.56.101| 10730| 1.3.0| fe787ca|
| 3| DataNode|Running| 192.168.56.102| 10730| 1.3.0| fe787ca|
| 5| DataNode|Running| 192.168.56.103| 10730| 1.3.0| fe787ca|
+------+----------+-------+---------------+------------+-------+---------+
Total line number = 6
It costs 0.317s
6.扩容
Expand means add services of ConfigNode or DataNode into an existing IoTDB cluster.
1.配置
iotdb-confignode.properties:
cn_internal_address=192.168.56.104
cn_seed_config_node=192.168.56.101:10710
iotdb-datanode.properties:
dn_rpc_address=192.168.56.104
dn_internal_address=192.168.56.104
dn_seed_config_node=192.168.56.101:10710
2.防火墙加入
在原来的集群的三台机上执行
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="6667" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="10710" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="10720" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="10730" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="10740" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="10750" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.104" port protocol="tcp" port="10760" accept"
firewall-cmd --reload
同时,新加的那台机器也需要执行启动集群步骤中的防火墙开放操作,让几台机器之间的端口能互通
3.启动
cd /home/testuser/apache-iotdb-1.3.0-all-bin
sbin/start-standalone.sh
tail -f logs/log_datanode_all.log
查看启动后是否已加入集群
IoTDB> show cluster;
+------+----------+-------+---------------+------------+-------+---------+
|NodeID| NodeType| Status|InternalAddress|InternalPort|Version|BuildInfo|
+------+----------+-------+---------------+------------+-------+---------+
| 0|ConfigNode|Running| 192.168.56.101| 10710| 1.3.0| fe787ca|
| 2|ConfigNode|Running| 192.168.56.103| 10710| 1.3.0| fe787ca|
| 4|ConfigNode|Running| 192.168.56.102| 10710| 1.3.0| fe787ca|
| 6|ConfigNode|Running| 192.168.56.104| 10710| 1.3.0| fe787ca|
| 1| DataNode|Running| 192.168.56.101| 10730| 1.3.0| fe787ca|
| 3| DataNode|Running| 192.168.56.103| 10730| 1.3.0| fe787ca|
| 5| DataNode|Running| 192.168.56.102| 10730| 1.3.0| fe787ca|
| 7| DataNode|Running| 192.168.56.104| 10730| 1.3.0| fe787ca|
+------+----------+-------+---------------+------------+-------+---------+
Total line number = 8
It costs 0.013s
7.缩容
Shrink means removing a service from the IoTDB cluster.
以对192.168.56.102进行移除为例:
(1)移除ConfigNode,shrink service ConfigNode
cd /home/testuser/apache-iotdb-1.3.0-all-bin
# way 1: shrink with ip:port
sbin/remove-confignode.sh 192.168.56.102:10710
# way 2: shrink with NodeID of `show cluster`
sbin/remove-confignode.sh 6
(2)移除DataNode,shrink service DataNode
cd /home/testuser/apache-iotdb-1.3.0-all-bin
# way 1: shrink with ip:port
sbin/remove-datanode.sh 192.168.56.102:6667
# way 2: shrink with NodeID of `show cluster`
sbin/remove-confignode.sh 7
(3)检查,check the result
IoTDB> show cluster
+------+----------+-------+---------------+------------+-------+---------+
|NodeID| NodeType| Status|InternalAddress|InternalPort|Version|BuildInfo|
+------+----------+-------+---------------+------------+-------+---------+
| 0|ConfigNode|Running| 192.168.56.101| 10710| 1.3.0| fe787ca|
| 2|ConfigNode|Running| 192.168.56.103| 10710| 1.3.0| fe787ca|
| 6|ConfigNode|Running| 192.168.56.104| 10710| 1.3.0| fe787ca|
| 1| DataNode|Running| 192.168.56.101| 10730| 1.3.0| fe787ca|
| 3| DataNode|Running| 192.168.56.103| 10730| 1.3.0| fe787ca|
| 7| DataNode|Running| 192.168.56.104| 10730| 1.3.0| fe787ca|
+------+----------+-------+---------------+------------+-------+---------+
Total line number = 6
8.【reference】Cleanup,如果想清除集群数据重新初始化
如果想重新初始化集群,结束所有进程,然后删除data和logs目录,并重新启动即可。
(1).结束进程
End processes of ConfigNode and DataNode
# 1. Stop services ConfigNode and DataNode
sbin/stop-standalone.sh
# 2. Check whether there are IoTDB processes left or not
jps
# 或者
ps -ef|grep iotdb
# 3. If there is any IoTDB process left, kill it
kill -9 <pid>
# If there is only 1 IoTDB instance, execue command below to remove all IoTDB process
ps -ef|grep iotdb|grep -v grep|tr -s ' ' ' ' |cut -d ' ' -f2|xargs kill -9
(2).清除数据
Remove directories of data and logs
cd /home/testuser/apache-iotdb-1.3.0-all-bin
rm -rf data logs
三、简单使用
1.简单命令
#连接登录:
> bash sbin/start-cli.sh -h 127.0.0.1 -p 6667 -u root -pw root
#创建数据库
IoTDB> create database root.ln
#查看数据库
IoTDB> SHOW DATABASES
#创建timeseries
IoTDB> CREATE TIMESERIES root.ln.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN
IoTDB> CREATE TIMESERIES root.ln.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=RLE
#查看timeseries
IoTDB> SHOW TIMESERIES
IoTDB> SHOW TIMESERIES root.ln.wf01.wt01.status
#插入数据
IoTDB> INSERT INTO root.ln.wf01.wt01(timestamp,status) values(100,true);
IoTDB> INSERT INTO root.ln.wf01.wt01(timestamp,status,temperature) values(200,false,20.71)
#查询数据
IoTDB> SELECT * FROM root.ln.wf01.wt01
#退出登录
IoTDB> quit
IoTDB> exit
2.修改root密码
IoTDB > ALTER USER root SET PASSWORD 'newpwd';
四、API调用
(1)Java Native
https://iotdb.apache.org/UserGuide/latest/API/Programming-Java-Native-API.html#session-pool-for-native-api
引入maven
<dependencies>
<dependency>
<groupId>org.apache.iotdb</groupId>
<artifactId>iotdb-session</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
Session初始化测试
一般不使用,采用本机localhost和6667端口及默认账号密码root/root初始化Session
// use default configuration
Session session = new Session.Builder().build();
System.out.println(session);
session.open();
session.close();
通过Session.Builder构造器初始化Session,采用默认的账号root和默认密码root,指定IP和端口
// initialize with a single node
Session session =
new Session.Builder()
.host(host)
.port(port)
.build();
session.open();
System.out.println(session);
session.close();
采用自定义的账号和密码码初始化Session
Session session =
new Session.Builder()
.host(host)
.port(port)
.username("root")
.password("123456")
.build();
有多个节点的情况下,通过Session.Builder构造器初始化Session
// initialize with multiple nodes
List<String> nodeUrls = new ArrayList<>();
nodeUrls.add("192.168.56.101:6667");
nodeUrls.add("192.168.56.102:6667");
nodeUrls.add("192.168.56.103:6667");
Session session =
new Session.Builder()
.nodeUrls(nodeUrls)
.username("root")
.password("123456")
.build();
数据库管理测试
创建数据库
String storageGroupId = "root.group1";
session.setStorageGroup(storageGroupId);
删除数据库
String storageGroupId = "root.group1";
session.deleteStorageGroup(storageGroupId);
删除多个
List<String> storageGroupIds = new ArrayList<String>();
storageGroupIds.add("root.group1");
storageGroupIds.add("root.group2");
session.deleteStorageGroups(storageGroupIds);
Timeseries Test
创建Timeseries
session.createTimeseries("root.sestest.wf01.wt01.s0", TSDataType.INT32, TSEncoding.RLE, CompressionType.SNAPPY);
session.createTimeseries("root.sestest.wf01.wt01.s1", TSDataType.INT32, TSEncoding.RLE, CompressionType.SNAPPY);
删除Timeseries
//可以传一个timeseries
//session.deleteTimeseries("root.sestest.wf01.wt01.s0");
//session.deleteTimeseries("root.sestest.wf01.wt01.s1");
//也可以传多个timeseries
List<String> timeseries = new ArrayList<String>();
timeseries.add("root.sestest.wf01.wt01.s0");
timeseries.add("root.sestest.wf01.wt01.s1");
session.deleteTimeseries(timeseries);
检查是否存在
String timeseries = "root.sestest.wf01.wt01.s0";
System.out.println(session.checkTimeseriesExists(timeseries));
Tablet Insert
/**
* 插入Tablet
* @throws StatementExecutionException
* @throws IoTDBConnectionException
*/
@Test
public void insertTabletTest() throws StatementExecutionException, IoTDBConnectionException {
int BATCH_INSERT_SIZE = 10000;
long TOTAL_INSERT_ROW_COUNT = 20003L;
session.setFetchSize(2048);
session.open(false);
/*
* 一个Tablet例子:
* deviceID: root.ln.wf01.wt01
* time status, temperature, speed
* 1 true 1.0 1
* 2 false 2.0 2
* 3 true 3.0 3
*/
// 设置设备名字,设备下面的传感器名字,各个传感器的类型
List<MeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("status", TSDataType.BOOLEAN));
schemaList.add(new MeasurementSchema("temperature", TSDataType.DOUBLE));
schemaList.add(new MeasurementSchema("speed", TSDataType.INT64));
Tablet tablet = new Tablet("root.ln.wf01.wt02", schemaList, BATCH_INSERT_SIZE);
// 以当前时间戳作为插入的起始时间戳
long timestamp = System.currentTimeMillis();
long row = 0;
for (row = 0; row < TOTAL_INSERT_ROW_COUNT; row++) {
int rowIndex = tablet.rowSize++;
tablet.addTimestamp(rowIndex, timestamp);
// 随机生成数据
tablet.addValue("status", rowIndex, (row & 1) == 0);
tablet.addValue("temperature", rowIndex, (double) row);
tablet.addValue("speed", rowIndex, row);
if (tablet.rowSize == tablet.getMaxRowNumber()) {
session.insertTablet(tablet);
tablet.reset();
System.out.println("已经插入了:" + (row + 1) + "行数据");
}
timestamp++;
}
// 插入剩余不足 BATCH_INSERT_SIZE的数据
if (tablet.rowSize != 0) {
session.insertTablet(tablet);
tablet.reset();
System.out.println("已经插入了:" + (row) + "行数据");
}
}
Record Insert Test
@Test
public void insertRecordTest() throws IoTDBConnectionException, StatementExecutionException {
//session.setStorageGroup("root.group1");
String deviceId = "root.group1.device1";
List<String> measurements = new ArrayList<>();
measurements.add("s0");
measurements.add("s1");
measurements.add("s2");
List<TSDataType> types = new ArrayList<>();
types.add(TSDataType.INT32);
types.add(TSDataType.INT32);
types.add(TSDataType.INT32);
List<Object> values = new ArrayList<>();
values.add(1);
values.add(2);
values.add(3);
long timestamp = System.currentTimeMillis();
session.insertRecord(deviceId, timestamp, measurements, types, values);
}
DeleteData Test
删除数据测试
/**
* 删除数据测试,
* 参数timestamp为long类型,表示要删除的数据的时间,在这时间或这时间之前的数据将删除
* Delete data before or equal to a timestamp of one or several timeseries
* @throws StatementExecutionException
*/
@Test
public void deleteDataTest() throws StatementExecutionException {
String deviceId = "root.group1.device2.s0";
long timestamp = System.currentTimeMillis();
System.out.println(timestamp);
try {
session.deleteData(deviceId, timestamp);
} catch (IoTDBConnectionException e) {
e.printStackTrace();
}
}
批量删除数据测试
/**
* 批量删除数据测试,
* 参数timestamp为long类型,表示要删除的数据的时间,在这时间或这时间之前的数据将删除
* Delete data before or equal to a timestamp of one or several timeseries
* @throws StatementExecutionException
*/
@Test
public void deleteDatasTest() throws StatementExecutionException {
List<String> paths = new ArrayList<String>();
paths.add("root.group1.device2.s0");
paths.add("root.group1.device2.s1");
paths.add("root.group1.device2.s2");
//删除当前时间以前的数据
long timestamp = System.currentTimeMillis();
try {
session.deleteData(paths, timestamp);
} catch (IoTDBConnectionException e) {
e.printStackTrace();
}
}
查询测试QueryTest
查询数据
使用session.executeRawDataQuery方法
/**
* 查询数据
*/
@Test
public void executeRawDataQueryTest() throws StatementExecutionException, IoTDBConnectionException {
List<String> paths = new ArrayList<>();
paths.add("root.ln.wf01.wt01.temperature");
paths.add("root.ln.wf01.wt01.speed");
paths.add("root.ln.wf01.wt01.status");
String startDateTimeStr = "2024-01-01 00:00:00";
LocalDateTime ldt = LocalDateTime.parse(startDateTimeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Date date = Date.from(ldt.atZone( ZoneId.systemDefault()).toInstant());
long startTime = date.getTime();
//当前时间
long endTime = System.currentTimeMillis();
SessionDataSet sessionDataSet = session.executeRawDataQuery(paths, startTime, endTime);
while(sessionDataSet.hasNext()) {
RowRecord rowRecord = sessionDataSet.next();
System.out.println(rowRecord);
}
}
查询最新数据
使用session.executeLastDataQuery方法
/**
* 查询最新数据
* 查询指定时间以后的最新数据
* @throws StatementExecutionException
* @throws IoTDBConnectionException
*/
@Test
public void executeLastDataQueryTest() throws StatementExecutionException, IoTDBConnectionException {
List<String> paths = new ArrayList<>();
paths.add("root.ln.wf01.wt01.temperature");
paths.add("root.ln.wf01.wt01.speed");
paths.add("root.ln.wf01.wt01.status");
//查询指定时间以后的最新数据,这里指定时间为"2024-01-01 00:00:00";
String startDateTimeStr = "2024-01-01 00:00:00";
LocalDateTime ldt = LocalDateTime.parse(startDateTimeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Date date = Date.from(ldt.atZone( ZoneId.systemDefault()).toInstant());
long startTime = date.getTime();
SessionDataSet sessionDataSet = session.executeLastDataQuery(paths, startTime);
while(sessionDataSet.hasNext()) {
RowRecord rowRecord = sessionDataSet.next();
System.out.println(rowRecord);
}
}
IoTDB-SQL Interface(根据sql查询数据)
使用session.executeQueryStatement方法
/**
* IoTDB-SQL Interface
* Sql语句接口,
* @throws StatementExecutionException
* @throws IoTDBConnectionException
*/
@Test
public void executeQueryStatement() throws StatementExecutionException, IoTDBConnectionException {
SessionDataSet sessionDataSet = session.executeQueryStatement("select * from root.ln.wf01.wt03");
while(sessionDataSet.hasNext()) {
RowRecord rowRecord = sessionDataSet.next();
System.out.println(rowRecord);
}
}
IoTDB-SQL Interface(根据sql更新数据)
使用session.executeNonQueryStatement方法
@Test
public void executeNonQueryStatement() throws StatementExecutionException, IoTDBConnectionException {
session.executeNonQueryStatement("INSERT INTO root.ln.wf01.wt03(timestamp,status) values(200,true);");
}
五、源码下载
github: https://github.com/jxlhljh/iotdbTest.git
gitee: https://gitee.com/jxlhljh/iotdbTest.git
更多推荐
所有评论(0)