一、基础连接类

  1. package com.test.repository.utils;
  2.  
  3. import com.test.domain.entry.bo.common.InfluxDbRow;
  4. import org.influxdb.InfluxDB;
  5. import org.influxdb.InfluxDBFactory;
  6. import org.influxdb.dto.BatchPoints;
  7. import org.influxdb.dto.Point;
  8. import org.influxdb.dto.Query;
  9. import org.influxdb.dto.QueryResult;
  10.  
  11. import java.util.List;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. public class InfluxDBConnect {
  15.  
  16. private String username;
  17. private String password;
  18. private String url;
  19. private String database;
  20. private int retentionDay;
  21. private int replicationCount;
  22.  
  23. private InfluxDB influxDB;
  24.  
  25. public InfluxDBConnect(String username, String password, String url, String database, int retentionDay, int replicationCount) {
  26. this.username = username;
  27. this.password = password;
  28. this.url = url;
  29. this.database = database;
  30. this.retentionDay = retentionDay;
  31. this.replicationCount = replicationCount;
  32. }
  33.  
  34. /** 连接时序数据库;获得InfluxDB **/
  35. void connection() {
  36. if (influxDB == null) {
  37. influxDB = InfluxDBFactory.connect(url, username, password);
  38. }
  39. }
  40.  
  41. /**
  42. * 设置数据保存策略
  43. * defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
  44. */
  45. void createRetentionPolicy() {
  46. String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
  47. "default", database, retentionDay + "d", replicationCount);
  48. this.query(command);
  49. }
  50.  
  51. /**
  52. * 查询
  53. * @param command 查询语句
  54. * @return 查询结果
  55. */
  56. QueryResult query(String command) {
  57. return influxDB.query(new Query(command, database));
  58. }
  59.  
  60. /**
  61. * 插入
  62. */
  63. public void insert(InfluxDbRow influxDbRow) {
  64. if (influxDbRow == null) {
  65. return;
  66. }
  67. Point.Builder builder = Point.measurement(influxDbRow.getMeasurement());
  68. builder.tag(influxDbRow.getTags());
  69. builder.fields(influxDbRow.getFields());
  70. if (influxDbRow.getTimeSecond() != null) {
  71. builder.time(influxDbRow.getTimeSecond(), TimeUnit.SECONDS);
  72. }
  73. influxDB.write(database, "default", builder.build());
  74. }
  75.  
  76. /**
  77. * 删除
  78. * @param command 删除语句
  79. * @return 返回错误信息
  80. */
  81. public String deleteMeasurementData(String command) {
  82. QueryResult result = influxDB.query(new Query(command, database));
  83. return result.getError();
  84. }
  85.  
  86. /**
  87. * 创建数据库
  88. * @param dbName 库名称
  89. */
  90. public void createDB(String dbName) {
  91. this.query("create database " + dbName);
  92. }
  93.  
  94. /**
  95. * 删除数据库
  96. * @param dbName
  97. */
  98. public void deleteDB(String dbName) {
  99. this.query("drop database " + dbName);
  100. }
  101.  
  102. public void close() {
  103. this.influxDB.close();
  104. }
  105.  
  106. /**
  107. * 指导导入
  108. * @param influxDbRows 行记录
  109. */
  110. public void batchPointsImport(List<InfluxDbRow> influxDbRows) {
  111. if (influxDbRows == null || influxDbRows.size() == 0) {
  112. return;
  113. }
  114. BatchPoints batchPoints = BatchPoints.database(this.database).retentionPolicy("default").build();
  115. for (InfluxDbRow influxDbRow : influxDbRows) {
  116. if (influxDbRow.getTags().size() + influxDbRow.getFields().size() == 0) continue;
  117. Point.Builder builder = Point.measurement(influxDbRow.getMeasurement());
  118. builder.tag(influxDbRow.getTags());
  119. builder.fields(influxDbRow.getFields());
  120. if (influxDbRow.getTimeSecond() != null) {
  121. builder.time(influxDbRow.getTimeSecond(), TimeUnit.SECONDS);
  122. } else {
  123. builder.time(System.currentTimeMillis() / 1000, TimeUnit.SECONDS);
  124. }
  125. batchPoints.point(builder.build());
  126. }
  127. influxDB.write(batchPoints);
  128. }
  129. }
  1. package com.test.repository.config;
  2.  
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Scope;
  8.  
  9. @Configuration
  10. @Slf4j
  11. public class InfluxDBConnectConfig {
  12.  
  13. @Value("${spring.influx.url}")
  14. private String url;
  15. @Value("${spring.influx.user}")
  16. private String username;
  17. @Value("${spring.influx.password}")
  18. private String password;
  19. @Value("${spring.influx.database}")
  20. private String database;
  21. @Value("${spring.influx.retentionDay}")
  22. private Integer retentionDay;
  23. @Value("${spring.influx.replicationCount}")
  24. private Integer replicationCount;
  25.  
  26. @Bean
  27. @Scope("prototype")
  28. public InfluxDBConnect influxDBConnectFactory() {
  29. if (this.retentionDay == null) this.retentionDay = 30;
  30. if (this.replicationCount == null) this.replicationCount = 1;
  31. return new InfluxDBConnect(username, password, url, database, retentionDay, replicationCount);
  32. }
  33.  
  34. }
 

