自动配置类
/**
* @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);
}