package com.awspaas.user.apps.ranlotnum.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Iterator; import java.util.List; import java.util.Properties; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.pentaho.di.core.KettleEnvironment; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.job.Job; import org.pentaho.di.job.JobMeta; import org.pentaho.di.trans.Trans; import org.pentaho.di.trans.TransMeta; public class Kettle { public static Properties properties=new Properties(); static{ try { properties.load(Kettle.class.getResourceAsStream("/config.properties")); } catch (IOException e) { e.printStackTrace(); } } public void runTran(String filename) { try { KettleEnvironment.init(); TransMeta transMeta = new TransMeta(filename); Trans trans = new Trans(transMeta); KettleUtil.runJob(filename); //姝ゅ涓鸿緭鍏ョ殑鍙傛暟锛屼篃鍙互閫氳繃鍙傛暟浼犺繘鏂规硶涓� //String [] s = {"鎴戞槸鍙傛暟1","鎴戞槸鍙傛暟2"}; //trans.prepareExecution(s); trans.startThreads(); trans.waitUntilFinished(); if (trans.getErrors() != 0) { System.out.println("Error"); } } catch (KettleException e) { e.printStackTrace(); } } public void runJob(String jobname){ try { KettleEnvironment.init(); //jobname 鏄疛ob鑴氭湰鐨勮矾寰勫強鍚嶇О JobMeta jobMeta = new JobMeta(jobname, null); Job job = new Job(null, jobMeta); //鍚慗ob 鑴氭湰浼犻�鍙傛暟锛岃剼鏈腑鑾峰彇鍙傛暟鍊硷細${鍙傛暟鍚峿 //job.setVariable(paraname, paravalue); job.start(); job.waitUntilFinished(); if (job.getErrors() > 0) { System.out.println("decompress fail!"); } } catch (KettleException e) { System.out.println(e); } } public void runTranJob(String flag){ try { dealKjbAndKtr(); KettleEnvironment.init(); } catch (Exception e) { e.printStackTrace(); } new TreadExtract(flag); } @SuppressWarnings("unchecked") private void dealKjbAndKtr() { String oracleUrl=properties.getProperty("oracle.jdbc.url"); String[] oracleArr=oracleUrl.split(":|/|@"); String oracleDriver=properties.getProperty("oracle.jdbc.driverClass"); String oracleType=""; if(oracleDriver.indexOf("Oracle")!=-1){ oracleType="ORACLE"; }else{ oracleType="MYSQL"; } String oracleServer=oracleArr[oracleArr.length-3]; String oraclePort=oracleArr[oracleArr.length-2]; String oracleDatabase=oracleArr[oracleArr.length-1]; String oracleUser=properties.getProperty("oracle.jdbc.user"); String oraclePwd = properties.getProperty("oracle.jdbc.password"); String mysqlUrl=properties.getProperty("mysql.jdbc.url"); String[] mysqlArr=mysqlUrl.split(":|/|@"); String mysqlDriver=properties.getProperty("mysql.jdbc.driverClass"); String mysqlType=""; if(mysqlDriver.indexOf("Oracle")!=-1){ mysqlType="ORACLE"; }else{ mysqlType="MYSQL"; } String mysqlServer=mysqlArr[mysqlArr.length-3]; String mysqlPort=mysqlArr[mysqlArr.length-2]; String mysqlDatabase=mysqlArr[mysqlArr.length-1]; String mysqlUser=properties.getProperty("mysql.jdbc.user"); String mysqlPwd = properties.getProperty("mysql.jdbc.password"); try{ String path=Kettle.class.getResource("/").getPath(); File f=new File(path+"\\VoteSmallBusinesses.kjb"); if(!f.exists()){ f=new File(path); path=f.getParent(); } String path1=path+"\\VoteSmallBusinesses.kjb"; String path2=path+"\\VoteSmallBusinessesNB.kjb"; String path3=path+"\\SELECTTIME.ktr"; String path4=path+"\\DELETE_PUB_ENTYEAREXAMINFO.ktr"; String path5=path+"\\DELETE_PUB_MARPRIPINFO.ktr"; String path6=path+"\\SAVE_PUB_ENTYEAREXAMINFO.ktr"; String path7=path+"\\SAVE_PUB_MARPRIPINFO.ktr"; String path8=path+"\\SS_SupportivePolicy_TopIcis_zengliang.kjb"; String path9=path+"\\set_SS_SupportivePolicy_Time.ktr"; String path10=path+"\\SS_SupportivePolicy_ToTopicis.ktr"; for(int i=0;i<10;i++){ SAXReader reader = new SAXReader(); File file=null; Document doc=null; List projects=null; if(i==0){ file=new File(path1); doc = (Document) reader.read(file); projects=doc.selectNodes("job/connection"); }else if(i==1){ file=new File(path2); doc = (Document) reader.read(file); projects=doc.selectNodes("job/connection"); }else if(i==2){ file=new File(path3); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); }else if(i==3){ file=new File(path4); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); }else if(i==4){ file=new File(path5); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); }else if(i==5){ file=new File(path6); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); }else if(i==6){ file=new File(path7); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); }else if(i==7){ file=new File(path8); doc = (Document) reader.read(file); projects=doc.selectNodes("job/connection"); }else if(i==8){ file=new File(path9); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); }else if(i==9){ file=new File(path10); doc = (Document) reader.read(file); projects=doc.selectNodes("transformation/connection"); } Iterator it=projects.iterator(); while(it.hasNext()){ Element elm=(Element)it.next(); Element e=(Element) elm.selectNodes("name").get(0); if(e.getText().equals("topicis")){ ((Element) elm.selectNodes("server").get(0)).setText(oracleServer); ((Element) elm.selectNodes("type").get(0)).setText(oracleType); ((Element) elm.selectNodes("access").get(0)).setText("Native"); ((Element) elm.selectNodes("database").get(0)).setText(oracleDatabase); ((Element) elm.selectNodes("port").get(0)).setText(oraclePort); ((Element) elm.selectNodes("username").get(0)).setText(oracleUser); ((Element) elm.selectNodes("password").get(0)).setText(oraclePwd); ((Element)((Element) elm.selectNodes("attributes/attribute").get(3)).selectNodes("attribute").get(0)).setText(oraclePort); }else if(e.getText().equals("mouse")){ ((Element) elm.selectNodes("server").get(0)).setText(mysqlServer); ((Element) elm.selectNodes("type").get(0)).setText(mysqlType); ((Element) elm.selectNodes("access").get(0)).setText("Native"); ((Element) elm.selectNodes("database").get(0)).setText(mysqlDatabase); ((Element) elm.selectNodes("port").get(0)).setText(mysqlPort); ((Element) elm.selectNodes("username").get(0)).setText(mysqlUser); ((Element) elm.selectNodes("password").get(0)).setText(mysqlPwd); List<Element> eleList = elm.selectNodes("attributes/attribute"); for(Element element:eleList){ if(((Element)element.selectNodes("code").get(0)).getText().equalsIgnoreCase("PORT_NUMBER")){ ((Element)element.selectNodes("attribute").get(0)).setText(mysqlPort); break; } } } } OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); OutputFormat of = new OutputFormat(); of.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(osw, of); writer.write(doc); writer.close(); } }catch(Exception ex){ ex.printStackTrace(); } } }
package com.awspaas.user.apps.ranlotnum.util; import org.pentaho.di.core.exception.KettleXMLException; import org.pentaho.di.job.Job; import org.pentaho.di.job.JobMeta; public class TreadExtract implements Runnable { public static final String TYEP_ZT="01"; public static final String TYEP_NB="02"; public static final String TYEP_SS="03"; public static final String TYEP_ALL_QY="04"; public static final String TYEP_ALL_SS="05"; private String flag; public TreadExtract(String flag) { super(); this.flag=flag; } @Override public void run() { String jobName = ""; if(TreadExtract.TYEP_ZT.equals(flag)){ jobName = "VoteSmallBusinesses.kjb"; }else if(TreadExtract.TYEP_NB.equals(flag)){ jobName = "VoteSmallBusinessesNB.kjb"; }else if(TreadExtract.TYEP_SS.equals(flag)){ jobName = "SS_SupportivePolicy_TopIcis_zengliang.kjb"; }else if(TreadExtract.TYEP_ALL_QY.equals(flag)){ jobName = "importAll.kjb"; }else if(TreadExtract.TYEP_ALL_SS.equals(flag)){ jobName = "SS_SupportivePolicy_TopIcis_All.kjb"; } // jobName 是Job脚本的路径及名称 JobMeta jobMeta = null; try { jobMeta = new JobMeta(jobName, null); } catch (KettleXMLException e) { e.printStackTrace(); } Job job = new Job(null, jobMeta); // 向Job 脚本传递参数,脚本中获取参数值:${参数名} job.start(); job.waitUntilFinished(); } }
java调用kettle文件实例
最新推荐文章于 2024-04-23 21:53:09 发布