ProcessDB实时/时序数据库——JAVA_API检索一段时间的历史统计数据

该代码示例展示了一个Java程序,用于连接数据库并按名称分页查询历史统计数据。它使用特定的库(如DBConnectManager和DBHistoryDataDao)进行数据库操作,处理日期范围,以及获取和解析统计数据,包括最大值、最小值、平均值等信息。
摘要由CSDN通过智能技术生成

示例代码:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.processdb.connection.DBConnect;
import com.processdb.connection.DBConnectManager;
import com.processdb.driver.common.ConnectInfo;
import com.processdb.driver.common.ConstantValue;
import com.processdb.driver.dao.DBHistoryDataDao;
import com.processdb.driver.record.PointHistory;
import com.processdb.driver.record.StatsRecord;
import com.processdb.exception.DBException;

public class QueryHistoryStatDataByNamePagingTest {

    static SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        //建立实时库链接
        ConnectInfo host = new ConnectInfo();
        host.setConnectName("testConn");    //链接名称
        host.setHostName("127.0.0.1");        //实时库所在服务器的IP地址
        host.setHostPort(8301);                //实时库程序端口号
        host.setPassword("root");            //登录实时库的用户名
        host.setUserName("root");            //登录实时库的密码
        DBConnect myConn = null;
        try {
            myConn = DBConnectManager.getInstance().getConnect(host);    //建立链接
        } catch (DBException e1) {
            e1.printStackTrace();
        }
        
        QueryHistoryStatDataByNamePagingTest tester = new QueryHistoryStatDataByNamePagingTest();

        String pointNameArray[] = {"public.sys.PDB_OS_CPU_USAGE"};

        Date begindate = null;//统计开始时间
        Date enddate = null;//统计结束时间
        try {
            begindate = dateFmt.parse("2023-02-09 10:00:00");
            enddate = dateFmt.parse("2023-02-09 11:00:00");
        }catch (Exception pe) {
            pe.printStackTrace();
            return;
        }
        int step = (int)enddate.getTime()/1000 - (int)begindate.getTime()/1000;//统计周期
        byte query_type = ConstantValue.HIS_VALUE_STAT;//检索历史类型:统计历史数据

        Vector<PointHistory> pointList = new Vector<PointHistory>();
        for(int i=0; i<pointNameArray.length; i++) {
            //构造检索对象
            PointHistory ph = new PointHistory(pointNameArray[i], begindate, enddate, step, query_type);
            pointList.add(ph);
        }
        
        //执行检索操作
        int res = DBHistoryDataDao.requestPointHistoryByName(myConn,pointList);
        if(res != 0)
        {
            System.out.println("requestPointHistoryByName: failed, error code: " + res);
            return;
        }

        for(int ii=0; ii<pointList.size(); ii++) {
            PointHistory ph = pointList.elementAt(ii);
            //判断当前检索点是否存在错误
            if(ph.getReturnCode() != 0)
            {
                System.out.println("Point Name: " + ph.getPointName() + ", error code: " + ph.getReturnCode());
                continue;
            }
            Vector<StatsRecord> statsRecodList = ph.getStatsRecords();
            int counts = statsRecodList.size();
            System.out.println("Point Name: " + ph.getPointName());
            System.out.println("Statistic Record Result Count = " + counts);//当前结果集的记录数
            System.out.println("Statistic Record Candidate Count = " +  ph.getCandidateCount());//符合条件的记录数,用来算分页数

            for(int i=0; i<counts; i++) {
                StatsRecord record = statsRecodList.elementAt(i);
                Date t = record.getTime();//记录时间
                double rawRecordCount = record.getCount();//原始记录个数
                double flow = record.getSumValue();//流量
                Date maxValueTime = record.getMaxValueTime();//最大值发生时间
                double max = record.getMaxValue();//最大值
                Date minValueTime = record.getMinValueTime();//最小值发生时间
                double min = record.getMinValue();//最小值
                double duration = record.getDuration();//统计时长
                double avg = record.getAvg();//平均值
                Date firstValueTime = record.getFirstValueTime();//第一个值发生时间
                double first = record.getFirstValue();//第一个值
                Date lastValueTime = record.getLastValueTime();//最后一个值发生时间
                double last = record.getLastValue();//最后一个值
                System.out.println(i+1 +", start time:"+ dateFmt.format(t) +",raw record count:"+ rawRecordCount +",duration:"+ duration +
                        ",ave:"+ avg +",flow:"+ flow +",max:"+ max +",min:"+ min +
                        ", first value time:"+ dateFmt.format(firstValueTime) +",first:"+ first +
                        ", last value time:"+ dateFmt.format(lastValueTime) +",last:"+ last);
            }
        }
    }    
}

运行结果:

Point Name: public.sys.PDB_OS_CPU_USAGE
Statistic Record Result Count = 1
Statistic Record Candidate Count = 1
1, start time:2023-02-09 10:00:00,raw record count:3590.0,duration:3600.0,ave:35.38874351772425,flow:127045.58922863007,max:100.0,min:11.764447212219238, first value time:2023-02-09 10:00:00,first:18.916858673095703, last value time:2023-02-09 10:59:59,last:16.3778018951416
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值