commit
fa3b9b2244
|
@ -0,0 +1,96 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-modules</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-modules-carData</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataSource -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataScope -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datascope</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common Log -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-log</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 接口模块 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<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>
|
||||
<version>0.13.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.carData;
|
||||
|
||||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData
|
||||
* @Project:cloud-server-8
|
||||
* @name:CarDataApplication
|
||||
* @Date:2024/9/26 15:33
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableMyFeignClients
|
||||
public class CarDataApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CarDataApplication.class,args);
|
||||
System.out.println("caused: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;;caused: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.muyu.carData.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.annotation
|
||||
* @Project:cloud-server-8
|
||||
* @name:IoTTableName
|
||||
* @Date:2024/9/27 19:29
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
|
||||
public @interface IoTTableName {
|
||||
|
||||
String value() default "";
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.carData.config.cacheconfig;
|
||||
import com.github.benmanes.caffeine.cache.Expiry;
|
||||
import org.checkerframework.checker.index.qual.NonNegative;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.cacheconfig
|
||||
* @Project:cloud-server-8
|
||||
* @name:CacheExpiry
|
||||
* @Date:2024/9/26 23:46
|
||||
*/
|
||||
public class CacheExpiry implements Expiry<String,ExpiryTime>{
|
||||
@Override
|
||||
public long expireAfterCreate(String key, ExpiryTime value, long currentTime) {
|
||||
return TimeUnit.SECONDS.toNanos(value.getExpiryTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long expireAfterUpdate(String key, ExpiryTime value, long currentTime, @NonNegative long currentDuration) {
|
||||
return TimeUnit.SECONDS.toNanos(value.getRefreshTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long expireAfterRead(String key, ExpiryTime value, long currentTime, @NonNegative long currentDuration) {
|
||||
return TimeUnit.SECONDS.toNanos(value.getRefreshTime());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.muyu.carData.config.cacheconfig;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.cacheconfig
|
||||
* @Project:cloud-server-8
|
||||
* @name:CaffeineConfig
|
||||
* @Date:2024/9/26 23:51
|
||||
*/
|
||||
@Configuration
|
||||
public class CaffeineConfig {
|
||||
|
||||
@Bean
|
||||
public Cache<String, ? extends ExpiryTime> caffeineCache(){
|
||||
CacheExpiry cacheExpiry = new CacheExpiry();
|
||||
return Caffeine.newBuilder()
|
||||
.expireAfter(cacheExpiry)
|
||||
.initialCapacity(128)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.muyu.carData.config.cacheconfig;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.cacheconfig
|
||||
* @Project:cloud-server-8
|
||||
* @name:ExpiryTime
|
||||
* @Date:2024/9/26 23:44
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ExpiryTime {
|
||||
|
||||
/**
|
||||
* 过期时间单位(秒)默认30分钟
|
||||
*/
|
||||
@Builder.Default
|
||||
private long expiryTime = 30 * 60;
|
||||
|
||||
/**
|
||||
* 刷新时间单位(秒) 默认15分钟
|
||||
*/
|
||||
@Builder.Default
|
||||
private long refreshTime = 15 * 60;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
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;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.config.kafkaconfig
|
||||
* @Project:cloud-server-8
|
||||
* @name:KafkaConfig
|
||||
* @Date:2024/9/28 12:19
|
||||
*/
|
||||
@Configuration
|
||||
public class KafkaConfig {
|
||||
|
||||
@Bean
|
||||
public KafkaProducer kafkaProducer(){
|
||||
HashMap<String, Object> configs = new HashMap<>();
|
||||
configs.put("bootstrap.servers","60.204.221.52:9092");
|
||||
configs.put("retries",2);
|
||||
configs.put("batch.size",16384);
|
||||
//生产者可用于缓冲等待发送到服务器的记录的总内存字节数
|
||||
configs.put("buffer-memory",3554432);
|
||||
/**
|
||||
*生产者producer要求leader节点在考虑完成请求之前收到的确认数,用于控制发送记录在服务端的持久化
|
||||
*acks=0,设置为0,则生产者producer将不会等待来自服务器的任何确认.该记录将立即添加到套接字(socket)缓冲区并视为已发送
|
||||
* .在这种情况下,无法保证服务器已收到记录,并且重试配置(retries)将不会生效(因为客户端通常不会知道任何故障),每条记录返回的偏移量始终设置为-1.
|
||||
*acks=1,设置为1,leader节点会把记录写入本地日志,不需要等待所有follower节点完全确认就会立即应答producer.在这种情况下,
|
||||
* 在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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package com.muyu.carData.config.lotdbconfig;
|
||||
|
||||
import org.apache.iotdb.rpc.IoTDBConnectionException;
|
||||
import org.apache.iotdb.session.Session;
|
||||
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.List;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.lotdbconfig
|
||||
* @Project:cloud-server-8
|
||||
* @name:IotDBSessionConfig
|
||||
* @Date:2024/9/27 12:25
|
||||
* IotDB线程池
|
||||
*/
|
||||
@Component
|
||||
@Configuration
|
||||
public class IotDBSessionConfig {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(IotDBSessionConfig.class);
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
@Value("${spring.iotdb.username}")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
@Value("${spring.iotdb.password}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* ip地址
|
||||
*/
|
||||
@Value("${spring.iotdb.ip}")
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 端口号
|
||||
*/
|
||||
@Value("${spring.iotdb.port}")
|
||||
private Integer port;
|
||||
|
||||
/**
|
||||
* 最大连接数
|
||||
*/
|
||||
@Value("${spring.iotdb.maxSize}")
|
||||
private Integer maxSize;
|
||||
|
||||
|
||||
/**
|
||||
* 默认每次查询的条数
|
||||
*/
|
||||
@Value("${spring.iotdb.fetchSize}")
|
||||
private int fetchSize;
|
||||
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.carData.constract;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.constract
|
||||
* @Project:cloud-server-8
|
||||
* @name:IoTDBTableParam
|
||||
* @Date:2024/9/27 20:02
|
||||
*/
|
||||
public class IoTDBTableParam {
|
||||
|
||||
public static final String SYSLOG_IOT_TABLE = "student";
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
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:张腾
|
||||
* @Package:com.muyu.carData.consumer
|
||||
* @Project:cloud-server-8
|
||||
* @name:MyKafkaConsumer
|
||||
* @Date:2024/9/26 15:42
|
||||
*/
|
||||
@Component
|
||||
@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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.muyu.carData.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.domain
|
||||
* @Project:cloud-server-8
|
||||
* @name:IoTDBRecord
|
||||
* @Date:2024/9/27 19:25
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class IoTDBRecord {
|
||||
|
||||
/**
|
||||
* 节点路径
|
||||
*/
|
||||
private String deviceId;
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private long time = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* 属性
|
||||
*/
|
||||
private List<String> measurementList;
|
||||
|
||||
/**
|
||||
* 属性值
|
||||
*/
|
||||
private List<Object> valueList;
|
||||
|
||||
/**
|
||||
* 数据类型
|
||||
*/
|
||||
private List<String> typeList;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.muyu.carData.interfaces;
|
||||
|
||||
import com.muyu.carData.annotation.IoTTableName;
|
||||
import com.muyu.carData.domain.IoTDBRecord;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.interfaces
|
||||
* @Project:cloud-server-8
|
||||
* @name:IoTDBRecordable
|
||||
* @Date:2024/9/27 19:22
|
||||
* iot基类
|
||||
*/
|
||||
public interface IoTDBRecordable {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(IoTDBRecordable.class);
|
||||
|
||||
/**
|
||||
* 数据载入方法
|
||||
* @return Record
|
||||
*/
|
||||
default IoTDBRecord toRecord() throws Exception {
|
||||
IoTDBRecord ioTDBRecord = new IoTDBRecord();
|
||||
Object getIoTDBTime = this.getClass().getMethod("getIoTDBTime").invoke(this);
|
||||
if (null != getIoTDBTime){
|
||||
ioTDBRecord.setTime((Long) getIoTDBTime);
|
||||
}
|
||||
Class aClass = this.getClass();
|
||||
IoTTableName name = this.getClass().getAnnotation(IoTTableName.class);
|
||||
ioTDBRecord.setDeviceId(name.value());
|
||||
Field[] declaredFields = aClass.getDeclaredFields();
|
||||
ArrayList<String> measurements = new ArrayList<>();
|
||||
ArrayList<Object> records = new ArrayList<>();
|
||||
ArrayList<String> types = new ArrayList<>();
|
||||
for (Field declaredField : declaredFields) {
|
||||
measurements.add(declaredField.getName());
|
||||
String methodNamePro = declaredField.getName().substring(0, 1).toUpperCase() + declaredField.getName().substring(1);
|
||||
Method methodName = this.getClass().getMethod("get" + methodNamePro);
|
||||
records.add(methodName.invoke(this));
|
||||
types.add(methodName.getReturnType().getName());
|
||||
}
|
||||
ioTDBRecord.setMeasurementList(measurements);
|
||||
ioTDBRecord.setValueList(records);
|
||||
ioTDBRecord.setTypeList(types);
|
||||
return ioTDBRecord;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.muyu.carData.pojo;
|
||||
|
||||
import com.muyu.carData.config.cacheconfig.ExpiryTime;
|
||||
import lombok.*;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.pojo
|
||||
* @Project:cloud-server-8
|
||||
* @name:Student
|
||||
* @Date:2024/9/27 0:40
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Student extends ExpiryTime{
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 姓名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private String sex;
|
||||
|
||||
/**
|
||||
* 插入时间
|
||||
*/
|
||||
private long time = System.currentTimeMillis();
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.muyu.carData.testcontroller;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.muyu.carData.config.cacheconfig.CaffeineConfig;
|
||||
import com.muyu.carData.pojo.Student;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @Author:张腾
|
||||
* @Package:com.muyu.carData.controller
|
||||
* @Project:cloud-server-8
|
||||
* @name:TestController
|
||||
* @Date:2024/9/26 23:56
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/testCache")
|
||||
@Log4j2
|
||||
public class CacheController {
|
||||
|
||||
@Autowired
|
||||
private CaffeineConfig caffeineConfig;
|
||||
|
||||
@RequestMapping("/caffeine")
|
||||
public String caffeine() throws InterruptedException {
|
||||
Cache<String, Student> stringCache = (Cache<String, Student>) caffeineConfig.caffeineCache();
|
||||
Student build = Student.builder().id(1)
|
||||
.name("小马")
|
||||
.sex("男")
|
||||
.expiryTime(20 * 60)
|
||||
.refreshTime(15 * 30)
|
||||
.build();
|
||||
stringCache.put("1", build);
|
||||
Thread.sleep(1000);
|
||||
//返回缓存的个数
|
||||
log.info(stringCache.estimatedSize());
|
||||
//返回缓存的数据
|
||||
log.info(stringCache.getIfPresent("1"));
|
||||
return "111";
|
||||
}
|
||||
|
||||
}
|
|
@ -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:张腾
|
||||
* @Package:com.muyu.carData.testcontroller
|
||||
* @Project:cloud-server-8
|
||||
* @name:IotDBController
|
||||
* @Date:2024/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()
|
||||
}
|
||||
}*/
|
||||
}
|
|
@ -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:张腾
|
||||
* @Package:com.muyu.carData.testcontroller
|
||||
* @Project:cloud-server-8
|
||||
* @name:KafkaProducerController
|
||||
* @Date:2024/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 "消息发送成功";
|
||||
}
|
||||
|
||||
}
|
|
@ -80,6 +80,7 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
<module>cloud-modules-system</module>
|
||||
<module>cloud-modules-gen</module>
|
||||
<module>cloud-modules-file</module>
|
||||
<module>cloud-modules-carData</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>cloud-modules</artifactId>
|
||||
|
|
Loading…
Reference in New Issue