ProcessDB实时/时序数据库——JAVA_API操作字符串对象

一、插入字符串数据

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

import com.processdb.connection.DBConnect;
import com.processdb.connection.DBConnectManager;
import com.processdb.driver.common.ConnectInfo;
import com.processdb.driver.common.ConstantValue.MessageType;
import com.processdb.driver.dao.DBRealtimeDataDao;
import com.processdb.exception.DBException;


public class Test {

	static int counter = 0;
    /**
     * @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");
    	host.setHostPort(8301);
    	host.setPassword("shuquan230519");
    	host.setUserName("root");
    	DBConnect myConn = null;
		try {
			myConn = DBConnectManager.getInstance().getConnect(host);
		} catch (DBException e1) {
			e1.printStackTrace();
			return;
		}
	    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		int res;
        try {
            int count = 0;
            while(count<10)
            {
                count++;
                Date t = new Date();
                res = DBRealtimeDataDao.insertRealtimeMessage(myConn, "d1.t1.p6", MessageType.INFO, count+"Time stamp string "+dateFmt.format(t));
                if(res < 0)
                {
                    System.err.println("Error code: " + res);
                }else {
					System.out.println("insert success! PointName:d1.t1.p6,value:"+count+"Time stamp string "+dateFmt.format(t));
				}
  
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        myConn.close();
    }
}

运行结果如下:

insert success! PointName:d1.t1.p6,value:1Time stamp string 2023-10-11 11:37:44.936
insert success! PointName:d1.t1.p6,value:2Time stamp string 2023-10-11 11:37:45.938
insert success! PointName:d1.t1.p6,value:3Time stamp string 2023-10-11 11:37:46.947
insert success! PointName:d1.t1.p6,value:4Time stamp string 2023-10-11 11:37:47.948
insert success! PointName:d1.t1.p6,value:5Time stamp string 2023-10-11 11:37:48.949
insert success! PointName:d1.t1.p6,value:6Time stamp string 2023-10-11 11:37:49.958
insert success! PointName:d1.t1.p6,value:7Time stamp string 2023-10-11 11:37:50.959
insert success! PointName:d1.t1.p6,value:8Time stamp string 2023-10-11 11:37:51.960
insert success! PointName:d1.t1.p6,value:9Time stamp string 2023-10-11 11:37:52.964
insert success! PointName:d1.t1.p6,value:10Time stamp string 2023-10-11 11:37:53.965

二、查询字符串当前数据

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

import com.processdb.connection.DBConnect;
import com.processdb.connection.DBConnectManager;
import com.processdb.connection.DBPoint;
import com.processdb.driver.common.ConnectInfo;
import com.processdb.driver.dao.DBRealtimeDataDao;
import com.processdb.exception.DBException;


public class Test {

	static int counter = 0;
    /**
     * @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");
    	host.setHostPort(8301);
    	host.setPassword("shuquan230519");
    	host.setUserName("root");
    	DBConnect myConn = null;
		try {
			myConn = DBConnectManager.getInstance().getConnect(host);
		} catch (DBException e1) {
			e1.printStackTrace();
			return;
		}
	    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		int res;
        try {
            int count = 0;
            while(count<10)
            {
            	count++;
                Vector<DBPoint> pointList = new Vector<DBPoint>();
                DBPoint point = myConn.getPointByGlobalName("d1.t1.p6");
                if(point != null)
                    pointList.add(point);
                res = DBRealtimeDataDao.queryPointRealtimeData(myConn, pointList);
                if(res < 0)
                {
                    System.err.println("Error code: " + res);
                }
                else
                {
                    for(int i=0;i<pointList.size();i++)
                    {
                        DBPoint dbpoint = pointList.get(i);
                        if(dbpoint!=null)
                            System.out.println("Point Name: "+dbpoint.getGlobalName() + ", time: "+ dateFmt.format(dbpoint.getTime()) + ", status: "+ dbpoint.getQualityStr() + ", message: "+ dbpoint.getString());
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (DBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        myConn.close();
    }
}

运行结果如下:

Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965
Point Name: D1.T1.P6, time: 2023-10-11 11:37:53.000, status: GOOD, message: 10Time stamp string 2023-10-11 11:37:53.965

 

三、查询字符串历史数据

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.MessageRecord;
import com.processdb.driver.record.PointHistory;
import com.processdb.driver.record.StatsRecord;
import com.processdb.exception.DBException;

public class Test {

	SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	SimpleDateFormat dateFmt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	public Test() {
	}
    public int getHistoryData(DBConnect myConn,String pointNameArray[])
    {
		Date begindate = null;//统计开始时间
		Date enddate = null;//统计结束时间
		try {
			begindate = dateFmt.parse("2023-10-11 10:12:33");
			enddate = dateFmt.parse("2023-10-11 12:12:33");
		}catch (Exception pe) {
			pe.printStackTrace();
			return -1;
		}
        byte query_type = ConstantValue.HIS_MSG;//检索历史类型:字符串数据

		Vector<PointHistory> pointList = new Vector<PointHistory>();
		for(int i=0; i<pointNameArray.length; i++) {
			//构造检索对象
			PointHistory ph = new PointHistory(pointNameArray[i], begindate, enddate, 0, query_type);
			pointList.add(ph);
		}

		//执行检索操作
		int res = DBHistoryDataDao.requestPointHistoryByName(myConn,pointList);
		if(res != 0)
		{
			System.out.println("requestPointHistoryByName: failed, error code: " + res);
			return -1;
		}

		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<MessageRecord> messageRecodList = ph.getMessageRecords();
            int counts = messageRecodList.size();
            System.out.println("Point Name: " + ph.getPointName());
            System.out.println("Message Record Result Count = " + counts);//当前结果集的记录数
            System.out.println("Message Record Candidate Count = " +  ph.getCandidateCount());//符合条件的记录数

            for(int i=0; i<counts; i++) {
                MessageRecord record = messageRecodList.elementAt(i);
                Date t = record.getTime();
                short quality = record.getQuality();
                String message = record.getMessage();
                System.out.println((i+1) + "\t" + dateFmt.format(t) + "\t" + quality + "\t" + message);
            }
        
		}
		return 1;
	}
    /**
     * @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");
    	host.setHostPort(8301);
    	host.setPassword("shuquan230519");
    	host.setUserName("root");
    	DBConnect myConn = null;
		try {
			myConn = DBConnectManager.getInstance().getConnect(host);
		} catch (DBException e1) {
			e1.printStackTrace();
		}
		Test tester = new Test();

		String pointNameArray[] = {"D1.T1.P6"};
		while(true)
		{
			try{
				int i=0;
				while(i<1)//00000000)
				{
					tester.getHistoryData(myConn,pointNameArray);
			        //System.exit(0);
					
					Thread.sleep(1000);
					i++;
				}
		        System.exit(0);
			}catch (InterruptedException e) {}
		}
    }    
}

 运行结果如下:

Point Name: D1.T1.P6
Message Record Result Count = 15
Message Record Candidate Count = 0
1	2023-10-11 10:12:33	0	87Time stamp string Time stamp string Time stamp string2023-10-11 10:12:33.591
2	2023-10-11 10:12:34	0	88Time stamp string Time stamp string Time stamp string2023-10-11 10:12:34.592
3	2023-10-11 10:12:35	0	89Time stamp string Time stamp string Time stamp string2023-10-11 10:12:35.598
4	2023-10-11 10:12:36	0	90Time stamp string Time stamp string Time stamp string2023-10-11 10:12:36.599
5	2023-10-11 10:12:37	0	91Time stamp string Time stamp string Time stamp string2023-10-11 10:12:37.615
6	2023-10-11 10:12:38	0	92Time stamp string Time stamp string Time stamp string2023-10-11 10:12:38.617
7	2023-10-11 10:12:39	0	93Time stamp string Time stamp string Time stamp string2023-10-11 10:12:39.619
8	2023-10-11 10:12:40	0	94Time stamp string Time stamp string Time stamp string2023-10-11 10:12:40.636
9	2023-10-11 10:12:41	0	95Time stamp string Time stamp string Time stamp string2023-10-11 10:12:41.638
10	2023-10-11 10:12:42	0	96Time stamp string Time stamp string Time stamp string2023-10-11 10:12:42.640
11	2023-10-11 10:12:43	0	97Time stamp string Time stamp string Time stamp string2023-10-11 10:12:43.641
12	2023-10-11 10:12:44	0	98Time stamp string Time stamp string Time stamp string2023-10-11 10:12:44.653
13	2023-10-11 10:12:45	0	99Time stamp string Time stamp string Time stamp string2023-10-11 10:12:45.654
14	2023-10-11 10:12:46	0	100Time stamp string Time stamp string Time stamp string2023-10-11 10:12:46.669
15	2023-10-11 11:36:36	0	1Time stamp string Time stamp string Time stamp string2023-10-11 11:36:36.963

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值