Docker中部署InfluxDB


1、运行容器
$ docker run --rm \
-e INFLUXDB_DB=db0 -e INFLUXDB_ADMIN_ENABLED=true \
-e INFLUXDB_ADMIN_USER=admin -e INFLUXDB_ADMIN_PASSWORD=supersecretpassword \
-e INFLUXDB_USER=telegraf -e INFLUXDB_USER_PASSWORD=secretpassword \
-v $PWD:/var/lib/influxdb \
influxdb /init-influxdb.sh
2、Stack部署

(1)先新建一个Config,名称为influxdb.conf,内容如下:

[meta]
dir = "/var/lib/influxdb/meta"
retention-autocreate = true
logging-enabled = true [data]
dir = "/var/lib/influxdb/data"
index-version = "inmem"
wal-dir = "/var/lib/influxdb/wal"
wal-fsync-delay = "0s"
query-log-enabled = true
cache-max-memory-size =
cache-snapshot-memory-size =
cache-snapshot-write-cold-duration = "10m0s"
compact-full-write-cold-duration = "4h0m0s"
max-series-per-database =
max-values-per-tag =
max-concurrent-compactions =
trace-logging-enabled = false
[http]
enabled = true
bind-address = ":8086"
auth-enabled = false
log-enabled = true
write-tracing = false
pprof-enabled = true
https-enabled = false
https-certificate = "/etc/ssl/influxdb.pem"
https-private-key = ""
max-row-limit =
max-connection-limit =
shared-secret = ""
realm = "InfluxDB"
unix-socket-enabled = false
bind-socket = "/var/run/influxdb.sock"
(2) docker swarm角本
version: '3.3'
services:
influxdb:
image: influxdb:1.2.
ports:
- :
- :
- :
environment:
INFLUXDB_DB: db0
INFLUXDB_ADMIN_ENABLED:
INFLUXDB_ADMIN_USER: admin
INFLUXDB_ADMIN_PASSWORD: admin
INFLUXDB_USER: user
INFLUXDB_USER_PASSWORD: user
volumes:
- /opt/docker/influxdb/data:/var/lib/influxdb
configs:
- source: influxdb.conf
target: /etc/influxdb/influxdb.conf
configs:
influxdb.conf:
external: true

说明:InfluxDB自1.2.0之后,取消了WEB管理页面。因此我们部署的是1.2.0版本,部署完后可以访问:http://<ip>:8083来访问管理端。
3、使用JAVA插入测试

(1)引入Maven包:

<!-- https://mvnrepository.com/artifact/org.influxdb/influxdb-java -->
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.15</version>
</dependency>

(2)编写测试插入代码(网上粘一段):

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; /**
* 时序数据库 InfluxDB 连接
*/
public class InfluxDBConnect { private String username;
private String password;
private String url;
private String database; private InfluxDB influxDB; public InfluxDBConnect(String username, String password, String url, String database) {
this.username = username;
this.password = password;
this.url = url;
this.database = database;
} /** 连接时序数据库;获得InfluxDB **/
public InfluxDB connection() {
if (influxDB == null) {
influxDB = InfluxDBFactory.connect(url, username, password);
}
return influxDB;
} /**
* 设置数据保存策略
* defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
*/
public void createRetentionPolicy(int retentionDay, int replicationCount) {
String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
"defalut", database, retentionDay + "d", replicationCount);
this.query(command);
} /**
* 查询
* @param command 查询语句
* @return
*/
public QueryResult query(String command) {
return influxDB.query(new Query(command, database));
} /**
* 插入
* @param measurement 表
* @param tags 标签
* @param fields 字段
*/
public void insert(String measurement, 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) {
this.query("create database " + dbName);
} /**
* 删除数据库
* @param dbName
*/
public void deleteDB(String dbName) {
this.query("drop database " + dbName);
}
    /**
* 插入
*/
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());
}
}
(3)调用插入监控数据
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import org.junit.Before;
import org.junit.Test; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map; /**
* @author song 2019/8/27 16:28
*/
public class InsertTest {
private InfluxDBConnect influxdb;
private String username = "admin";//用户名
private String password = "admin";//密码
private String openurl = "http://54.222.182.173:8086";//连接地址
private String database = "db0";//数据库
private String measurement = "sys_code"; @Before
public void setUp() {
influxdb = new InfluxDBConnect(username, password, openurl, database);
influxdb.connection();
influxdb.createRetentionPolicy(30, 1);
} @Test
public void testInsert() throws IOException {
String data = new String(Files.readAllBytes(Paths.get("C:\\Aliyun\\实例监控数据.json")));
JSONObject jobj = JSON.parseObject(data);
JSONArray array = jobj.getJSONObject("MonitorData").getJSONArray("InstanceMonitorData");
for (Object o : array) {
JSONObject obj = (JSONObject) o;
Map<String, Object> fields = new HashMap<>();
long time = TypeUtils.castToDate(obj.get("TimeStamp")).getTime() / 1000;
fields.put("TimeStamp", time);
fields.put("IOPSRead", obj.get("IOPSRead"));
fields.put("IOPSWrite", obj.get("IOPSWrite"));
fields.put("IntranetBandwidth", obj.get("IntranetBandwidth"));
fields.put("BPSRead", obj.get("BPSRead"));
fields.put("BPSWrite", obj.get("BPSWrite"));
fields.put("IntranetTX", obj.get("IntranetTX"));
fields.put("IntranetRX", obj.get("IntranetRX"));
fields.put("CPU", obj.get("CPU"));
fields.put("InternetRX", obj.get("InternetRX"));
fields.put("InternetTX", obj.get("InternetTX"));
Map<String, String> tags = new HashMap<>();
tags.put("InstanceId", obj.get("InstanceId").toString());
influxdb.insert("ecs_time2", tags, fields);
}
} }

