Kettle集成到SpringBoot项目案例

一、案例关键代码结构

Kettle集成到SpringBoot项目案例_第1张图片

说明:转换ktr脚本和作业kjb脚本由可视化kettle先编写好

Kettle集成到SpringBoot项目案例_第2张图片

简单案例可参考:使用Kettle将SQL Server数据批量导入到MySql

kettle用到的部分包依赖(可从kettle客户端lib包拷贝,可自行打到自己maven仓库,因是测试案例本人是本地引用):

Kettle集成到SpringBoot项目案例_第3张图片


		    com.jcraft
		    jsch
		    0.1.46
		
		

		 
			org.pentaho.di 
			kettle-core 
			8.2.0.0-342 
			system 
			${project.basedir}/lib/kettle-core-8.2.0.0-342.jar 
		
		 
			org.pentaho.di 
			kettle-engine 
			8.2.0.0-342 
			system 
			${project.basedir}/lib/kettle-engine-8.2.0.0-342.jar 
		
		
		 
			org.pentaho.metastore 
			metastore 
			8.2.0.0-342 
			system 
			${project.basedir}/lib/metastore-8.2.0.0-342.jar 
		
		
		 
			org.apache.commons 
			commons-vfs2 
			2.2 
			system 
			${project.basedir}/lib/commons-vfs2-2.2.jar 
		
		
		 
			net.sourceforge 
			jtds 
			1.3.1 
			system 
			${project.basedir}/lib/jtds-1.3.1.jar 
		
		
		 
			org.apache.commons 
			commons-logging 
			1.1.3 
			system 
			${project.basedir}/lib/commons-logging-1.1.3.jar 
		
		
		 
			org.apache.commons 
			commons-io 
			2.2 
			system 
			${project.basedir}/lib/commons-io-2.2.jar 
		
		
		 
			oracle
			ojdbc8
			8.0
			system
			${project.basedir}/lib/ojdbc8-8.0.jar
		

		
			com.ibm
			db2jcc
			1.0
			system
			${project.basedir}/lib/db2jcc.jar
		

		
			com.ibm
			db2jcc4
			1.0
			system
			${project.basedir}/lib/db2jcc4.jar
		

二、关键代码说明

1)、配置文件application.properties添加

#win系统
kettle.script.path = E://kettle_test//
#linux系统
#kettle.script.path = /home/kettle_test/
2)、Service读取Kettle脚本关键代码
@Service
public class KettleService {

    @Value("${kettle.script.path}")
    private String dirPath;

    /**
     * 执行ktr文件
     * @param filename
     * @param params
     * @return
     */
    public ResponseMsg runKtr(String filename, Map params) {
        final ResponseMsg response = new ResponseMsg<>();
        try {
            KettleEnvironment.init();
            TransMeta tm = new TransMeta(dirPath + File.separator + filename);
            Trans trans = new Trans(tm);
            if (params != null) {
                Iterator> entries = params.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry entry = entries.next();
                    trans.setParameterValue(entry.getKey(),entry.getValue());
                }
            }

            trans.execute(null);
            trans.waitUntilFinished();
            response.setHead(ResponseHead.buildSuccessHead());
        } catch (Exception e) {
            e.printStackTrace();
            String er = e.getMessage();
            response.setHead(ResponseHead.buildFailedHead(er));

        }

        return response;
    }

    /**
     * 执行kjb文件
     * @param filename
     * @param params
     * @return
     */
    public ResponseMsg runKjb(String filename, Map params) {
        final ResponseMsg response = new ResponseMsg<>();
        try {
            KettleEnvironment.init();
            JobMeta jm = new JobMeta(dirPath + File.separator + filename, null);
            Job job = new Job(null, jm);
            if (params != null) {
                Iterator> entries = params.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry entry = entries.next();
                    job.setVariable(entry.getKey(), entry.getValue());
                }
            }

            job.start();
            job.waitUntilFinished();
            response.setHead(ResponseHead.buildSuccessHead());
        } catch (Exception e) {
            e.printStackTrace();
            String er = e.getMessage();
            response.setHead(ResponseHead.buildFailedHead(er));

        }
        return response;
    }
}

3)、Controller运用案例

执行ktr脚本

    @PostMapping("patientTrans")
    public ResponseMsg patientTrans (@Valid @RequestBody RequestMsg in, BindingResult result)
            throws CommonRuntimeException{
        String fileName = "patientTrans.ktr";
        ResponseMsg response = this.kettleService.runKtr(fileName, null);
        return response;
    }
执行kjb脚本
    @RequestMapping(value = "patientIn", method = RequestMethod.POST)
    @InParamCheck
    public ResponseMsg patientIn(@Valid @RequestBody RequestMsg in, BindingResult result)
        throws CommonRuntimeException {
        String fileName = "patientIn.kjb";
        ResponseMsg response = this.kettleService.runKjb(fileName, new HashMap<>());
        return response;
    }

4)、执行结果

发送测试请求

Kettle集成到SpringBoot项目案例_第4张图片

后端执行情况

Kettle集成到SpringBoot项目案例_第5张图片

 

你可能感兴趣的:(ETL)