java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot整合IoTB

Springboot整合IoTB的实现示例

作者:moxiaoran5753

本文主要介绍了Springboot整合IoTB的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、引入依赖

 <!--引入iotdb session依赖-->
<dependency>
	<groupId>org.apache.iotdb</groupId>
	<artifactId>iotdb-session</artifactId>
	<version>1.3.2</version>
</dependency>

二、配置IotDB连接

iotdb:
  username: root
  password: root
  ip: 192.168.56.10
  port: 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;

    @Bean
    public 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;

    @Override
    public 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;
    }

    @Override
    public 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 {

    // 测试连接
    @Test
    public void testInitSessionPool() {
        IotDBSessionConf conf = new IotDBSessionConf();
        SessionPool sessionPool = conf.initSessionPool();
        System.out.println(sessionPool.getVersion());
    }
}

到此这篇关于Springboot整合IoTB的实现示例的文章就介绍到这了,更多相关Springboot整合IoTB内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文