工具:springboot,kettle核心jar包(xxl-job,jenkins,k8,docker)
实现数据库的增量同步,需要编写好kettle转换文件以配置文件的形式,整合到springboot,有改动只需要更换转换文件,不需要更新jar包,job用于灵活调整定时同步,再结合jenkins自动部署,十分方便。
1、首先用kettle画图制作转换文件,Spoon.bat启动;
2、springboot整合
结构:
依赖:
lib存放kettle的核心jar包(在kettle软件的lib里都有):
核心代码:
/** * 执行转换 */ public R runTrans(){ this.runKtr(kettleConfig.getPathSupplier(),null); this.runKtr(kettleConfig.getPathBankInfo(),null); this.runKtr(kettleConfig.getPathQua(),null); return R.ok("转换执行成功"); } /** * 执行ktr文件 * * @param filename supplier-4.ktr supplier-bank-info.ktr supplier-qua.ktr * @param params * @return */ @Override public R runKtr(String filename, Map<String, String> params) { try { KettleEnvironment.init(); // TransMeta tm = new TransMeta(dirPath1 + File.separator + filename); TransMeta tm = new TransMeta(filename); Trans trans = new Trans(tm); if (params != null) { Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, String> entry = entries.next(); trans.setParameterValue(entry.getKey(), entry.getValue()); } } trans.execute(null); trans.waitUntilFinished(); } catch (Exception e) { e.printStackTrace(); return R.fail("启动转换失败"); } return R.ok("转换执行成功"); } /** * 执行kjb文件 * * @param filename supplier-single.kjb * @param params * @return */ @Override public R runKjb(String filename, Map<String, String> params) { try { KettleEnvironment.init(); JobMeta jm = new JobMeta(filename, null); Job job = new Job(null, jm); if (params != null) { Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, String> entry = entries.next(); job.setVariable(entry.getKey(), entry.getValue()); } } job.start(); job.waitUntilFinished(); } catch (Exception e) { e.printStackTrace(); } return R.ok("job执行成功"); }
jenkins部署:
xxl-job定时任务,实现定时同步(kettle自身也可以设置定时,但是job更为灵活):
kettle转换文件编写简易整理--->待续
springboot代码整合kettle完整代码--->待续