JAVA多种向influxDB中插入数据方式

摘要:
包装测试仪表直到;importorg.influxdb.dto.Query;//用户名privatestaticStringpassword=“admin”;密码);influxDB.createDatabase(数据库);数据库));builder.fields(字段);
package cn.test.jmeter.util;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @author weisy
 * @date 2021/5/11
 * @Description
 */
public class InfluxDbUtil {
    private static String openurl = "http://IP:8086";//连接地址
    private static String username = "admin";//用户名
    private static String password = "admin";//密码
    private static String database = "jmeter";//数据库
    private String  measurement;//表名

    private InfluxDB influxDB;


    public InfluxDbUtil(String username, String password, String openurl, String database) {
        this.username = username;
        this.password = password;
        this.openurl = openurl;
        this.database = database;
    }

    public static InfluxDbUtil setUp() {
//创建 连接
        InfluxDbUtil influxDbUtil = new InfluxDbUtil(username, password, openurl, database);

        influxDbUtil.influxDbBuild();

//        influxDbUtil.createRetentionPolicy();

// influxDB.deleteDB(database);
// influxDB.createDB(database);
        return influxDbUtil;
    }

    /**
     * 连接时序数据库;获得InfluxDB
     **/
    public InfluxDB influxDbBuild() {
        if (influxDB == null) {
            influxDB = InfluxDBFactory.connect(openurl, username, password);
            influxDB.createDatabase(database);
//            influxDB.setRetentionPolicy("one_month");
        }
        return influxDB;
    }

    /**
     *  * 设置数据保存策略
     *  * defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
     *  
     */
//    public void createRetentionPolicy() {
//        String command = String.format("CREATE RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %s DEFAULT",
//                "defalut", database, "30d", 1);
//        this.query(command);
//    }

    /**
     *  * 查询
     *  * @param command 查询语句
     *  * @return
     *  
     */
    public QueryResult query(String command) {
        return influxDB.query(new Query(command, database));
    }

    /**
     *  * 插入
     *  * @param tags 标签
     *  * @param fields 字段
     *  
     */
    /*
     * 单条插入:多个tag多个field
     * */
    public void insert1(Map<String, String> tags, Map<String, Object> fields,String measurement) {
        Builder builder = Point.measurement(measurement);
        builder.tag(tags);
        builder.fields(fields);

        influxDB.write(database, "", builder.build());
    }

    /*
    * 单条插入:多个tag多个field自定义时间戳
    * */
    public void insert2(Map<String, String> tags, Map<String, Object> fields,String measurement,Long timeStamp,TimeUnit timeUnit) {
        Builder builder = Point.measurement(measurement);
        builder.tag(tags);
        builder.fields(fields);
        if (0 != timeStamp) {
            builder.time(timeStamp, timeUnit);
        }
        influxDB.write(database, "", builder.build());
    }

    /*
    * 单条插入:单条tag单条field
    * */
    public void insert3(String tags, Integer fields,String measurement,Long timeStamp) {

        Point point = Point.measurement(measurement)
                .time(timeStamp, TimeUnit.MILLISECONDS)
                .tag("label", tags)
                .addField("value", fields).build();
        influxDB.write("jmeter", "", point);
    }


    /*
    * 批量插入
    * */
    public void insertBatch(ArrayList<Map<String, String>> sqlserverlist, String measurement) {

        BatchPoints batchPoints = BatchPoints
                .database("jmeter")
                .build();
        //遍历sqlserver获取数据
        for(Map<String, String> map : sqlserverlist) {
            //创建单条数据对象——表名
            Point point = Point.measurement(measurement)
                    .time(Long.parseLong(map.get("timeStamp")), TimeUnit.MILLISECONDS)
                    //tag属性——只能存储String类型
                    .tag("label", map.get("label"))
                    //field存储数据
                    .addField("value", 1)
                    .addField("rt", map.get("rt"))
                    .build();
            //将单条数据存储到集合中
            batchPoints.point(point);
        }
        influxDB.write(batchPoints);
    }

