1. 程式人生 > >kettle-java程式碼執行轉換並獲取步驟度量

kettle-java程式碼執行轉換並獲取步驟度量

1.windows部署成功kettle並能成功執行。如果沒有成功詳見kettle windows部署方法

2.在eclipse中新建一個java專案或者maven專案,jdk配置為1.8.

3.將下圖中的jar包從kettle安裝路徑下lib目錄copy到專案中,並build path

4.執行執行

4.1.執行ktr檔案

注意:本次要執行ktr檔案涉及到postgresql一次同時也要講postgresql-jdbc.*.jar新增進專案,獲取到的資訊通過json傳遞,json-lib也需新增進專案。

	public static void runktr() {
		try {
			KettleEnvironment.init();
			TransMeta transMeta = new TransMeta("C:\\Users\\lixiang\\Desktop\\pgtorizhi.ktr");
			Trans trans = new Trans(transMeta);
			trans.prepareExecution(null);
			trans.startThreads();
			trans.waitUntilFinished();
			if (trans.isFinished()) {
				JSONArray jsonArray = new JSONArray();
				for (int i = 0; i < trans.nrSteps(); i++) {
					StepInterface baseStep = trans.getRunThread(i);
					StepStatus stepStatus = new StepStatus(baseStep);
					
					//fields即為步驟度量資訊
					String[] fields = stepStatus.getTransLogFields();
					JSONArray childArray = new JSONArray();
					for (int f = 1; f < fields.length; f++) {
						childArray.add(fields[f]);
						System.out.println(fields[f]);
					}
					jsonArray.add(childArray);
				}
			} else {
				System.out.println("執行失敗");
			}
		} catch (KettleXMLException e) {
			e.printStackTrace();
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}

4.2.執行資料庫資源庫的ktr檔案

	public static void rundatabasektr() {
		try {
			KettleEnvironment.init();
			KettleDatabaseRepository repository = new KettleDatabaseRepository();
			
			//資源庫資料庫地址,我這裡採用poatgresql資料庫
			DatabaseMeta databaseMeta = new DatabaseMeta("pg_202", "PostgreSQL", "Native", "192.168.50.202", "postgres",
					"5432", "postgres", "postgres");
			
			//kettle資源庫
			KettleDatabaseRepositoryMeta kettleDatabaseMeta = new KettleDatabaseRepositoryMeta("pg", "ERP",
					"Transformation description", databaseMeta);

			repository.init(kettleDatabaseMeta);
			repository.connect("admin", "admin");//資源庫使用者名稱密碼
			RepositoryDirectoryInterface directory = repository.loadRepositoryDirectoryTree();

			String transname = "zh_1";// 轉換名稱
			String[] params = null;// 引數

			// 根據變數查詢到模型所在的目錄物件,此步驟很重要。
			RepositoryDirectoryInterface directory1 = repository.findDirectory("/");

			// 建立ktr元物件
			TransMeta transformationMeta = ((Repository) repository).loadTransformation(transname, directory1, null,
					true, null);
			// 建立ktr
			Trans trans = new Trans(transformationMeta);
			// 執行ktr
			trans.execute(params);
			// 等待執行完畢
			trans.waitUntilFinished();
			if (trans.getErrors() > 0) {
				System.out.println("no");
			} else {
				System.out.println("yes");
			}

		} catch (KettleXMLException e) {
			e.printStackTrace();
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}

4.3執行file repository ktr

public static void runfilerepositoryktr() {
		try {
			KettleEnvironment.init();
			KettleFileRepository repository = new KettleFileRepository();
			String baseDirectory = "F:\\huaxun\\software\\pdi-ce-7.1.0.0-12\\data-integration\\workspace";
			KettleFileRepositoryMeta kettlefileMeta = new KettleFileRepositoryMeta("KettleFileRepository", "wokspace",
					"file repostory", baseDirectory);
			repository.init(kettlefileMeta);
			repository.connect("admin", "admin");
			String transname = "zh_1";
			String[] params = null;// 引數
			RepositoryDirectoryInterface directory = repository.loadRepositoryDirectoryTree();

			// 建立ktr元物件
			TransMeta transformationMeta = ((Repository) repository).loadTransformation(transname, directory, null,
					true, null);
			// 建立ktr
			Trans trans = new Trans(transformationMeta);
			// 執行ktr
			trans.execute(params);
			// 等待執行完畢
			trans.waitUntilFinished();
			if (trans.getErrors() > 0) {
				System.out.println("no");
			} else {
				System.out.println("yes");
			}

		} catch (KettleXMLException e) {
			e.printStackTrace();
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}

4.4執行kjb檔案

public static void runkjb(){
		try {
			KettleEnvironment.init();
			JobMeta jobmeta = new JobMeta("C:\\Users\\lixiang\\Desktop\\jb_1.kjb", null);
			Job job = new Job(null,jobmeta);
			
            job.start();  
            job.waitUntilFinished();  
            if (job.getErrors() > 0) {  
            	System.out.println("執行job發生異常");  
            }  
		} catch (KettleXMLException e) {
			e.printStackTrace();
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}