Springboot整合IoTB
一、引入依赖
<!--引入iotdb session依赖-->
<dependency><groupId>org.apache.iotdb</groupId><artifactId>iotdb-session</artifactId><version>1.3.2</version>
</dependency>
二、配置IotDB连接
iotdb:username: rootpassword: rootip: 192.168.56.10port: 6667#批量操作数量maxSize: 200
三、创建配置类
import lombok.extern.slf4j.Slf4j;
import org.apache.iotdb.session.pool.SessionPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@Slf4j
public class IotDBSessionConf {@Value("${iotdb.username}")private String username;@Value("${iotdb.password}")private String password;@Value("${iotdb.ip}")private String ip;@Value("${iotdb.port}")private int port;@Value("${iotdb.maxSize}")private int maxSize;//Session线程池private static SessionPool pool;@Beanpublic SessionPool initSessionPool() {if(pool==null) {log.info(">>>>>SessionPool初始化....");pool =new SessionPool.Builder().user(username).password(password).host(ip).port(port).maxSize(maxSize).build();}return pool;}
}
四、创建service和实现类
@Service
@RequiredArgsConstructor
public class IoTDBServiceImpl implements IoTDBService {private final IotDBSessionConf conf;@Overridepublic SessionDataSetWrapper executeQueryStatement(String sql) {SessionPool sessionPool = conf.initSessionPool();SessionDataSetWrapper wrapper = null;try {wrapper = sessionPool.executeQueryStatement(sql);} catch (IoTDBConnectionException e) {throw new RuntimeException(e);} catch (StatementExecutionException e) {throw new RuntimeException(e);}return wrapper;}@Overridepublic SessionDataSetWrapper executeRawDataQuery(List<String> path, long startTime, long endTime, long timeOut) {SessionPool sessionPool = conf.initSessionPool();SessionDataSetWrapper wrapper = null;try {wrapper = sessionPool.executeRawDataQuery(path, startTime, endTime, timeOut);} catch (IoTDBConnectionException e) {throw new RuntimeException(e);} catch (StatementExecutionException e) {throw new RuntimeException(e);}return wrapper;}
}
五、创建工具类
@RequiredArgsConstructor
public class IoTDBUtils {private final IoTDBService iotDBService;/*** 根据自定义SQL语句执行查询* @param sql* @return*/SessionDataSetWrapper executeQueryStatement(String sql) {return iotDBService.executeQueryStatement(sql);}/*** 根据时间区间执行查询数据* @param paths 时间序列* @param startTime 开始时间戳(包含)* @param endTime 结束时间戳(不包含)* @param timeOut 超时时间* @return*/SessionDataSetWrapper executeRawDataQuery(List<String> paths, long startTime, long endTime, long timeOut){return iotDBService.executeRawDataQuery(paths, startTime, endTime, timeOut);}
}
单元测试
@SpringBootTest(classes = IotDBSessionConfTest.class)
public class IotDBSessionConfTest {// 测试连接@Testpublic void testInitSessionPool() {IotDBSessionConf conf = new IotDBSessionConf();SessionPool sessionPool = conf.initSessionPool();System.out.println(sessionPool.getVersion());}
}