时序性数据库测试成功

pull/3/head
张腾 2024-09-29 01:17:24 +08:00
parent b299c8d3a1
commit 25bc628f8f
8 changed files with 273 additions and 223 deletions

View File

@ -72,11 +72,6 @@
<artifactId>cloud-common-api-doc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-core</artifactId>
@ -87,7 +82,11 @@
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.iotdb</groupId>
<artifactId>iotdb-session</artifactId>

View File

@ -1,6 +1,9 @@
package com.muyu.carData.config.kafkaconfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -19,6 +22,55 @@ public class KafkaConfig {
@Bean
public KafkaProducer kafkaProducer(){
HashMap<String, Object> configs = new HashMap<>();
return null;
configs.put("bootstrap.servers","60.204.221.52:9092");
configs.put("retries",2);
configs.put("batch.size",16384);
//生产者可用于缓冲等待发送到服务器的记录的总内存字节数
configs.put("buffer-memory",3554432);
/**
*producerleader,
*acks=0,0,producer.(socket)
* .,,(retries)(),-1.
*acks=1,1,leader,followerproducer.,
* follower,leader,.
*/
configs.put("acks","-1");
//指定key使用的序列化类
StringSerializer keySerializer = new StringSerializer();
//指定value使用的序列化类
StringSerializer valueSerializer = new StringSerializer();
//创建kafka生产者
return new KafkaProducer(configs, keySerializer, valueSerializer);
}
@Bean
public KafkaConsumer kafkaConsumer(){
HashMap<String, Object> configs = new HashMap<>();
configs.put("bootstrap.servers","60.204.221.52:9092");
//开启consumer的偏移量offset自动提交到kafka
configs.put("enable.auto.commit",true);
//偏移量自动提交的时间间隔,单位毫秒
configs.put("auto.commit.interval",5000);
//在Kafka中没有初始化偏移量或者当前偏移量不存在情况
//earliest, 在偏移量无效的情况下, 自动重置为最早的偏移量
//latest, 在偏移量无效的情况下, 自动重置为最新的偏移量
//none, 在偏移量无效的情况下, 抛出异常.
configs.put("auto.offset.reset","latest");
//请求阻塞的最大时间(毫秒)
configs.put("fetch.max.wait",500);
//请求应答的最小字节数
configs.put("fetch.min.size",1);
//心跳间隔时间(毫秒)
configs.put("heartbeat-interval",3000);
//一次调用poll返回的最大记录条数
configs.put("max.poll.records",500);
//指定消费组
configs.put("group.id","firstGroup");
//指定key使用的反序列化类
StringDeserializer keyDeserializer = new StringDeserializer();
//指定value使用的反序列化类
StringDeserializer valueDeserializer = new StringDeserializer();
//创建kafka消费者
return new KafkaConsumer(configs,keyDeserializer,valueDeserializer);
}
}

View File

