influxDB

安装influxdb

#下载
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.3.x86_64.rpm
#安装
yum localinstall influxdb-1.8.3.x86_64.rpm

#启动
service influxdb start

#检查是否启动成功
service influxdb status

#PS:配置文件位于  vim  进行修改是否需要账号密码登录
/etc/influxdb/influxdb.conf

打开命令窗口

#本机
influx -precision rfc3339

#远程
influx -host 地址 -precision rfc3339

#-precision为设置显示时间格式
#rfc3339表示格式

结构

在这里插入图片描述
在这里插入图片描述

设置密码

打开命令窗口后查看用户

influx
show users;
#查看所有用户
exit
#退出

去配置文件influxdb.conf中,将auth-enabled项设置为true,在重启服务之前添加几个用户,并对其进行授权。

vim /etc/influxdb/influxdb.conf

再次打开influxdb命令窗口

influx
#添加一个管理员用户
create user "root" with password 'root' with all privileges

重启后

influxdb中使用用户名密码登录
influx -username root -password root

简单的增删改查


# 插入一条数据
INSERT cpu,host=serverA,region=us_west value=0.64
# 查看数据
SELECT "host", "region", "value" FROM "cpu"
# 往另一个表中插入数据
INSERT temperature,machine=unit42,type=assembly external=25,internal=37
# 查看所有内容
SELECT * FROM "temperature"
# 使用表名通配符,同时查看多个表中的多条记录
# SELECT * FROM /.*/ LIMIT 10
# 带有查询条件
SELECT * FROM "cpu_load_short" WHERE "value" > 0.9
# 删除数据
delete from "cpu" where host='serverA'


#删除数据的条件不能是field,因为field没有索引.但是可以是tags
#查询数据的条件可以是field

springboot整合influxDb

导包

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

配置yml文件

spring:
  influx:
    url: http://192.168.28.128:8086
    password: root
    user: root
    database: my_sensor1

配置类

package com.zhk.study.influxdb;

import java.util.concurrent.TimeUnit;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class InfluxdbConfig {

    @Value("${spring.influx.url}")
    private String influxDBUrl;

    @Value("${spring.influx.user}")
    private String userName;

    @Value("${spring.influx.password}")
    private String password;

    @Value("${spring.influx.database}")
    private String database;

    @Bean
    public InfluxDB influxdb(){
	  InfluxDB influxDB = InfluxDBFactory.connect(influxDBUrl, userName, password);
	  try {

		/**
		 * 异步插入:
		 * enableBatch这里第一个是point的个数,第二个是时间,单位毫秒
		 * point的个数和时间是联合使用的,如果满100条或者60 * 1000毫秒
		 * 满足任何一个条件就会发送一次写的请求。
		 */
		influxDB.setDatabase(database).enableBatch(100,1000 * 60, TimeUnit.MILLISECONDS);

	  } catch (Exception e) {
		e.printStackTrace();
	  } finally {
		//设置默认策略
		influxDB.setRetentionPolicy("sensor_retention");
	  }
	  //设置日志输出级别
	  influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
	  return influxDB;
    }
}

使用

package com.zhk.study.influxdb;
import java.util.ArrayList;
import java.util.List;

import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/influx/")
public class TestController {

    @Autowired
    private InfluxDB influxDB;

    //measurement 表名字
    private final String measurement = "sensor";

    @Value("${spring.influx.database}")
    private String database;
    /**
     * 批量插入第一种方式
     */
    @GetMapping("insert1")
    public String insert1(){
	  ArrayList<String> strings = new ArrayList<>();

	  for (int i = 0; i < 50; i++) {
	      //一条数据
		Point point = Point.measurement(measurement)
			  //所对应的有索引的字段
			  .tag("deviceId", "sensor" + i)
			  //所对应的无索引的字段
			  .addField("temp", 3)
			  .addField("voltage", 145 + i)
			  .addField("A1", "4i")
			  .addField("A2", "4i").build();
		strings.add(point.lineProtocol());
	  }
	  log.info("存储数据");

	  //写入
	  influxDB.write(strings);
	  return "succes" ;
    }
    /**
     * 批量插入第二种方式
     */
    @GetMapping("insert2")
    public String insert2(){

	  BatchPoints batchPoints = BatchPoints
		    .database(database)
		    .consistency(InfluxDB.ConsistencyLevel.ALL)
		    .build();
	  //遍历sqlserver获取数据
	  for(int i=0;i<50;i++){
		//创建单条数据对象——表名
		Point point = Point.measurement(measurement)
			  //tag属性——只能存储String类型
			  .tag("deviceId", "sensor" + i)
			  .addField("temp", 3)
			  .addField("voltage", 145+i)
			  .addField("A1", "4i")
			  .addField("A2", "4i").build();
		//将单条数据存储到集合中
		batchPoints.point(point);
	  }
	  log.info("存储数据");
	  influxDB.write(batchPoints);
	  return "succes" ;
    }

