feat:() 新增iotdb配置文件,iotdb测试

dev.processing
晨哀 2024-10-04 10:08:23 +08:00
parent f56787b5a8
commit 0e391451b0
2 changed files with 166 additions and 0 deletions

View File

@ -0,0 +1,107 @@
package com.muyu.processing.config;
import lombok.extern.log4j.Log4j2;
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.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* iotdb
* @Author
* @Packagecom.muyu.processing.config
* @Projectcar-cloud-server
* @nameIotDBConfig
* @Date2024/9/30 15:13
*/
@Log4j2
@Component
@Configuration
public class IotDBConfig {
/**
* session
*/
@Bean
public static Session session(){
Session session = null;
try {
session = new Session(
"47.101.49.53",
6667,
"root",
"root"
);
session.open(false);
session.setFetchSize(100);
} catch (Exception e) {
throw new RuntimeException(e);
}
return session;
}
/**
*
*/
public void execute(String deviceId, Long time, List<String> measurement, List<String> values){
if (!CollectionUtils.isEmpty(measurement) && !CollectionUtils.isEmpty(values)){
try {
session().insertAlignedRecord(deviceId,time,measurement,values);
} catch (IoTDBConnectionException e) {
throw new RuntimeException(e);
} catch (StatementExecutionException e) {
throw new RuntimeException(e);
}
}
}
/**
*
*/
public List<HashMap<String, Object>> executeQuery(String sql){
log.info("sql:{}",sql);
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
try {
SessionDataSet sessionDataSet = session().executeQueryStatement(sql);
int fetchSize = sessionDataSet.getFetchSize();
List<String> columnNames = sessionDataSet.getColumnNames();
List<String> columnTypes = sessionDataSet.getColumnTypes();
System.out.println(columnNames);
System.out.println(columnTypes);
if (fetchSize > 0){
while (sessionDataSet.hasNext()){
HashMap<String, Object> map = new HashMap<>();
RowRecord next = sessionDataSet.next();
List<Field> fields = next.getFields();
// 查询结果第一个为时间戳
long timestamp = next.getTimestamp();
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
String key = field.getStringValue();
// 这里的需要按照类型获取
Object value = field.getObjectValue(field.getDataType());
map.put(key, value);
}
list.add(map);
}
}
sessionDataSet.closeOperationHandle();
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
}

View File

@ -0,0 +1,59 @@
package com.muyu.processing.controller;
import com.muyu.processing.config.IotDBConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* iotdb
* @Author
* @Packagecom.muyu.processing.controller
* @Projectcar-cloud-server
* @nameIotdbController
* @Date2024/10/2 9:39
*/
@RestController
@RequestMapping("iotdb")
public class IotDbController {
@Resource
private IotDBConfig iotDBConfig;
private String json = "{\n" + " \"carVin\": \"VIN123456\",\n" +
" \"carName\": \"宝马\",\n" + "}";
/**
*
*/
@GetMapping("add")
public void add(){
// Map map = JSON.parseObject(json, Map.class);
// Set set = map.keySet();
ArrayList<String> key = new ArrayList<>();
ArrayList<String> value = new ArrayList<>();
key.add("car_vin");
key.add("car_name");
value.add("VIN123456");
value.add("宝马");
System.out.println(key);
System.out.println(value);
long l = System.currentTimeMillis();
iotDBConfig.execute("root.vehicle", l, key, value);
}
/**
*
*/
@GetMapping("findList")
public void findList(){
String sql = "select * from root.vehicle";
List<HashMap<String, Object>> list = iotDBConfig.executeQuery(sql);
System.out.println(list);
}
}