SpringBoot连接测试InfluxDB时序数据库
1)创建一个Springboot项目,在pom.xml引入influxDB相关的包
<!-- influxdb -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.8.10</version> <!-- 可以使用最新的稳定版本 -->
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.22</version> <!-- 确保使用支持的版本 -->
</dependency>
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>6.6.0</version>
</dependency>
2)application.yml添加配置信息
influxdb:
url: http://127.0.0.1:8017
username: root
password: wz1123456
token: vHqPBS1IPKo6ydzh-c5mqXnJn0h1HgHhbeD5M94BTRC0V49422iPyq3c6Vj9MYmamk3EjcivbtkqvwYPqigY3Q==
bucket: wzadministrator
org: wz_influxdb
3)新建InfluxDBConfig.java文件
@Configuration
public class InfluxDBConfig {
@Value("${influxdb.url}")
private String url;
@Value("${influxdb.token}")
private String token;
@Value("${influxdb.org}") // 组织名称
private String org;
@Value("${influxdb.bucket}") // 存储桶名称
private String bucket;
@Bean
public InfluxDBClient influxDBClient() {
return InfluxDBClientFactory.create(url, token.toCharArray(), org, bucket);
}
}
4)自动化测试InfluxDBDataReader.java,以插入数据为例,DevPointFiveSeDayShard类为实体类,自定义
@Component
public class InfluxDBDataReader {
String realTimePre = "REA:";
String recentTimePre = "REC:";
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private JedisPool jedisPool;
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss");
// 在你的类中添加 ObjectMapper 的实例
@Autowired
private ObjectMapper objectMapper;
@Autowired
private DevPointForMongoDBService devPointForMongoDBService;
@Autowired
private InfluxDBClient influxDBClient;
@Value("${influxdb.bucket}")
private String bucket;
@Value("${influxdb.org}")
private String org;
/**
* 定时任务启动,每隔5s,统一在AllAutoReader类中启动
*/
// @EventListener(ApplicationReadyEvent.class)
// @Scheduled(fixedRate = 5000)
// @PostConstruct
public void runInfluxDB() {
insertDevToInfluxDB();
}
public void insertDevToInfluxDB(){
try{
List<DevPointFiveSeDayShard> devPointFiveList = RandomlyGeneratEntityCongfig.GenerateEntity(30000);
List<Point> points = new ArrayList<>();
WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
for (DevPointFiveSeDayShard data : devPointFiveList) {
// 创建一个 Point 对象
Point point = Point.measurement("devPoint2") // measurement 名称
.addTag("productId", data.getProductId()) // tag
.addField("variableId", data.getVariableId()) // Field
.addField("calcValue", data.getCalcValue()) // field
.addField("errType", data.getErrType()) // field
.time(data.getReportDateTime(), WritePrecision.MS); // 设置时间戳,单位为纳秒
points.add(point);
}
// 插入数据
System.out.println("Writing points to bucket: " + bucket);
long startTime = System.currentTimeMillis();
writeApi.writePoints(points);
long endTime = System.currentTimeMillis();
System.out.println(LocalDateTime.now().format(formatter) + "存储InfluxDB数据耗时:" + (endTime - startTime) + "ms" + "存储量:" + devPointFiveList.size());
System.out.println("points插入完成==");
}catch (Exception e){
e.printStackTrace();
}
}
}
其他操作见官网文档:InfluxDB OSS v2 Documentation