您的位置:首页 > 数据库

dynatrace purepath数据转换到数据库

2016-07-29 17:47 381 查看
   dynatrace的purepath捕获的数据,有利于我们整体测量性能,但由于是xml格式的,所以需要做一些转换才能到数据库。

import java.util.Date;

/*
< pathinfo error_state="Transaction failed"
purepath="/remind/soa/cxf/SyncLCAMWorkflowMsg#getWorkflowMsg"
response_time_ms="2.6969668865203857"
breakdown="CPU: 2.668 ms, Sync: -, Wait: -, Suspension: -"
size="1"
agent="OracleWebLogic_Monitoring[AdminServer]@gdsdb:6113"
application="Default Application"
start_time="2016-07-20T09:29:18.156+08:00"
duration_ms="2.697021484375"/>
* */
public class PathInfoVO {
private String errorState;
private String purePath;
private float responseTimeMs;
private float breakDownCPU;
private String breakDownSync;
private String breakDownWait;
private String breakDownSuspension;
private int size;
private String agent;
private String application;
private Date startTime;
private float durationMs;
public String getAgent() {
return agent;
}
public void setAgent(String agent) {
this.agent = agent;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public float getBreakDownCPU() {
return breakDownCPU;
}
public void setBreakDownCPU(float breakDownCPU) {
this.breakDownCPU = breakDownCPU;
}
public String getBreakDownSuspension() {
return breakDownSuspension;
}
public void setBreakDownSuspension(String breakDownSuspension) {
this.breakDownSuspension = breakDownSuspension;
}
public String getBreakDownSync() {
return breakDownSync;
}
public void setBreakDownSync(String breakDownSync) {
this.breakDownSync = breakDownSync;
}
public String getBreakDownWait() {
return breakDownWait;
}
public void setBreakDownWait(String breakDownWait) {
this.breakDownWait = breakDownWait;
}
public float getDurationMs() {
return durationMs;
}
public void setDurationMs(float durationMs) {
this.durationMs = durationMs;
}
public String getErrorState() {
return errorState;
}
public void setErrorState(String errorState) {
this.errorState = errorState;
}
public String getPurePath() {
return purePath;
}
public void setPurePath(String purePath) {
this.purePath = purePath;
}
public float getResponseTimeMs() {
return responseTimeMs;
}
public void setResponseTimeMs(float responseTimeMs) {
this.responseTimeMs = responseTimeMs;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
/*
* toString method
*/
public String toString() {
StringBuffer sb = new StringBuffer(512);
sb.append("PathInfoVO[");
sb.append("\n errorState=").append(this.errorState);
sb.append("\n purePath=").append(this.purePath);
sb.append("\n responseTimeMs=").append(this.responseTimeMs);
sb.append("\n breakDownCPU=").append(this.breakDownCPU);
sb.append("\n breakDownSync=").append(this.breakDownSync);
sb.append("\n breakDownWait=").append(this.breakDownWait);
sb.append("\n breakDownSuspension=").append(this.breakDownSuspension);
sb.append("\n size=").append(this.size);
sb.append("\n agent=").append(this.agent);
sb.append("\n application=").append(this.application);
sb.append("\n startTime=").append(this.startTime);
sb.append("\n durationMs=").append(this.durationMs);
sb.append("\n]");
return sb.toString();
}
}


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/*
<  pathinfo error_state="Transaction failed"
purepath="/remind/soa/cxf/SyncLCAMWorkflowMsg#getWorkflowMsg"
response_time_ms="2.6969668865203857"
breakdown="CPU: 2.668 ms, Sync: -, Wait: -, Suspension: -"
size="1"
agent="OracleWebLogic_Monitoring[AdminServer]@gdsdb:6113"
application="Default Application"
start_time="2016-07-20T09:29:18.156+08:00"
duration_ms="2.697021484375"/>
* */
class XMLHandler extends DefaultHandler {
//使用栈这个数据结构来保存
private Stack<String> stack = new Stack<String>();
//数据
private String errorState;
private String purePath;
private String agent;
List<PathInfoVO> pathInfoList = new ArrayList();
//db定义
static final String driver_class  = "oracle.jdbc.driver.OracleDriver";
static final String connectionURL = "jdbc:oracle:thin:@10.10.11.16:1521:orcl";
static final String userID        = "test";
static final String userPassword  = "test";

@Override
public void startDocument() throws SAXException{
System.out.println("start document -> parse begin");
}

/**
* 文档解析到最后
*/
@Override
public void endDocument() throws SAXException{
if(pathInfoList.size()>0){
insertDB(pathInfoList);
}
System.out.println("end document -> parse finished");
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException{
// 将标签名压入栈
stack.push(qName);
PathInfoVO pathInfoVO = new PathInfoVO();
DecimalFormat decimalFormat=new DecimalFormat(".00");
// 处理属性
String temp[] = new String[4];
String t_CPU;
String t_Sync;
String t_Wait;
String t_Suspension;
for (int i = 0; i < attributes.getLength(); ++i){
String attrName = attributes.getQName(i);
String attrValue = attributes.getValue(i);
if("error_state".equals(attrName)){
pathInfoVO.setErrorState(attrValue);
} else if("purepath".equals(attrName)){
pathInfoVO.setPurePath(attrValue);
}else if("response_time_ms".equals(attrName)){
pathInfoVO.setResponseTimeMs(Float.parseFloat(decimalFormat.format(Float.parseFloat(attrValue))));
}else if("breakdown".equals(attrName)){
temp =attrValue.split(",");
t_CPU = temp[0].replace("CPU: ", "").replace(" ms", "");
t_Sync = temp[1].replace("Sync:", "").replace("-", "");
t_Wait = temp[2].replace("Wait:", "").replace("-", "");
t_Suspension = temp[3].replace(" Suspension:", "").replace("-", "");
pathInfoVO.setBreakDownCPU(Float.valueOf(t_CPU).floatValue());
pathInfoVO.setBreakDownSync(t_Sync);
pathInfoVO.setBreakDownWait(t_Wait);
pathInfoVO.setBreakDownSuspension(t_Suspension);
}else if("size".equals(attrName)){
pathInfoVO.setSize(Integer.parseInt(attrValue));
}else if("agent".equals(attrName)){
pathInfoVO.setAgent(attrValue);
}else if("application".equals(attrName)){
pathInfoVO.setApplication(attrValue);
}else if("start_time".equals(attrName)){
pathInfoVO.setStartTime(parseStringToDate(attrValue));
}else if("duration_ms".equals(attrName)){
pathInfoVO.setDurationMs(Float.parseFloat(decimalFormat.format(Float.parseFloat(attrValue))));
}
//System.out.println("属性: " + attrName + "=" + attrValue)
}
//System.out.println(pathInfoVO);
pathInfoList.add(pathInfoVO);
}
private Date parseStringToDate(String time){
DateFormat format ;
Date newDate = new Date();
try {
format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
newDate = format.parse(time.replace("T", " ").replace("+08:00", ""));
}catch(Exception e){
e.printStackTrace();
}
return newDate;
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException{
// 取出标签名
String tag = stack.peek();
if ("error_state".equals(tag)){
errorState = new String(ch, start, length);
}
else if ("purepath".equals(tag)){
purePath = new String(ch, start, length);
}
else if ("agent".equals(tag)){
agent = new String(ch, start, length);
}
}
/**
* 每一个实体
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException{
stack.pop();// 表示该元素解析完毕,需要从栈中弹出标签
}

private String dateToString(Date dateTime){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str= "1999-01-01 00:00:00";
if(dateTime != null){
str=sdf.format(dateTime);
}
return str;
}

private void insertDB(List pathInfoVOList) {
Connection  con = null;
Statement   stmt = null;
String  s_sql = "insert into t_purepath values(?,?,?,?,?,?,?,?,?,to_date(?, 'yyyy-mm-dd hh24:mi:ss'),?)";
try {
Class.forName (driver_class).newInstance();
con = DriverManager.getConnection(connectionURL, userID, userPassword);
PreparedStatement pstmt = con.prepareStatement(s_sql);
con.setAutoCommit(false);
PathInfoVO pathInfoVO ;
for(int i=0; i<pathInfoVOList.size(); i++){
pathInfoVO = (PathInfoVO)pathInfoList.get(i);
//error_state varchar2
pstmt.setString(1, pathInfoVO.getErrorState());
//purepath varchar2
pstmt.setString(2, pathInfoVO.getPurePath());
//response_time_ms number
pstmt.setFloat(3, pathInfoVO.getResponseTimeMs());
//breakdown_cpu number
pstmt.setFloat(4, pathInfoVO.getBreakDownCPU());
//breakdown_Sync varchar2
pstmt.setString(5, pathInfoVO.getBreakDownSync());
//breakdown_Wait varchar2
pstmt.setString(6, pathInfoVO.getBreakDownWait());
//breakdown_Suspension varchar2
pstmt.setString(7, pathInfoVO.getBreakDownSuspension());
//agent  varchar2
pstmt.setString(8, pathInfoVO.getAgent());
//application varchar2
pstmt.setString(9, pathInfoVO.getApplication());
//start_time date
pstmt.setString(10, dateToString(pathInfoVO.getStartTime()));
//duration_ms  number
pstmt.setFloat(11, pathInfoVO.getDurationMs());
pstmt.addBatch();
if(i % 10000 == 0){
pstmt.executeBatch();
con.commit();
}
}
//不足一万条的
pstmt.executeBatch();
con.commit();
}  catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
stmt = null;//--> 让他迅速成为java gc的对象
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
con = null;//--> 让他迅速成为java gc的对象
}
}
}
}
}

import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class ParsePurePathXML {
public static void main(String[] args) throws Exception{
//获得SAX解析器工厂实例
SAXParserFactory factory = SAXParserFactory.newInstance();
//获得SAX解析器实例
SAXParser parser = factory.newSAXParser();
//开始进行解析,传入待解析的文档的处理器
parser.parse(new File("d:/purepathinfo.xml"), new XMLHandler());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: