一、基础连接类

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的更多相关文章

  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. 探究分析---利用sql批量更新部分时间的同比数据

    问题:如何将social_kol_tmp表 中的字段cost_YA中日期为201901-201909中的值替换为相同brand和pltform对应18年月份的col_cost字段的数据,其他日期的co ...

  2. Git分支管理(四)

    一.什么是分支 分支的好处: 同时并行推进多个功能开发,提高开发效率 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任 何影响.失败的分支删除重新开始即可. 二.分支的操作 1. 创建 ...

  3. Zookeeper注册中心搭建-单机版(三)

    Zookeeper是一个分布式协调组件,本质是一个软件. Zookeeper常用的功能有: 发布订阅功能,把 zookeeper 当作注册中心的原因. 分布式/集群管理功能 Zookeeper是Jav ...

  4. Spring Cloud微服务安全实战_4-5_搭建OAuth2资源服务器

    上一篇搭建了一个OAuth2认证服务器,可以生成token,这篇来改造下之前的订单微服务,使其能够认这个token令牌. 本篇针对订单服务要做三件事: 1,要让他知道自己是资源服务器,他知道这件事后, ...

  5. 11/10 <priorityQueue> 215 347

    215. Kth Largest Element in an Array 快速排序法,选择一个数,比这个数大的交换到左边,比这个数小的交换到右边. class Solution { public in ...

  6. 【Step-By-Step】第 三 周

    本周面试题一览: 什么是XSS攻击,XSS 攻击可以分为哪几类?我们如何防范XSS攻击? 如何隐藏页面中的某个元素? 浏览器事件代理机制的原理是什么? setTimeout 倒计时为什么会出现误差? ...

  7. DVWA File Upload 通关教程

    File Upload,即文件上传.文件上传漏洞通常是由于对上传文件的类型.内容没有进行严格的过滤.检查,使得攻击者可以通过上传木马获取服务器的webshell权限,因此文件上传漏洞带来的危害常常是毁 ...

  8. python 获取日期以及时间

    >>> import datetime >>> >>> i = datetime.datetime.now() >>> prin ...

  9. AtCoder Grand Contest 039 简要题解

    从这里开始 比赛目录 Problem A Connection and Disconnection 简单讨论即可. Code #include <bits/stdc++.h> using ...

  10. .NET Core:Token认证

    现在是WebAPI的时代,你所需要面对的不止是浏览器了,通常会使用Web, WebApp, NativeApp等多种呈现方式.其中诸如Ember,Angular,Backbone之类的前端框架类库正随 ...