JAVA操作InfluxDB的一个Demo的更多相关文章

  1. JDBC数据源(DataSource)数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用。

    JDBC数据源(DataSource)的简单实现   数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用. 2.数据源提供了一种简单获取数据库连接的方式,并能在内部通 ...

  2. java操作xml的一个小例子

    最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要 ...

  3. 「小程序JAVA实战」 小程序手写属于自己的第一个demo(六)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-06/ 自己尝试的写一个小demo,用到自定义样式,自定义底部导航,页面之间的跳转等小功能.官方文档 ...

  4. java操作Excel的poi 遍历一个工作簿

    遍历一个工作簿 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.h ...

  5. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  6. Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)

    在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...

  7. Java版 人脸识别SDK demo

    虹软人脸识别SDK之Java版,支持SDK 1.1+,以及当前最新版本2.0,滴滴,抓紧上车! 前言 由于业务需求,最近跟人脸识别杠上了,本以为虹软提供的SDK是那种面向开发语言的,结果是一堆dll· ...

  8. 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  9. (1)shiro简介和第一个demo

    之前一直在用shiro开发,不过只是会使用,并没有深入了解,最近有时间学习了一下,把最近学习所得分享一下. shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授 ...

随机推荐

  1. 使用git的几个常用指令

    1. 移除git:rm -rf .git/ 2.查看结果:ls -al 3.语法糖(查看列表):ll 4.将远程库的内容,更新到本地:git pull origin master: 5.添加到本地仓库 ...

  2. 3-剑指Offer: 连续子数组的最大和

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  3. 2019.6.11_MySQL进阶三:临时表

    临时表 临时表主要应用于保存一些临时数据.临时表只在当前连接可见.当关闭连接时,MySQL会自动删除表并且释放空间.临时表在MySQL 3.23版本中添加,低于 3.23版本就无法使用MySQL的临时 ...

  4. spring cloud fegin传递request header

    本文链接:https://blog.csdn.net/zhongzunfa/article/details/82791903 1.概述 今天一个朋友, 遇到一个如何在使用spring cloud fe ...

  5. Linux DNS分离解析与构建智能DNS服务器

    一 构建DNS分离解析 方法一 : [root@localhost ~]# vim /etc/named.conf [root@localhost ~]# cd /var/named/ [root@l ...

  6. 详解 ASP.NET Core MVC 的设计模式

    MVC 是什么?它是如何工作的?我们来解剖它 在本节课中我们要讨论的内容: 什么是 MVC? 它是如何工作的? 什么是 MVC MVC 由三个基本部分组成 - 模型(Model),视图(View)和控 ...

  7. 洛谷p3384【模板】树链剖分题解

    洛谷p3384 [模板]树链剖分错误记录 首先感谢\(lfd\)在课上调了出来\(Orz\) \(1\).以后少写全局变量 \(2\).线段树递归的时候最好把左右区间一起传 \(3\).写\(dfs\ ...

  8. 关于RAMOS所用的操作系统

    关于RAMOS所用的操作系统 RAMOS所用的操作系统,XP就不用说了,精简版最小的600MB到1.5GB的都有.现代意义上的WIN7/8/10 RAMOS一般选用精简版操作系统,节约内存的同时,还能 ...

  9. MySQL实战45讲学习笔记:第二十四讲

    一.引子 在前面的文章中,我不止一次地和你提到了 binlog,大家知道 binlog 可以用来归档,也可以用来做主备同步,但它的内容是什么样的呢?为什么备库执行了 binlog 就可以跟主库保持一致 ...

  10. Golang(九)简单 Goroutine 池实现

    0. 前言 最近使用 Golang 写一个并发执行的测试脚本 之前习惯使用 Java,习惯性想先建一个线程池.然后意识到 Golang 没有封装好的线程池 结合之前学习的 Goroutine 原理和 ...