Based DockerSwarm InfluxDB deployment and operational use of JAVA

Docker deployed InfluxDB


1, run container
$ 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 deployment

(1) first create a new Config, named influxdb.conf, reads as follows:

[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 = 1073741824
cache-snapshot-memory-size = 26214400
cache-snapshot-write-cold-duration = "10m0s"
compact-full-write-cold-duration = "4h0m0s"
max-series-per-database = 1000000
max-values-per-tag = 100000
max-concurrent-compactions = 0
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 = 0
max-connection-limit = 0
shared-secret = ""
realm = "InfluxDB"
unix-socket-enabled = false
bind-socket = "/var/run/influxdb.sock"
(2) docker swarm Kadomoto
version: '3.3'
services:
  influxdb:
    image: influxdb:1.2.0
    ports:
      - 8086:8086
      - 8083:8083
      - 2003:2003
    environment:
      INFLUXDB_DB: db0
      INFLUXDB_ADMIN_ENABLED: 1
      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
 
Description: InfluxDB since 1.2.0, canceled the WEB management page. So we are deploying a version 1.2.0, can be accessed after deployment: http: // <ip>: 8083 to access the management side.

3, insert the test using JAVA

(1) introducing Maven package:

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

 

(2) preparation of test insert code (online sticky paragraph):

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;


    publicInfluxDBConnect (username String, String password, URL String, String Database) {
         the this .username = username;
         the this .password = password;
         the this .url = URL;
         the this .database = Database; 
    } 

    / ** connection sequence database; obtained InfluxDB * * / 
    public influxDB Connection () {
         IF (influxDB == null ) { 
            influxDB = InfluxDBFactory.connect (URL, username, password); 
        } 
        return influxDB; 
    } 

    / ** 
     * set data retention policy
     * Defalut policy name / database database name / 30d data storage time of 30 days / 1 the number of copies is 1 / ending DEFAULT to set the default policy 
     * / 
    public  void createRetentionPolicy ( int retentionDay, int replicationCount) { 
        String the Command = String.format ( "The RETENTION POLICY the CREATE \"% S \ "the ON \"% S \ "DURATION% S% S Of REPLICATION the DEFAULT" ,
                 "defalut", Database, retentionDay + "D" , replicationCount);
         the this .query (Command); 
    } 

    / ** 
     * query 
     * @param the Command query 
     * @return 
     * / 
    public QueryResult query (the Command String) {
        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());
    }

    /**
     * Remove 
     * @param Command delete statement 
     * @return returns an error message
      * / 
    public String deleteMeasurementData (String Command) { 
        the QueryResult Result = influxDB.query ( new new Query (Command, Database));
         return result.getError (); 
    } 

    / * * 
     * create a database 
     * @param dbName library name
      * / 
    public  void the createDB (String dbName) {
         the this .query ( "the create database" + dbName); 
    } 

    / ** 
     * delete database 
     * @param dbName
     */
    public void deleteDB(String dbName) {
        this.query("drop database " + dbName);
    }

}
(3) call insert monitoring data

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) Effects:

 




 

Guess you like

Origin www.cnblogs.com/songxingzhu/p/11429227.html