OOZIE依赖解析并用mxGraph画出依赖图

本文介绍了两种方法解析OOZIE应用的依赖关系:一是通过Hive UDTF解析XML文件,将依赖存储在Hive表中,然后用mxGraph绘制依赖图;二是直接解析本地SVN代码,在用户查询后解析并展示依赖。
摘要由CSDN通过智能技术生成

 

方式一:

在hive中创建表,通过UDTF解析hdfs上的coordinate.xml获取应用间的依赖关系,并以父子维的方式存储在hive表中,以后可以在java中直接访问hive表,并用mxgraph画出依赖图

create_hadoop_app_info.hql

use pad_hdp;
drop table hadoop_app_info;
create table hadoop_app_info(
appname           string comment '应用名',
frequency         string comment '调度频率',
startTime         string comment '调度时间',
dataInStr         string comment '依赖的应用们'
)comment 'HADOOP应用解析结果表' row format delimited fields terminated by '\001' stored as rcfile;

 

CoordinatorParse

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class CoordinatorParse {

	/**
	 * @param args
	 */
	/*
	 * public static void main(String[] args) throws Exception{ // TODO
	 * Auto-generated method stub List<OozieApp> appRelation =new
	 * ArrayList<OozieApp>(); String
	 * svnPath="D:/CCShare/PAD-HADOOP1.87.0/src/main/resources/config/hduser0401"
	 * ;
	 * 
	 * List<String> svnPathList=getCoordinatorList(svnPath); for (int
	 * i=0;i<svnPathList.size();i++){
	 * singleFileParseForGraph(svnPathList.get(i).toString(),appRelation); }
	 * for(OozieApp op:appRelation){
	 * //System.out.println(op.getDataIn()+","+op.getDataOut()); } List<String>
	 * jsList=new ArrayList<String>(); jsList.add(
	 * "var root = graph.insertVertex(parent, null, 'root', 200, 200, w, h);");
	 * //System.out.println(
	 * "var root = graph.insertVertex(parent, null, 'root', 200, 200, w, h);");
	 * for(OozieApp op:appRelation){ String
	 * str1="var "+op.getDataIn().replace("-",
	 * "_")+" = graph.insertVertex(parent, null, '"
	 * +op.getDataIn()+"', 200, 200, w, h);"; String
	 * str2="var "+op.getDataOut().replace("-",
	 * "_")+" = graph.insertVertex(parent, null, '"
	 * +op.getDataOut()+"', 200, 200, w, h);"; String
	 * str3="var "+op.getDataIn().replace("-", "_")+op.getDataOut().replace("-",
	 * "_")+" = graph.insertEdge(parent, null, '',"+op.getDataIn().replace("-",
	 * "_")+" ,"+op.getDataOut().replace("-", "_")+");";
	 * 
	 * if(!jsList.contains(str1)){ jsList.add(str1); //
	 * System.out.println(str1); } if(!jsList.contains(str2)){ jsList.add(str2);
	 * //System.out.println(str2); } jsList.add(str3);
	 * //System.out.println(str3); } }
	 */
	public static void main(String[] args) throws Exception{ 
		Map<String, OozieApp> oozieAppMap = new HashMap<String, OozieApp>();
		List<String> svnPathList =new CoordinatorParse().getCoordinatorList("hdfs://dev-l002781.app.paic.com.cn:9000/apps/hduser0401");

	
	
		for (int i = 0; i < svnPathList.size(); i++) {
			try {
				new CoordinatorParse().singleFileParseForGraph(svnPathList.get(i).toString(),
						oozieAppMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//return oozieAppMap;
	  }

	public  Map<String, OozieApp> getOozieAppMap(String svnPath) {
		Map<String, OozieApp> oozieAppMap = new HashMap<String, OozieApp>();
		List<String> svnPathList = this.getCoordinatorList(svnPath);

		for (int i = 0; i < svnPathList.size(); i++) {
			try {
				this.singleFileParseForGraph(svnPathList.get(i).toString(),
						oozieAppMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return oozieAppMap;
	}
	
	  
	public List<String> getCoordinatorList(String str) {
		List<String> list = new ArrayList<String>();
		System.out.println(str);
		BufferedReader br = null;
		FileSystem fs;
		try {
			fs = FileSystem.get(new Configuration());
			FileStatus[] status = fs.listStatus(new Path(str));
			for (FileStatus file : status) {

				String strTmp = file.getPath().getName();
				//System.out.println(strTmp);
				if (strTmp.indexOf("pad-dp-") >= 0
						&& strTmp.indexOf("svn") < 0
						&& strTmp.indexOf("-mis-") < 0
						&& strTmp.indexOf("pad-dp-check") < 0
						&& strTmp.indexOf("pad-dp-clean") < 0
						&& strTmp.indexOf("pad-dp-bak") < 0) {
					//System.out.println(file.getPath());
					FileSystem subfs = FileSystem.get(new Configuration());
					FileStatus[] substatus = subfs.listStatus(file.getPath());
					if (substatus!=null){
						//System.out.println(subfs.listStatus(new Path(strTmp)));
						for (FileStatus subfile : substatus) {
							//SSystem.out.println(subfile.getPath());
							if (subfile.getPath().getName().equals("coordinator.xml")) {
								//System.out.println(subfile.getPath());
								list.add(subfile.getPath().toString());
							}
						}
					}
					
					//System.out.println(file.getPath().getName());
				}
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public  Map<String,OozieApp> singleFileParseForGraph(String str,
			Map<String,OozieApp> appRelationMap) throws Exception {
		
		BufferedReader br = null;
		FileSystem fs;
		
			fs = FileSystem.get(new Configuration());
			FileStatus file =fs.getFileStatus(new Path(str));
			System.out.println(file.getPath());
			FSDataInputStream inputStream = fs.open(file.getPath());
			br =	  new BufferedReader(new InputStreamReader(inputStream));
			
			String line = null;
			 String strs="";
			while (null != (line = br.readLine())) {
				strs= strs+line;
				//System.out.println(line);
			}
		//InputStream is = new ByteArrayInputStream(text.getBytes());

		//System.out.println("singleFileParseForGraph=============================");
		// ???DOM??????????????
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		// ??DOM?????л??DOM??????

		DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
		// ?????File????????????
		Document doc = null;
		
		// 字符串转XML
		
		StringReader sr = new StringReader(strs);
		InputSource is = new InputSource(sr);
		//DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//DocumentBuilder builder=factory.newDocumentBuilder();
		//Document doc = builder.parse(is);



		doc = dbBuilder.parse(is);
		// ??????????Student?????????б?
		NodeList listIn = doc.getElementsByTagName("data-in");
		NodeList listOut = doc.getElementsByTagName("data-out");
		String frequency = ((Element) doc.getElementsByTagName("coordinator-app").item(0)).getAttribute("frequency");
		
		
		System.out.println("frequency:"+frequency);
		String datasetOut = "";
		String datasetIn = "";
//		OozieApp oozieApp = new OozieApp();
		System.out.println("singleFileParseForGraph");
		
		OozieApp oozieApp = new OozieApp();
		List<String> dataInList =new ArrayList<String>();
		
		for (int i = 0; i < listOut.getLength(); i++) {

			String str_tmp_1 = ((Element) listOut.item(i)).getAttribute("dataset");
			str_tmp_1=str_tmp_1.replace("-dataset", "");
			
			if (str_tmp_1.contains("pad-dp-")) {
				datasetOut=str_tmp_1;
				for (int j = 0; j < listIn.getLength(); j++) {
					
					datasetIn = ((Element) listIn.item(j))
							.getAttribute("dataset");
					datasetIn=datasetIn.replace("-dataset", "");
                    dataInList.add(datasetIn);
                    
					//appRelation.add(oozieApp);
				}
			}
		}
		System.out.println("str:"+str);
		String startTime = getAppStartTime(str);
		System.out.println("getAppStartTime_startTime:"+startTime);
	
		oozieApp.setDataInList(dataInList);
		//System.out.println("dataInList:"+dataInList.size());
		//System.out.println("datasetOut:"+datasetOut);
		//System.out.println("frequency:"+frequency);
		//System.out.println("startTime:"+startTime);
		
		oozieApp.setDataOut(datasetOut);
		oozieApp.setStartTime(startTime);
		
		oozieApp.setFrequency(frequency);
		
		//System.out.println("frequency:"+oozieApp.getFrequency());
		//System.out.println("startTime:"+oozieApp.getStartTime());
		appRelationMap.put(datasetOut, oozieApp);
		
		System.out.println("appRelationMap:"+appRelationMap.size());
		
		
		//System.out.println("startTime:"+startTime);


		return appRelationMap;

	}
	
	
	
	
	
	public  String getAppStartTime(String str) throws Exception {
		Properties props = new Properties();
		//System.out.println("getAppStartTime");
		String[] strArray = str.split("/");
		//System.out.println("strArray:"+strArray[strArray.length - 2]);
		//System.out.println(str);
		//str=str.replace("ordinator", "fdsafsd");
		System.out.println(str);
		str=str.replaceAll("coordinator.xml", strArray[strArray.length - 2]
				+ ".properties");
		System.out.println(str);
		
		BufferedReader br = null;
		FileSystem fs;
		
			fs = FileSystem.get(new Configuration());
			FileStatus file =fs.getFileStatus(new Path(str));
			System.out.println(file.getPath());
			FSDataInputStream inputStream = fs.open(file.getPath());
			br =	  new BufferedReader(new InputStreamReader(inputStream));
			
			String line = null;
			 String strs="";
			while (null != (line = br.readLine())) {
				strs= strs+"\r\n"+line;
				//System.out.println(line);
			}
			// 
			
		
		//System.out.println(str);
		try {
			InputStream is = new ByteArrayInputStream(strs.getBytes());

            
			//InputStream in = new BufferedInputStream(new FileInputStream(str));
			InputStream in=is;
			System.out.println("================================================");
			props.load(is);
			String value = props.getProperty("job_start");
			System.out.println("job_start:" + value);
			int byteRead;
           /* while ((byteRead = is.read()) != -1) {
                System.out.print((char)byteRead);
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值