java(maven管理jar)调用kettle

楔子

java调用kettle,jar包使用maven来管理。
通常需要的jar可以在kettle 的lib目录下找到,但是一个个找太麻烦,一些中央仓库还没有kettlejar

项目pom.xml配置kettle仓库

<repositories><!-- kettle中央仓库 -->
	<repository>
		<id>pentaho-public</id>
		<name>Pentaho Public</name>
		<url>http://nexus.pentaho.org/content/groups/omni</url>
		<releases>
			<enabled>true</enabled>
			<updatePolicy>always</updatePolicy>
		</releases>
		<snapshots>
			<enabled>true</enabled>
			<updatePolicy>always</updatePolicy>
		</snapshots>
	</repository>
</repositories>

此仓库包含kettle jar
在这里插入图片描述

java操作

public class DemoJob {
	public static void main(String[] args) throws KettleException {

		KettleEnvironment.init();
		// 创建DB资源库
		KettleDatabaseRepository repository = new KettleDatabaseRepository();
		DatabaseMeta databaseMeta = setDatabaseMeta();
		// 选择资源库 此处参我与本地不一致还是正确执行
		KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta = new KettleDatabaseRepositoryMeta("Kettle", "Kettle", "Transformation description", databaseMeta);
		repository.init(kettleDatabaseRepositoryMeta);
		// 连接资源库
		repository.connect("admin", "admin");
		RepositoryDirectoryInterface directoryInterface = repository.loadRepositoryDirectoryTree();
		System.out.println(directoryInterface);// 根节点是 “/”
		List<RepositoryDirectoryInterface> children = directoryInterface.getChildren();
		for (RepositoryDirectoryInterface re : children) {
			System.out.println(re);
		}
		execJob(repository);

	}

/**
 * 执行 作业
* 
 * @param repository
 * @throws KettleException
 */
	private static void execJob(KettleDatabaseRepository repository) throws KettleException {
		RepositoryDirectoryInterface dir = repository.findDirectory("/201811_JOB");
		JobMeta jobMeta = repository.loadJob(repository.getJobId("A_邮件job", dir), null);
		Job job = new Job(repository, jobMeta);
		// 设置参数
		// jobMeta.setParameterValue("method", "update");

		job.setLogLevel(LogLevel.DETAILED);
		// 启动执行指定的job
		job.start();

		job.waitUntilFinished();// 等待job执行完;
		job.setFinished(true);
		System.out.println(job.getResult());

	}

/**
 * 执行转换
 * 
 * @param repository
 * @throws KettleException
*/
	@SuppressWarnings("unused")
	private static void execTran(KettleDatabaseRepository repository) throws KettleException {
		// 根据指定的字符串路径 找到目录
		RepositoryDirectoryInterface dir = repository.findDirectory("/201811_KTR/A_KTR");
		// 在 指定目录下找转换
		TransMeta tmeta = repository.loadTransformation(repository.getTransformationID("A_TRAN", dir), null);
		// 设置参数
		// tmeta.setParameterValue("", "");
		Trans trans = new Trans(tmeta);
		trans.execute(null);// 执行trans
		trans.waitUntilFinished();// 等待直到数据结束
		if (trans.getErrors() > 0) {
			System.out.println("transformation error");
		} else {
			System.out.println("transformation successfully");
		}
	}

	/**
	 * 设置kettle资源库连接信息
	 * 
	 * @return
	 */
	private static DatabaseMeta setDatabaseMeta() {
		return new DatabaseMeta(PropUtils.getConfigValByKey("name"), PropUtils.getConfigValByKey("type"), PropUtils.getConfigValByKey("access"), PropUtils.getConfigValByKey("host"), PropUtils.getConfigValByKey("db"), PropUtils.getConfigValByKey("port"), PropUtils.getConfigValByKey("user"), PropUtils.getConfigValByKey("pass"));
	}
}

部分pom.xml内容

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<downloadJavadocs>true</downloadJavadocs>
		<downloadSources>true</downloadSources>
		<kettle.version>7.1.0.1-34</kettle.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>
		<!-- kettle -->
		<dependency>
			<groupId>pentaho-kettle</groupId>
			<artifactId>kettle-engine</artifactId>
			<version>${kettle.version}</version>
		</dependency>
		<dependency>
			<groupId>pentaho</groupId>
			<artifactId>metastore</artifactId>
			<version>${kettle.version}</version>
		</dependency>
		<dependency>
			<groupId>pentaho-kettle</groupId>
			<artifactId>kettle-core</artifactId>
			<version>${kettle.version}</version>
			<exclusions>
				<exclusion>
					<groupId>jug-lgpl</groupId>
					<artifactId>jug-lgpl</artifactId>
				</exclusion>
				<exclusion>
					<groupId>secondstring</groupId>
					<artifactId>secondstring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<artifactId>xercesImpl</artifactId>
					<groupId>xerces</groupId>
				</exclusion>
				<exclusion>
					<groupId>org.apache.xmlgraphics</groupId>
					<artifactId>batik-js</artifactId>
				</exclusion>
			</exclusions>
		</dependency>


		<dependency>
			<groupId>pentaho-kettle</groupId>
			<artifactId>kettle-dbdialog</artifactId>
			<version>${kettle.version}</version>
		</dependency>


		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>
		<dependency>
			<groupId>com.verhas</groupId>
			<artifactId>license3j</artifactId>
			<version>1.0.7</version>
		</dependency>
		<!-- <dependency> <groupId>org.pentaho.di.plugins</groupId> <artifactId>kettle-json-plugin-core</artifactId> <version>8.0.0.0-28</version> </dependency> <dependency> <groupId>org.pentaho.di.plugins</groupId> 
			<artifactId>kettle-json-plugin</artifactId> <version>8.0.0.0-28</version> <type>zip</type> </dependency> -->
		<!-- kettle/end -->
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>
	</dependencies>

gitee地址

demo gitee地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值