示例代码:
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