JAVA操作InfluxDB的一个Demo
一、基础连接类
- package com.test.repository.utils;
- import com.test.domain.entry.bo.common.InfluxDbRow;
- import org.influxdb.InfluxDB;
- import org.influxdb.InfluxDBFactory;
- import org.influxdb.dto.BatchPoints;
- import org.influxdb.dto.Point;
- import org.influxdb.dto.Query;
- import org.influxdb.dto.QueryResult;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- public class InfluxDBConnect {
- private String username;
- private String password;
- private String url;
- private String database;
- private int retentionDay;
- private int replicationCount;
- private InfluxDB influxDB;
- public InfluxDBConnect(String username, String password, String url, String database, int retentionDay, int replicationCount) {
- this.username = username;
- this.password = password;
- this.url = url;
- this.database = database;
- this.retentionDay = retentionDay;
- this.replicationCount = replicationCount;
- }
- /** 连接时序数据库;获得InfluxDB **/
- void connection() {
- if (influxDB == null) {
- influxDB = InfluxDBFactory.connect(url, username, password);
- }
- }
- /**
- * 设置数据保存策略
- * defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
- */
- void createRetentionPolicy() {
- String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
- "default", database, retentionDay + "d", replicationCount);
- this.query(command);
- }
- /**
- * 查询
- * @param command 查询语句
- * @return 查询结果
- */
- QueryResult query(String command) {
- return influxDB.query(new Query(command, database));
- }
- /**
- * 插入
- */
- public void insert(InfluxDbRow influxDbRow) {
- if (influxDbRow == null) {
- return;
- }
- Point.Builder builder = Point.measurement(influxDbRow.getMeasurement());
- builder.tag(influxDbRow.getTags());
- builder.fields(influxDbRow.getFields());
- if (influxDbRow.getTimeSecond() != null) {
- builder.time(influxDbRow.getTimeSecond(), TimeUnit.SECONDS);
- }
- influxDB.write(database, "default", builder.build());
- }
- /**
- * 删除
- * @param command 删除语句
- * @return 返回错误信息
- */
- public String deleteMeasurementData(String command) {
- QueryResult result = influxDB.query(new Query(command, database));
- return result.getError();
- }
- /**
- * 创建数据库
- * @param dbName 库名称
- */
- public void createDB(String dbName) {
- this.query("create database " + dbName);
- }
- /**
- * 删除数据库
- * @param dbName
- */
- public void deleteDB(String dbName) {
- this.query("drop database " + dbName);
- }
- public void close() {
- this.influxDB.close();
- }
- /**
- * 指导导入
- * @param influxDbRows 行记录
- */
- public void batchPointsImport(List<InfluxDbRow> influxDbRows) {
- if (influxDbRows == null || influxDbRows.size() == 0) {
- return;
- }
- BatchPoints batchPoints = BatchPoints.database(this.database).retentionPolicy("default").build();
- for (InfluxDbRow influxDbRow : influxDbRows) {
- if (influxDbRow.getTags().size() + influxDbRow.getFields().size() == 0) continue;
- Point.Builder builder = Point.measurement(influxDbRow.getMeasurement());
- builder.tag(influxDbRow.getTags());
- builder.fields(influxDbRow.getFields());
- if (influxDbRow.getTimeSecond() != null) {
- builder.time(influxDbRow.getTimeSecond(), TimeUnit.SECONDS);
- } else {
- builder.time(System.currentTimeMillis() / 1000, TimeUnit.SECONDS);
- }
- batchPoints.point(builder.build());
- }
- influxDB.write(batchPoints);
- }
- }
- package com.test.repository.config;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Scope;
- @Configuration
- @Slf4j
- public class InfluxDBConnectConfig {
- @Value("${spring.influx.url}")
- private String url;
- @Value("${spring.influx.user}")
- private String username;
- @Value("${spring.influx.password}")
- private String password;
- @Value("${spring.influx.database}")
- private String database;
- @Value("${spring.influx.retentionDay}")
- private Integer retentionDay;
- @Value("${spring.influx.replicationCount}")
- private Integer replicationCount;
- @Bean
- @Scope("prototype")
- public InfluxDBConnect influxDBConnectFactory() {
- if (this.retentionDay == null) this.retentionDay = 30;
- if (this.replicationCount == null) this.replicationCount = 1;
- return new InfluxDBConnect(username, password, url, database, retentionDay, replicationCount);
- }
- }
JAVA操作InfluxDB的一个Demo的更多相关文章
- JDBC数据源(DataSource)数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用。
JDBC数据源(DataSource)的简单实现 数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用. 2.数据源提供了一种简单获取数据库连接的方式,并能在内部通 ...
- java操作xml的一个小例子
最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要 ...
- 「小程序JAVA实战」 小程序手写属于自己的第一个demo(六)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-06/ 自己尝试的写一个小demo,用到自定义样式,自定义底部导航,页面之间的跳转等小功能.官方文档 ...
- java操作Excel的poi 遍历一个工作簿
遍历一个工作簿 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.h ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)
在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...
- Java版 人脸识别SDK demo
虹软人脸识别SDK之Java版,支持SDK 1.1+,以及当前最新版本2.0,滴滴,抓紧上车! 前言 由于业务需求,最近跟人脸识别杠上了,本以为虹软提供的SDK是那种面向开发语言的,结果是一堆dll· ...
- 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- (1)shiro简介和第一个demo
之前一直在用shiro开发,不过只是会使用,并没有深入了解,最近有时间学习了一下,把最近学习所得分享一下. shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授 ...
随机推荐
- 使用git的几个常用指令
1. 移除git:rm -rf .git/ 2.查看结果:ls -al 3.语法糖(查看列表):ll 4.将远程库的内容,更新到本地:git pull origin master: 5.添加到本地仓库 ...
- 3-剑指Offer: 连续子数组的最大和
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- 2019.6.11_MySQL进阶三:临时表
临时表 临时表主要应用于保存一些临时数据.临时表只在当前连接可见.当关闭连接时,MySQL会自动删除表并且释放空间.临时表在MySQL 3.23版本中添加,低于 3.23版本就无法使用MySQL的临时 ...
- spring cloud fegin传递request header
本文链接:https://blog.csdn.net/zhongzunfa/article/details/82791903 1.概述 今天一个朋友, 遇到一个如何在使用spring cloud fe ...
- Linux DNS分离解析与构建智能DNS服务器
一 构建DNS分离解析 方法一 : [root@localhost ~]# vim /etc/named.conf [root@localhost ~]# cd /var/named/ [root@l ...
- 详解 ASP.NET Core MVC 的设计模式
MVC 是什么?它是如何工作的?我们来解剖它 在本节课中我们要讨论的内容: 什么是 MVC? 它是如何工作的? 什么是 MVC MVC 由三个基本部分组成 - 模型(Model),视图(View)和控 ...
- 洛谷p3384【模板】树链剖分题解
洛谷p3384 [模板]树链剖分错误记录 首先感谢\(lfd\)在课上调了出来\(Orz\) \(1\).以后少写全局变量 \(2\).线段树递归的时候最好把左右区间一起传 \(3\).写\(dfs\ ...
- 关于RAMOS所用的操作系统
关于RAMOS所用的操作系统 RAMOS所用的操作系统,XP就不用说了,精简版最小的600MB到1.5GB的都有.现代意义上的WIN7/8/10 RAMOS一般选用精简版操作系统,节约内存的同时,还能 ...
- MySQL实战45讲学习笔记:第二十四讲
一.引子 在前面的文章中,我不止一次地和你提到了 binlog,大家知道 binlog 可以用来归档,也可以用来做主备同步,但它的内容是什么样的呢?为什么备库执行了 binlog 就可以跟主库保持一致 ...
- Golang(九)简单 Goroutine 池实现
0. 前言 最近使用 Golang 写一个并发执行的测试脚本 之前习惯使用 Java,习惯性想先建一个线程池.然后意识到 Golang 没有封装好的线程池 结合之前学习的 Goroutine 原理和 ...