    public void insertBatch2(ArrayList<Map<String, String>> sqlserverlist, String measurement) {

        BatchPoints batchPoints = BatchPoints
                .database("jmeter")
                .build();
        //遍历sqlserver获取数据
        for(Map<String, String> map : sqlserverlist) {
            //创建单条数据对象——表名
            Point point = Point.measurement(measurement)
                    .time(Long.parseLong(map.get("timeStamp")), TimeUnit.MILLISECONDS)
                    //tag属性——只能存储String类型
                    .tag("label", map.get("label"))
                    //field存储数据
                    .addField("value", Integer.valueOf(map.get("value")))
                    .build();
            //将单条数据存储到集合中
            batchPoints.point(point);
        }
        influxDB.write(batchPoints);
    }

    /**
     *  * 删除
     *  * @param command 删除语句
     *  * @return 返回错误信息
     *  
     */
    public String deleteMeasurementData(String command) {
        QueryResult result = influxDB.query(new Query(command, database));
        return result.getError();
    }

    /**
     *  * 创建数据库
     *  * @param dbName
     *  
     */
    @SuppressWarnings("deprecation")
    public void createDB(String dbName) {
        influxDB.createDatabase(dbName);
    }



    /**
     *  * 删除数据库
     *  * @param dbName
     *  
     */
    public void deleteDB(String dbName) {
        influxDB.deleteDatabase(dbName);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getOpenurl() {
        return openurl;
    }

    public void setOpenurl(String openurl) {
        this.openurl = openurl;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public static void main(String[] args) {

        InfluxDbUtil influxDB = InfluxDbUtil.setUp();
        Map<String, String> tags = new HashMap<>();
        Map<String, Object> fields = new HashMap<>();
        tags.put("TAG_NAME","abc");
        fields.put("TAG_VALUE","111");
//        try {
//            fields.put("TIMAMPEST", DateUtil.getCurrentDateStr());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        influxDB.insert1(tags, fields,"table2");
        //查询
        QueryResult result=  influxDB.query("select *from table2 order by time");
        System.out.println(result);
    }

}

免责声明:文章转载自《JAVA多种向influxDB中插入数据方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇微信小程序根据生日获取年龄linux安装nodejs下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

在SQLAlchemy ORM中动态变更表名

在开发过程中,经常会遇到几张表结构相同,仅仅表名不一样。这在直接使用SQL语句进行查询的环境中处理起来很简单,但如果使用了SQLAlchemy ORM之后,因在model定义时就确定了表名,就需要用其他方法进行表名的变更。 假定数据库中有两张表:user,user_1,下面用一个简单程序展示如何在查询时变更表名。  使用declarative_base定义...

如何使用Notepad++快速整理挤在一起的CallStack信息(将换行符作为被替换的内容)

问题描述 ===================== 经常需要在SharePoint的ULS日志中捞取一些带有callstack的信息, 而且需要把callstack展开细细查看. 然而, ULS log中的callstack是挤在一起的, 查看起来很不方便. 笔者原来一直手工添加换行符, 但是近期这类工作实在有点多, 于是开始想办法了. 问题举例, 笔者...

FusionCharts属性大全

属性的分类就以官方的API文档为准吧: 1.Chart: <1>Functional Attributes(功能属性) <2>Titles and AxisNames(标题和坐标抽名字) <3> Charts Cosmetics(图表美容属性) <4>Divisional Lines/Grids(...

java导出生成word

最近做的项目,需要将一些信息导出到word中。在网上找了好多解决方案,现在将这几天的总结分享一下。 目前来看,java导出word大致有6种解决方案:   1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。D...

JAVA读取yml配置文件指定key下的所有内容

先引入需要的依赖 <!--读取yml文件--> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId>...

Sqlite(数据库)

       1.Sqlite数据类型:NULL. 空值、INTEGER. 整型、REAL.浮点型、TEXT.文本类型、BLOB. 二进制类型,用来存储文件,比如图片       2.使用sqlite3需要先导入libsqlite3.dylib并导入主头文件       3. 设置数据库文件存放路径,如沙盒的Doucuments文件夹内       NSS...