    /**
     * 获取数据
     */
    @GetMapping("query")
    public String query(@RequestParam(defaultValue = "1") Integer page){
        int pageSize = 10;
        //InfluxDB支持分页查询,一次可以设置发呢也查询条件
	  String pageQuery = "limit" + pageSize + "offsiz" + (page -1) * pageSize;
	  String queryCondition = "";  //查询条件暂且为空
	  String queryCmd = "SELECT * FROM "
		    // 查询指定设备下的日志信息
		    // 要指定从 RetentionPolicyName.measurement中查询指定数据,默认的策略可以不加;
		    // + 策略name + "." + measurement
		    + measurement
		    // 添加查询条件(注意查询条件选择tag值,选择field数值会严重拖慢查询速度)
		    + queryCondition
		    // 查询结果需要按照时间排序
		    + " ORDER BY time DESC"
		    // 添加分页查询条件
		    + pageQuery;
	  log.info("查询");

	  QueryResult query = influxDB.query(new Query(queryCmd, database));
	  return query.toString();
    }






}


<think>好的,用户想了解或使用InfluxDB,需要相关的使用指南或教程。首先,我需要根据提供的三个引用内容来整理相关信息,确保回答结构清晰,并且符合用户的要求。 首先,用户提到的三个引用都是关于InfluxDB的安装、配置基础操作的。引用[1]提到了InfluxDB的配置文件命令行启动步骤,引用[2]详细介绍了基础概念操作命令,引用[3]则是一个UI管理工具InfluxDB Studio的安装使用教程。我需要将这些内容整合起来,分步骤指导用户。 接下来,用户希望得到使用指南或教程,所以应该按照安装配置、基础概念、基础操作、可视化工具的顺序来组织内容。同时,用户的问题可能涉及到如何开始使用InfluxDB,包括安装、配置、数据操作管理界面,所以需要覆盖这些方面。 需要检查每个步骤是否引用了正确的来源。比如,安装InfluxDB的部分应引用引用[1][2],配置部分引用[1],基础概念引用[2],操作命令也是引用[2],而InfluxDB Studio的安装使用则引用[3]。 另外,用户可能对基础概念不太熟悉,因此需要详细解释Measurement、Point、Tag等术语,确保用户理解。在操作部分,需要提供具体的命令示例,比如创建数据库、插入数据、查询数据等,这些都是用户实际使用时会用到的。 最后,生成的相关问题需要围绕用户可能进一步关心的内容,比如性能优化、查询语言、与其他数据库对比等,确保覆盖用户可能的后续疑问。 需要确保回答中使用正确的LaTeX格式,但根据用户的问题,可能数学公式较少,重点在于代码块命令示例。因此,代码部分要使用正确的格式,如代码块包裹在三个反引号中,并注明语言类型。 最后检查是否所有引用都正确标注,例如配置文件修改启动步骤来自引用[1],基础概念来自引用[2],管理工具来自引用[3]。确保没有遗漏任何关键点,并且回答结构清晰,步骤明确,用户能够按照指引逐步操作。</think>### InfluxDB 使用指南与教程 #### 一、InfluxDB 安装与配置 1. **安装步骤** - 根据操作系统选择对应安装包(Windows/Linux/macOS),可通过官网或包管理器下载[^1] - 示例(Ubuntu): ```bash wget https://dl.influxdata.com/influxdb/releases/influxdb_2.7.1_amd64.deb sudo dpkg -i influxdb_2.7.1_amd64.deb ``` 2. **配置文件修改** - 修改 `influxdb.conf` 配置端口、存储路径等参数[^1] - 关键配置项: ```toml [http] enabled = true bind-address = ":8086" ``` 3. **启动服务** - 通过命令行启动: ```bash influxd --config /etc/influxdb/influxdb.conf ``` #### 二、基础概念(核心术语)[^2] 1. **Database**:数据存储的逻辑容器,类似关系型数据库中的库 2. **Measurement**:类似表,存储同一类时序数据(如 `cpu_usage`) 3. **Point**:单条数据记录,包含以下要素: - **Timestamp**:时间戳(主索引) - **Tags**:标签键值对(用于高效查询,如 `host=server01`) - **Fields**:实际指标值(如 `value=0.64`) 4. **Retention Policy**:数据保留策略,定义数据存储时长副本数 #### 三、基础操作命令 1. **数据库操作** ```sql CREATE DATABASE mydb -- 创建数据库 USE mydb -- 切换数据库 DROP DATABASE mydb -- 删除数据库 ``` 2. **写入数据** ```sql INSERT cpu_usage,host=server01 value=0.64 -- 插入一条数据(Measurement为cpu_usage) ``` 3. **查询数据** ```sql SELECT * FROM cpu_usage WHERE host='server01' AND time > now() - 1h ``` #### 四、可视化工具 InfluxDB Studio[^3] 1. **安装步骤** - 下载地址:https://gitcode.com/gh_mirrors/in/InfluxDBStudio - 支持 Windows/macOS,提供图形化界面管理数据库查询数据 2. **核心功能** - 连接多个 InfluxDB 实例 - 实时数据可视化与导出 - 执行批量数据操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大海里行船

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值