(4)效果:


按时间的查询,可以是:SELECT * FROM "aliyun_ecs" WHERE time >'2019-08-27T14:39:00Z' and time < '2019-08-27T15:39:00Z'

基于DockerSwarm 部署InfluxDB并使用JAVA操作的更多相关文章

  1. 一些基本的操作,编译,构建,单元测试,安装,网站生成和基于Maven部署项目。

    一些基本的操作,编译,构建,单元测试,安装,网站生成和基于Maven部署项目. 使用Maven构建项目“mvn package” 来构建项目 使用Maven清理项目“mvn clean” 来清理项目 ...

  2. Java操作Excle(基于Poi)

    有一次有个同事问我会不会有java操作Excle,回答当然是不会了!感觉被嘲讽了,于是开始寻找度娘,找到个小例子,结果越写越有意思,最后就成就了这个工具类. import java.io.Buffer ...

  3. 2 - 基于ELK的ElasticSearch 7.8.x技术整理 - java操作篇 - 更新完毕

    3.java操作ES篇 3.1.摸索java链接ES的流程 自行创建一个maven项目 3.1.1.依赖管理 点击查看代码 <properties> <ES-version>7 ...

  4. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

  5. [系统集成] 基于Kubernetes 部署 jenkins 并动态分配资源

    基于kubernetes 部署 jenkins master 比较简单,难点是为 jenkins 动态分配资源.基于kubernetes 为 jenkins 动态分配资源需要实现下述功能: 资源分配: ...

  6. 在Jena框架下基于MySQL数据库实现本体的存取操作

    在Jena框架下基于MySQL数据库实现本体的存取操作 转自:http://blog.csdn.net/jtz_mpp/article/details/6224311 最近在做一个基于本体的管理系统. ...

  7. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  8. Java操作队列

    Java操作队列 常见的几种模式:   1 简单队列simple 模型:(p + 队列 + c) P:生产者producer,将消息发送到队列 红色:消息队列 C:消费者consumer,从队列消费消 ...

  9. 4 kafka集群部署及kafka生产者java客户端编程 + kafka消费者java客户端编程

    本博文的主要内容有   kafka的单机模式部署 kafka的分布式模式部署 生产者java客户端编程 消费者java客户端编程 运行kafka ,需要依赖 zookeeper,你可以使用已有的 zo ...

随机推荐

  1. win10上安装ubunt18双系统过程中出现mmx64.efi not found问题

    安装Ubuntu18过程中,从u盘启动ubunt安装,出现mmx64.efi not found问题 如下图: 制作好ubunt启动盘之后在EFI/BOOT下会看到两个文件,将其中grubx64.ef ...

  2. Python元组与字符串操作(9)——随机数、元组、命名元组

    随机数 import random #导入random模块 randint(a,b) 返回[a,b]之间的整数 random.randint(0,9) randrange([start],stop,[ ...

  3. ulimit 更改 gcc升级 查看显卡状态命令

    一.更改ulimit: vim /etc/security/limits.conf 在文件最下方添加以下内容 * soft nofile 65536* hard nofile 65536 二. gcc ...

  4. FollowUp CRM是什么,有什么作用,好不好

    FollowUp,基于Gmail的私人CRM: 是一款Chrome插件,构建在Gmail邮箱服务之上: FollowUp支持通过Gmail:设置提醒,编写备注,计划会议,查看下一步的内容等: Foll ...

  5. 吴丽丽-201871010123《面向对象程序设计(Java)》第七周学习总结

    吴丽丽-201871010123<面向对象程序设计(Java)>第七周学习总结 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个 ...

  6. Java8——Stream

    /* * 一.Stream API 的操作步骤: * * 1. 创建 Stream * * 2. 中间操作 * * 3. 终止操作(终端操作) */ public class TestStreamaA ...

  7. ibatis<iterate>标签

    <iterate  property="" 从传入的参数集合中使用属性名去获取值,   这个必须是一个List类型,   否则会出现OutofRangeException, ...

  8. 5.Vue的组件

    1.什么是组件 组件是可复用的Vue实例,也就是一组可以复用的模版,类似JSTL的自定义标签. 你可能会有页头.侧边栏.内容区等组件,每个组件又包含了其它的像导航链接.博文之类的组件. 2.第一个Vu ...

  9. c# 多线程多个参数

    for (int i = 0; i <count; i++) //根据选择的串口号数量创建对应数量的线程 { thread = new Thread(new ParameterizedThrea ...

  10. Bootstrap-table实现动态合并相同行

    Bootstrap-table  表格合并相同名字的列 @编写function() /** * 合并行 * @param data 原始数据(在服务端完成排序) * @param fieldName ...