iotdb Java原生接口操作

 自动配置类

/**
 * @author chenchen
 * @description
 * @date 2023/5/16 14:13
 */
@Configuration
public class IotDBSessionConfig {

    @Value("${support.iotdb.host:127.0.0.1}")
    private String host;
    @Value("${support.iotdb.port:6667}")
    private int port;
    @Value("${support.iotdb.username:root}")
    private String username;
    @Value("${support.iotdb.password:root}")
    private String password;
    @Value("${support.iotdb.maxSize:10}")
    private int maxSize;

    @Bean
    public Session iotDBSession() throws IoTDBConnectionException {
        Session session = new Session.Builder()
                .host(host)
                .port(port)
                .username(username)
                .password(password)
                .build();
        session.open(false);
        return session;
    }

    @Bean
    public SessionPool sessionPool() {
        return new SessionPool.Builder()
                .host(host)
                .port(port)
                .user(username)
                .password(password)
                .maxSize(maxSize)
                .build();
    }
}

 数据写入

    /**
     * @description 插入iotdb数据
     * @author chenchen
     * @date 2023/5/17 9:49
     * @param
     * @return void
     */
    @Test
    public void test11() throws IoTDBConnectionException, StatementExecutionException, InterruptedException {
        //insertTablet
        String deviceId = "root.ln.wf01.wt01";

        List<MeasurementSchema> schemaList = new ArrayList<>();
        schemaList.add(new MeasurementSchema("status", TSDataType.BOOLEAN));
        schemaList.add(new MeasurementSchema("temperature", TSDataType.FLOAT));

        //根据maxRowNumber初始化多少个timestamp和values测量点个数
        Tablet tablet = new Tablet(deviceId, schemaList, 5);

        for (int i = 0; i < tablet.getMaxRowNumber(); i++) {
            tablet.rowSize++;
            Thread.sleep(1000);
            tablet.addTimestamp(i, System.currentTimeMillis());
            for (MeasurementSchema measurementSchema : schemaList) {
                if (measurementSchema.getType().equals(TSDataType.BOOLEAN)) {
                    tablet.addValue(measurementSchema.getMeasurementId(), i, true);
                } else {
                    tablet.addValue(measurementSchema.getMeasurementId(), i, new Random().nextFloat());
                }
            }
        }
        iotDBSession.insertAlignedTablet(tablet);
    }

 数据查询 

    /**
     * @description 按时间段查询时序数据
     * @author chenchen
     * @date 2023/5/17 10:19
     * @param
     * @return void
     */
    @Test
    public void test12() throws IoTDBConnectionException, StatementExecutionException {
        String path = "root.ln.wf01.wt01.status";
        String path2 = "root.ln.wf01.wt01.temperature";

        List<String> paths = new ArrayList<>();
        paths.add(path);
        paths.add(path2);

        //开始时间
        String beginTime = "2023-05-17 09:30:00";
        LocalDateTime dateTime = LocalDateTime.parse(beginTime, dtf);
        long startTime = dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        //结束时间
        LocalDateTime completeTime = LocalDateTime.now();
        long endTime = completeTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();

        //SessionDataSet sessionDataSet = iotDBSession.executeRawDataQuery(paths, startTime, endTime, 1000);
        //SessionDataSet sessionDataSet = iotDBSession.executeLastDataQuery(paths);
        String sql = "select status, temperature from root.ln.wf01.wt01 order by Time desc";
        SessionDataSet sessionDataSet = iotDBSession.executeQueryStatement(sql);

        List<String> columnNames = sessionDataSet.getColumnNames();
        //List<String> columnTypes = sessionDataSet.getColumnTypes();

        List<Map<String, Object>> list = new ArrayList<>();

        while (sessionDataSet.hasNext()) {
            Map<String, Object> map = new LinkedHashMap<>();
            RowRecord record = sessionDataSet.next();
            long timestamp = record.getTimestamp();
            map.put(columnNames.get(0), timestamp);
            List<Field> fields = record.getFields();
            for (int i = 0; i < fields.size(); i++) {
                map.put(columnNames.get(i + 1), fields.get(i).getStringValue());
            }
            list.add(map);
        }

        list.forEach(System.out::println);
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值