当前位置:   article > 正文

Apache-iotdb物联网数据库的安装及使用_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/

二、安装(单机版)

官方文档参考: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

下面的这些我为了保险也设置了一下

####
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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
  • 1
  • 2

2.启动

# Unix/OS X
> cd /home/testuser/apache-iotdb-1.3.0-all-bin
> bash sbin/start-standalone.sh

# Windows
> sbin\start-standalone.bat
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.停止

# Unix/OS X
> bash sbin/stop-standalone.sh

# Windows
> sbin\stop-standalone.bat
  • 1
  • 2
  • 3
  • 4
  • 5

二、安装(集群版)

官网文档:https://iotdb.apache.org/UserGuide/latest/QuickStart/ClusterQuickStart.html

1.集群安装规划

服务安装规划

IPRole
192.168.56.101ConfigNode,DataNode
192.168.56.102ConfigNode,DataNode
192.168.56.103ConfigNode,DataNode

端口规划

ServiceConfigNodeDataNode
Port10710,107206667,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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

下面的这些我为了保险也设置了一下

####
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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
  • 1
  • 2

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
  • 1
  • 2

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
  • 1
  • 2
  • 3

(2)192.168.56.102

iotdb-confignode.properties:

cn_internal_address=192.168.56.102
cn_seed_config_node=192.168.56.101:10710
  • 1
  • 2

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
  • 1
  • 2
  • 3

(3)192.168.56.103

iotdb-confignode.properties:

cn_internal_address=192.168.56.103
cn_seed_config_node=192.168.56.101:10710
  • 1
  • 2

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
  • 1
  • 2
  • 3
  • 4

注意:上面的配置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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

(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
  • 1
  • 2
  • 3
  • 4

启动其他节点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
  • 1
  • 2
  • 3

(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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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
  • 1
  • 2

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
  • 1
  • 2
  • 3
  • 4

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

同时,新加的那台机器也需要执行启动集群步骤中的防火墙开放操作,让几台机器之间的端口能互通

3.启动

cd /home/testuser/apache-iotdb-1.3.0-all-bin
sbin/start-standalone.sh
tail -f logs/log_datanode_all.log
  • 1
  • 2
  • 3

查看启动后是否已加入集群

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

(2).清除数据

Remove directories of data and logs

cd /home/testuser/apache-iotdb-1.3.0-all-bin
rm -rf data logs
  • 1
  • 2

三、简单使用

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2.修改root密码

IoTDB > ALTER USER root SET PASSWORD 'newpwd';
  • 1

四、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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Session初始化测试

一般不使用,采用本机localhost和6667端口及默认账号密码root/root初始化Session

		// use default configuration 
		Session session = new Session.Builder().build();
		System.out.println(session);
		session.open();
		session.close();
  • 1
  • 2
  • 3
  • 4
  • 5

通过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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

采用自定义的账号和密码码初始化Session

		Session session = 
			    new Session.Builder()
			        .host(host)
			        .port(port)
			        .username("root")
			        .password("123456")
			        .build();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

有多个节点的情况下,通过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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

数据库管理测试

创建数据库

		String storageGroupId = "root.group1";
		session.setStorageGroup(storageGroupId);
  • 1
  • 2

删除数据库

		String storageGroupId = "root.group1";
		session.deleteStorageGroup(storageGroupId);
  • 1
  • 2

删除多个

		List<String> storageGroupIds = new ArrayList<String>();
		storageGroupIds.add("root.group1");
		storageGroupIds.add("root.group2");
		session.deleteStorageGroups(storageGroupIds);
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2

删除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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

检查是否存在

		String timeseries = "root.sestest.wf01.wt01.s0";
		System.out.println(session.checkTimeseriesExists(timeseries));
  • 1
  • 2

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) + "行数据");
		}
		
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

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);

	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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();
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

批量删除数据测试

	/**
	 * 批量删除数据测试,
	 * 参数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();
		}
		
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

查询测试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);
			
		}
		
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

查询最新数据

使用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);
		}
		
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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);
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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);");
	}
  • 1
  • 2
  • 3
  • 4

五、源码下载

github: https://github.com/jxlhljh/iotdbTest.git
gitee: https://gitee.com/jxlhljh/iotdbTest.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/943013
推荐阅读
相关标签
  

闽ICP备14008679号