@ -1,20 +1,15 @@
package com.muyu.carData.config.lotdbconfig;
import com.muyu.carData.domain.IoTDBRecord;
import com.muyu.carData.interfaces.IoTDBRecordable;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.session.SessionDataSet;
import org.apache.iotdb.session.util.Version;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.session.pool.SessionPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
@ -61,216 +56,25 @@ public class IotDBSessionConfig {
@Value("${spring.iotdb.maxSize}")
private Integer maxSize;
private static Session session;
/**
* session iotdb
* @return
*
*/
public Session getSession(){
@Value("${spring.iotdb.fetchSize}")
private int fetchSize;
if (null == session){
try {
logger.info("正在连接iotdb......");
session = new Session.Builder()
.host(ip)
.port(port)
.username(username)
.password(password)
.version(Version.V_0_13)
.build();
IotDBSessionConfig.session.open(false);
session.setFetchSize(maxSize);
//设置时区
session.setTimeZone("+08:00");
} catch (IoTDBConnectionException | StatementExecutionException e) {
logger.error(String.valueOf(e.getCause()));
}
@Bean
public Session iotSession(){
Session session = new Session(ip, port, username, password);
try {
session.open();
} catch (IoTDBConnectionException e) {
logger.error(String.valueOf(e.getCause()));
}
return session;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<String> getDeviceIds(List<? extends IoTDBRecordable> records) throws Exception {
List<String> deviceIds = new ArrayList<>();
for (IoTDBRecordable record : records) {
IoTDBRecord ioTDBRecord = record.toRecord();
String deviceId = ioTDBRecord.getDeviceId();
deviceIds.add(deviceId);
}
return deviceIds;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<Long> getTimes(List<? extends IoTDBRecordable> records) throws Exception {
List<Long> times = new ArrayList<>();
for (IoTDBRecordable record : records) {
long time = record.toRecord().getTime();
times.add(time);
}
return times;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<List<String>> getMeasurementsList(List<? extends IoTDBRecordable> records) throws Exception {
List<List<String>> measurementList = new ArrayList<>();
for (IoTDBRecordable record : records) {
List<String> iotDBRecord = record.toRecord().getMeasurementList();
measurementList.add(iotDBRecord);
}
return measurementList;
}
/**
*
* @param records
* @return
* @throws Exception
*/
public List<List<Object>> getValueList(List<? extends IoTDBRecordable> records) throws Exception {
List<List<Object>> valueList = new ArrayList<>();
for (IoTDBRecordable record : records) {
List<Object> iotDBRecord = record.toRecord().getValueList();
valueList.add(iotDBRecord);
}
return valueList;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<List<TSDataType>> getTypeList(List<? extends IoTDBRecordable> records) throws Exception {
List<List<TSDataType>> typeList = new ArrayList<>();
for (IoTDBRecordable record : records) {
IoTDBRecord tdbRecord = record.toRecord();
List<TSDataType> strList = new ArrayList<>();
for (String str : tdbRecord.getTypeList()) {
strList.add(TSDataType.valueOf(str));
}
typeList.add(strList);
}
return typeList;
}
/**
*
* @param type
* @return
*/
private TSDataType convertTypeByEntity(String type){
switch (type){
case "java.lang.Double":
return TSDataType.DOUBLE;
case "java.lang.Integer":
return TSDataType.INT32;
case "java.lang.Long":
return TSDataType.INT64;
case "java.lang.Boolean":
return TSDataType.BOOLEAN;
case "java.lang.Float":
return TSDataType.FLOAT;
default:
return TSDataType.TEXT;
}
}
/**
*
* @param records
*/
public void insertRecords(List<? extends IoTDBRecordable> records){
try {
session.insertRecords(getDeviceIds(records),
getTimes(records),getMeasurementsList(records),getTypeList(records),getValueList(records));
} catch (Exception e) {
logger.error("IotDB批量插入异常{}",e.getMessage());
}
}
public void insertRecord(IoTDBRecordable ioTDBRecordable){
try {
IoTDBRecord tdbRecord = ioTDBRecordable.toRecord();
List<TSDataType> typeList = new ArrayList<>();
for (String str : tdbRecord.getTypeList()) {
typeList.add(TSDataType.valueOf(str));
}
session.insertRecord(tdbRecord.getDeviceId(), tdbRecord.getTime(), tdbRecord.getMeasurementList(), typeList, tdbRecord.getValueList());
} catch (Exception e) {
logger.error("IotDB插入异常{}",e.getMessage());
}
}
/**
* sql
* @param sql sql
* @return
*/
public SessionDataSet query(String sql) throws IoTDBConnectionException, StatementExecutionException {
return session.executeQueryStatement(sql);
}
/**
*
* @param groupName
* @throws IoTDBConnectionException
* @throws StatementExecutionException
*/
public void deleteStorageGroup(String groupName) throws IoTDBConnectionException, StatementExecutionException {
session.deleteStorageGroup(groupName);
}
/**
*
*/
public void deleteTimeSeries(String timeSeries) throws IoTDBConnectionException, StatementExecutionException {
session.deleteTimeseries(timeSeries);
}
/**
*
* @param timeSeriesList
*/
public void batchDeleteTimeSeries(List<String> timeSeriesList) throws IoTDBConnectionException, StatementExecutionException {
session.deleteTimeseries(timeSeriesList);
}
/**
*
*/
public void deleteStorageGroupList(List<String> storageGroupList) throws IoTDBConnectionException, StatementExecutionException {
session.deleteStorageGroups(storageGroupList);
}
/**
*
* @param path
* @param endTime
*/
public void deleteDataByPathAndEndTime(String path,Long endTime){
}

View File

@ -1,7 +1,20 @@
package com.muyu.carData.consumer;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.muyu.carData.pojo.Student;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Collection;
/**
* @Author
* @Packagecom.muyu.carData.consumer
@ -10,8 +23,33 @@ import org.springframework.stereotype.Component;
* @Date2024/9/26 15:42
*/
@Component
public class MyKafkaConsumer {
@Log4j2
public class MyKafkaConsumer implements InitializingBean {
@Autowired
private KafkaConsumer kafkaConsumer;
private final String topicName = "test";
@Override
public void afterPropertiesSet() throws Exception {
log.info("启动线程开始监听topic:{}",topicName);
Thread thread = new Thread(() -> {
ThreadUtil.sleep(1000);
Collection<String> topics = Lists.newArrayList(topicName);
kafkaConsumer.subscribe(topics);
while (true){
ConsumerRecords<String,String> consumerRecords = kafkaConsumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {
//从consumerRecord中获取消费数据
String value = consumerRecord.value();
log.info("从Kafka中消费的原始数据{}",value);
//转换为java对象
Student stu = JSONUtil.toBean(value, Student.class);
log.info("消费数据转换为Java对象{}",stu);
}
}
});
thread.start();
}
}

View File

@ -1,9 +1,6 @@
package com.muyu.carData.pojo;
import com.muyu.carData.annotation.IoTTableName;
import com.muyu.carData.config.cacheconfig.ExpiryTime;
import com.muyu.carData.constract.IoTDBTableParam;
import com.muyu.carData.interfaces.IoTDBRecordable;
import lombok.*;
import lombok.experimental.SuperBuilder;
@ -20,8 +17,7 @@ import lombok.experimental.SuperBuilder;
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@IoTTableName(value = IoTDBTableParam.SYSLOG_IOT_TABLE)
public class Student extends ExpiryTime implements IoTDBRecordable {
public class Student extends ExpiryTime{
/**
*

View File

@ -1,4 +1,4 @@
package com.muyu.carData.controller;
package com.muyu.carData.testcontroller;
import com.github.benmanes.caffeine.cache.Cache;
import com.muyu.carData.config.cacheconfig.CaffeineConfig;
@ -18,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/testCache")
@Log4j2
public class TestController {
public class CacheController {
@Autowired
private CaffeineConfig caffeineConfig;

View File

@ -0,0 +1,111 @@
package com.muyu.carData.testcontroller;
import com.muyu.carData.config.lotdbconfig.IotDBSessionConfig;
import lombok.extern.log4j.Log4j2;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.write.record.Tablet;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @Author
* @Packagecom.muyu.carData.testcontroller
* @Projectcloud-server-8
* @nameIotDBController
* @Date2024/9/28 23:58
*/
@RequestMapping("/iotdb")
@RestController
@Log4j2
public class IotDBController {
@Autowired
private IotDBSessionConfig iotDBSessionConfig;
/**
*
* @param insertSize
* @param count
* @return
* @throws IoTDBConnectionException
* @throws StatementExecutionException
*/
@GetMapping("/insertData/{insertSize}/{count}")
public String insert(@PathVariable(name = "insertSize") int insertSize,@PathVariable(name = "count") int count) throws IoTDBConnectionException, StatementExecutionException {
Session session = iotDBSessionConfig.iotSession();
List<MeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("id", TSDataType.INT32));
schemaList.add(new MeasurementSchema("name", TSDataType.TEXT));
schemaList.add(new MeasurementSchema("sex", TSDataType.TEXT));
Tablet tablet = new Tablet("root.yang.baling", schemaList);
//以当前时间戳作为插入的起始时间戳
long timestamp = System.currentTimeMillis();
long beginTime = System.currentTimeMillis();
for (long row = 0;row < count; row++){
int rowIndex = tablet.rowSize++;
tablet.addTimestamp(rowIndex,timestamp); //批量插入的时间戳是数组形式所以需要使用rowIndex来指定插入的位置
tablet.addValue("id", rowIndex, ((row & 1) == 0 ? 1 : 0));
tablet.addValue("name", rowIndex, "name<=>" + row);
tablet.addValue("sex", rowIndex, "男");
if (tablet.rowSize == insertSize){
long bg = System.currentTimeMillis();
session.insertTablet(tablet);
tablet.reset();
log.info("已经插入了"+(row + 1)+",插入耗时:" + (System.currentTimeMillis() - bg));
}
timestamp++;
}
if (tablet.rowSize != 0){
session.insertTablet(tablet);
log.info("插入完成,耗时:" + (System.currentTimeMillis() - beginTime));
tablet.reset();
}
long endTime = System.currentTimeMillis();
log.info("插入了"+insertSize+"条数据,每次插入"+count+"条数据,总耗时:"+(endTime - beginTime));
return "时序性数据库测试success";
}
/* @GetMapping("/batchInsertRecords/{num}")
public String batchInsertRecords(@PathVariable Integer num){
String deviceId = "root.yang";
//属性(字段)
List<String> schemaList = new ArrayList<>();
schemaList.add("id");
schemaList.add("name");
schemaList.add("age");
ArrayList<TSDataType> tsDataTypes = new ArrayList<>();
tsDataTypes.add(TSDataType.INT32);
tsDataTypes.add(TSDataType.TEXT);
tsDataTypes.add(TSDataType.INT32);
List<Object> value = new ArrayList<>();
value.add(1);
value.add("张腾");
value.add(18);
ArrayList<String> deviceIds = new ArrayList<>();
ArrayList<Long> times = new ArrayList<>();
ArrayList<List<TSDataType>> typeList = new ArrayList<>();
ArrayList<List<Object>> valueList = new ArrayList<>();
for (int i = 0; i < num; i++) {
deviceIds.add()
}
}*/
}

View File

@ -0,0 +1,50 @@
package com.muyu.carData.testcontroller;
import com.alibaba.fastjson.JSONObject;
import com.muyu.carData.pojo.Student;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author
* @Packagecom.muyu.carData.testcontroller
* @Projectcloud-server-8
* @nameKafkaProducerController
* @Date2024/9/28 15:10
*/
@RestController
@RequestMapping("/produce")
@Log4j2
public class KafkaProducerController {
@Autowired
private KafkaProducer kafkaProducer;
private final String topicName = "test";
@GetMapping("/produceTest")
public String produceTest() {
try {
Student stu = Student.builder().id(2)
.name("杨闪闪")
.sex("男")
.build();
String stuStr = JSONObject.toJSONString(stu);
log.info("Topic:{}", topicName);
log.info("Java对象{}",stu);
log.info("转换为JSON{}",stuStr);
//使用KafkaProducer发送消息
ProducerRecord<String, String> stringProducerRecord = new ProducerRecord<>(topicName, stuStr);
kafkaProducer.send(stringProducerRecord);
}catch (Exception e){
log.error("Producer写入Topic异常,异常信息是:{}",e.getMessage());
}
return "消息发送成功";
}
}