本文主要是介绍java操作iotdb集群模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java集成iotdb集群模式
1、环境部署
已经部署好了三个节点的iotdb集群,版本为1.3.0,不会部署的看我上一篇文章
2、引入依赖
<dependency><groupId>org.apache.iotdb</groupId><artifactId>iotdb-session</artifactId><version>1.3.0</version>
</dependency>
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId>
</dependency>
3、引入配置
@Component
@ConfigurationProperties(prefix = "spring.iotdb")
public class IotDBConfigProperties {private List<String> nodeUrls;private String user;private String password;private int fetchSize;private int maxSize;
}
@Configuration
public class IotDBConfigSession {@Resourceprivate IotDBConfigProperties properties;@Beanpublic Session iotSession() throws IoTDBConnectionException, StatementExecutionException {Session session = new Session.Builder().nodeUrls(properties.getNodeUrls()).username(properties.getUser()).password(properties.getPassword()).version(Version.V_1_0).build();session.open(false);session.setFetchSize(properties.getFetchSize());try {session.createDatabase("root.iot");} catch (StatementExecutionException e) {if (e.getStatusCode() != TSStatusCode.DATABASE_ALREADY_EXISTS.getStatusCode()) {throw e;}}return session;}
}
环境配置
spring: iotdb:nodeUrls:- 192.168.1.251:6667- 192.168.1.252:6667- 192.168.1.253:6667user: rootpassword: rootfetchSize: 10000maxSize: 10
4、数据库操作
详情看iot官网的demo:
地址: https://github.com/apache/iotdb
接口的示例代码在:example/session/src/main/java/org/apache/iotdb/SessionExample.java
package com.ywzn.cloud.common.iotdb.utils;import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ywzn.cloud.common.iotdb.config.IotDBConfigSession;
import com.ywzn.cloud.common.iotdb.dto.IotDBQueryDTO;
import com.ywzn.cloud.common.iotdb.vo.IotDBHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;/*** @Description iotdb工具类*/
@Component
@Slf4j
public class IotDBUtils {//这里默认数据库final String dataBase = "root.iot.";@Resourceprivate IotDBConfigSession iotDBConfigSession ;private void insertRecord() throws IoTDBConnectionException, StatementExecutionException {Session session = iotDBConfigSession.iotSession();String deviceId = dataBase.concat("ROOT_SG1_D1");List<String> measurements = new ArrayList<>();List<TSDataType> types = new ArrayList<>();measurements.add("s1");measurements.add("s2");measurements.add("s3");types.add(TSDataType.INT64);types.add(TSDataType.INT64);types.add(TSDataType.INT64);for (long time = 0; time < 100; time++) {List<Object> values = new ArrayList<>();values.add(1L);values.add(2L);values.add(3L);session.insertRecord(deviceId, time, measurements, types, values);}}private void selectInto() throws IoTDBConnectionException, StatementExecutionException {Session session = iotDBConfigSession.iotSession();session.executeNonQueryStatement("select s1, s2, s3 into into_s1, into_s2, into_s3 from root.sg1.d1");try (SessionDataSet dataSet =session.executeQueryStatement("select into_s1, into_s2, into_s3 from root.sg1.d1")) {System.out.println(dataSet.getColumnNames());while (dataSet.hasNext()) {System.out.println(dataSet.next());}}}
}
这篇关于java操作iotdb集群模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!