InfluxDB的安装部署(docker)

本文介绍了如何使用Docker快速部署InfluxDB时序数据库,包括拉取镜像、创建容器,以及数据库的基本操作如创建、查看数据库,用户授权。此外,还提到了Java集成InfluxDB时序库的步骤。

1.拉取镜像
   docker pull tutum/influxdb
2.创建容器
    docker run -di \
       -p 8083:8083 \
       -p 8086:8086 \
       --expose 8090 \
       --expose 8099 \
       --name influxsrv \
       tutum/influxdb

直接访问访问部署的地址值:8083即可进去InfluxDB数据库的可视化页面

  1. 数据库的操作:

2.1创建数据库:

   CREATE DATABASE "category"    库名

2.2查看数据库:

   SHOW DATABASES

2.3用户授权

   Grant all privileges on cadvisor to admin    cadvisor :库名    admin    :  用户名

   Grant write on cadvisor to admin  给某个用户授权一个数据库的写操作

   Grant read on cadvisor to admin  给某个用户授权一个数据库的读操作

2.4 查看采集的数据

   Show measurements

3.java集成时序库

  3.1pom文件:

<dependency>
    <groupId>org.influxdb</groupId>
    <artifactId>influxdb-java</artifactId>
    <version>2.5</version>
</dependency>

 3.2java代码:

package com.tl.history.config;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;


import java.util.Map;

public class InfluxDbUtil {
    private static String openurl = "http://10.";//连接地址
    private static String username = "";//用户名
    private static String password = "";//密码
    private static String database = "";//数据库
    private static 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);
        }
        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, "730d", 1);
        this.query(command);
    }

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

    /**
     * 插入
     * @param tags 标签
     * @param fields 字段
     */
    public void insert(Map<String, String> tags, Map<String, Object> fields){
        Builder builder = Point.measurement(measurement);
        builder.tag(tags);
        builder.fields(fields);

        influxDB.write(database, "", 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){
        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(https://blog.csdn.net/String%20openurl) {
        this.openurl = openurl;
    }

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

例子:

(添加)

  influxDB = InfluxDbUtil.setUp();
            influxDB.createRetentionPolicy();
            for (Datainfo datainfo : list) {
                Map<String, String> tags = new HashMap<>();
                Map<String, Object> fields = new HashMap<>();
                Object value = redisTemplate.boundValueOps(datainfo.getXyid()).get();
                tags.put("tag", datainfo.getXyid());
                if (value == null){
                    fields.put("value", "");
                }else{
                    fields.put("value", value.toString());
                }
//                fields.put("TIMAMPEST",format);
                influxDB.insert(tags, fields);

(查询)

InfluxDbUtil influxDbUtil = InfluxDbUtil.setUp();
QueryResult query = influxDbUtil.query("select * from history where time >'2022-03-30' and time < '2022-03-31'");
System.out.println(query);
List<List<Object>> values = query.getResults().get(0).getSeries().get(0).getValues();
for (List<Object> value : values) {
    System.out.println(value);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值