当前位置: 首页 > 工具软件 > rrd4j > 使用案例 >

RRD数据库读写

太叔富
2023-12-01

RRD数据库读写

1、环境配置:

操作系统:Centos7.2
Java Version:1.8.0_131
RRD Version:3.1

2、代码

import javafx.util.Pair;
import org.rrd4j.ConsolFun;
import org.rrd4j.DsType;
import org.rrd4j.core.*;
import java.io.IOException;
import java.util.List;

public class RRDUtil {
    private String rrdName = "";
    private long rrdStart = 0;
    private long rrdStep = 0;
    private RrdDef rrdDef = null;
    private RrdDb rrdDb = null;

    /**
     * 1、定义RRD数据库
     * */
    public RRDUtil(String rrdName, long start, long step){
        this.rrdName = rrdName;
        this.rrdStart = start;
        this.rrdStep = step;
        this.rrdDb = null;
        this.rrdDef = new RrdDef(this.rrdName, this.rrdStart, this.rrdStep);
        this.rrdDef.setVersion(2);
    }

    /**
     * 2、定义RRD数据源
     * */
    public RRDUtil addDataSource(String dsName, DsType dsType, long heartbeat, double minValue, double maxValue){
        if(this.rrdDef == null || dsName == null || dsName.equals("")){
            return null;
        }
        this.rrdDef.addDatasource(dsName, dsType, heartbeat, minValue, maxValue);
        return this;
    }

    public RRDUtil addDataSource(DsDef dsDef){
        if(this.rrdDef == null || dsDef == null){
            return null;
        }
        this.rrdDef.addDatasource(dsDef);
        return this;
    }

    public RRDUtil addDataSources(List<DsDef> dsDefList){
        if(this.rrdDef == null || dsDefList == null){
            return null;
        }
        for(DsDef dsDef : dsDefList){
            this.rrdDef.addDatasource(dsDef);
        }
        return this;
    }

    /**
     * 3、定义RRD存档
     * */
    public RRDUtil addArchive(ConsolFun consolFun, double xff, int steps, int rows){
        this.rrdDef.addArchive(consolFun, xff, steps, rows);
        return this;
    }

    public RRDUtil addArchive(ArcDef arcDef){
        if(this.rrdDef == null || arcDef == null){
            return null;
        }
        this.rrdDef.addArchive(arcDef);
        return this;
    }

    public RRDUtil addArchives(List<ArcDef> arcDefsList){
        if(this.rrdDef == null || arcDefsList == null){
            return null;
        }
        for(ArcDef arcDef : arcDefsList){
            this.rrdDef.addArchive(arcDef);
        }
        return this;
    }

    /**
     * 4、构建,关闭数据库
     * */
    public RRDUtil build(){
        try {
            this.rrdDb = new RrdDb(this.rrdDef);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return this;
    }

    public void close(){
        try {
            this.rrdDb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 5、插入数据
     * */
    public boolean rrdUpdate(Pair<String, Double>...values){
        try {
            Sample sample = this.rrdDb.createSample();
            long time = System.currentTimeMillis() / 1000;
            sample.setTime(time);
            for(Pair<String, Double> val : values){
                sample.setValue(val.getKey(), val.getValue());
            }
            sample.update();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean rrdUpdate(String dsName, double value){
        try {
            Sample sample = this.rrdDb.createSample();
            long time = System.currentTimeMillis() / 1000;
            sample.setTime(time);
            sample.setValue(dsName, value);
            sample.update();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean rrdUpdate(double...values){
        try {
            Sample sample = this.rrdDb.createSample();
            long time = System.currentTimeMillis() / 1000;
            sample.setTime(time);
            sample.setValues(values);
            sample.update();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 6、查询数据
     * */
    public FetchData rrdQuery(ConsolFun fun, long start, long end){
        FetchData fetchData;
        FetchRequest request = this.rrdDb.createFetchRequest(fun, start, end, 1L);

        try {
            fetchData = request.fetchData();
            System.out.println("== Data fetched. " + fetchData.getRowCount() + " points obtained");
            System.out.println(fetchData.toString());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return fetchData;
    }

    public FetchData rrdQuery(ConsolFun fun, long start, long end, long resolution){
        FetchData fetchData;
        FetchRequest request = this.rrdDb.createFetchRequest(fun, start, end, resolution);

        try {
            fetchData = request.fetchData();
            System.out.println("== Data fetched. " + fetchData.getRowCount() + " points obtained");
            System.out.println(fetchData.toString());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return fetchData;
    }

    /**
     * 7、导出与恢复
     * */
    public void exportToXml(String xmlPath){
        try {
            rrdDb.exportXml(xmlPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static RrdDb restoreFromXml(String restoredPath, String xmlPath){
        RrdDb rrdDb = null;
        try {
            rrdDb = new RrdDb(restoredPath, xmlPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rrdDb;
    }
}

3、POM依赖

<dependency>
    <groupId>org.rrd4j</groupId>
    <artifactId>rrd4j</artifactId>
    <version>3.1</version>
</dependency>

4、测试

public static void main(String[] args) throws InterruptedException{
    final Random random = new Random();
    long START = System.currentTimeMillis() / 1000;
    RRDUtil rrdObj = new RRDUtil("demo.rrd", START - 1, 2);
    rrdObj.addDataSource("users", DsType.GAUGE, 4, 0, Double.NaN)
        .addDataSource("devices", DsType.GAUGE, 4, 0, Double.NaN)
        .addArchive(ConsolFun.AVERAGE, 0.5, 1, 1 * 2 * 60 * 60)
        .addArchive(ConsolFun.AVERAGE, 0.5, 5, 5 * 2 * 60 * 60)
        .build();

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            rrdObj.rrdUpdate(new Pair<>("users", 1.1 + random.nextInt(1000)),
            new Pair<>("devices", 2.2 + random.nextInt(1000)));
            //rrdObj.rrdUpdate("users", 1.0);
        }
    }, 100, 2000);

    while(true){
        rrdObj.rrdQuery(ConsolFun.AVERAGE, START, System.currentTimeMillis() / 1000, 2);
        Thread.sleep(2000);
    }
}
 类似资料: