kettle API
- 格式:doc
- 大小:120.50 KB
- 文档页数:28
目录1.1 kettle工具的优势 (2)1.1.1 kettle开源、高效 (2)1.1.2 kettle 非常强大而且易于使用的设计界面 (2)1.1.3 有非常强大而且众多的使用群体,应用广泛 (2)1.1.4 可用java进行集成开发 (2)1.2 kettle工具使用介绍 (3)1.2.1 资源库连接 (3)1.2.2 创建转换(transformation)或工作(job) (3)1.2.3 数据库连接 (3)1.2.4.1 数据抽取 (4)1.2.4.1 数据转换 (6)1.2.5 其他转换数据功能 (6)1.3 Java应用程序中集成Kettle (9)1.3.1 java程序分析 (9)1.3.2代码演示 (12)数据抽取之kettle工具1.1 kettle工具的优势1.1.1 kettle开源、高效Kettle是一款国外开源的etl工具,纯java编写,绿色无需安装,数据抽取高效稳定。
Kettle中有两种脚本文件,transformation和job,transformation完成针对数据的基础转换,job则完成整个工作流的控制。
1.1.2 kettle 非常强大而且易于使用的设计界面1.1.3 有非常强大而且众多的使用群体,应用广泛1.1.4 可用java进行集成开发提供了基于JAVA的脚步编写功能,可以灵活地自定义ETL过程,使自行定制、批量处理等成为可能,这才是一个程序员需要做的工作,而不仅是象使用Word一样操作kettle用户界面。
1.2 kettle工具使用介绍1.2.1 资源库连接kettle 有两种方式连接资源库:一种是纯数据库式,也就是你所有的转换全部都保存在一个数据库中,一般你在开始使用kettle的时候,它都会要求你建立一个资源仓库,这个资源仓库的连接方式就是你的数据库连接,你需要能够有相应的数据库驱动和对应的连接用户名和密码。
另外一种连接方式是使用文本文件,也就是xml文件,在做完任何转换之后,我们都可以把转换或者Job变成xml文件输出,这个输出文件包含你所有转换的全部信息。
基于kettle自动完成API数据解析入库一、需求分析为从学生信息系统中获取学生数据信息,并实时存入数据中台近源层,为后续开展相关分析准备好数据。
从源端学生信息系统获悉,源端学生信息数据统一通过数据服务API 接口开放访问,因此,数据中台侧需自行解析API获取学生信息数据。
二、学生信息API接口说明源端提供学生信息API接口信息详情如下表所示:获取apiToken方式如下:(1)资源URL:http://192.168.102.230:8080/user/ api_authentication(2)HTTP协议:post(3)Content-Type:application/json;charset=UTF-8(4)POST Body JSON 参数:(5)返回成功示例:三、kettle设计3.1 转换设计根据转换需求,设计流程如下:(1)生成记录主要设置ETL 转换中用到的一些变量及其值,如下:配置截图如下:(2)REST client主要配置访问Restful 接口,配置截图如下:(3)JSON input主要配置对Json 格式的数据源进行解析,配置截图如下:(4)表输出主要配置向数据库的表中写入数据,目标表需提前创建。
配置截图如下:3.2 作业设计根据作业运行需求,设计作业运行流程如下:(1)Start作业起点,配置作业运行周期为2分钟,每2分钟自动执行一次作业,实现API数据的解析和入库。
(2)SQL通过SQL脚本,在每次作业运行前,先对目标表进行清空数据,脚本如下:(3)转换调用3.1章节已设计好的转换,配置截图如下:(4)发送邮件对上一步转换运行结果,及时通过发送邮件告知作业运行状态,分为运行成功和运行失败2种情况。
运行成功时,组件配置如下:运行失败时,组件配置如下:四、验证启动作业,运行结果如下:到目录库查看数据表,如下图显示,确认作业运行成功,数据如期写入目标表中,同时收到作业运行成功的邮件通知。
Kettle加密参数1.引言K e tt le是一款开源的E TL工具,被广泛应用于数据集成和数据处理的场景中。
在使用Ke t tl e时,我们通常会遇到需要对敏感数据进行加密的情况。
本文将介绍如何在Ke tt le中使用加密参数,保护数据的安全性。
2.什么是加密参数加密参数是K et tl e中的一种特殊参数,通过使用加密算法,将参数的值加密后存储在作业(Jo b)或转换(T r an sf or ma ti on)的配置文件中。
这样可以确保在运行作业或转换时,敏感数据不会以明文的形式暴露在配置文件中,提高了数据的安全性。
3.如何配置加密参数3.1生成密钥文件在使用加密参数前,我们需要先生成一个密钥文件,用于加密和解密参数的值。
可以使用K et tl e自带的工具`En cr`来生成密钥文件。
打开终端,输入以下命令生成密钥文件:./En cr-s<密码>其中,`<密码>`为一个安全的字符串,用于生成密钥。
生成的密钥文件将保存在K et tl e的安装目录下。
3.2加密参数值配置文件中的参数可以通过在参数值前加上变量名`$`并使用花括号`{}`包裹来表示。
在加密参数时,我们需要将对应的参数值替换为加密后的值。
打开Ke tt le的作业或转换,进入参数配置页面。
找到需要加密的参数,将其值替换为加密后的值。
例如,假设要加密的参数为`pa ss wo rd`,加密后的值为`E NC(密文)`,则在配置文件中的参数值应为`${p as sw or d}`。
3.3解密参数值在作业或转换运行时,K et tl e会自动解密加密参数的值,并将解密后的值传递给对应的组件使用。
4.使用加密参数的场景4.1数据库连接信息加密在K et tl e中,我们经常需要连接到数据库执行数据操作。
为了保护敏感的数据库连接信息,可以将用户名和密码等参数值配置为加密参数。
通过配置加密参数,我们可以将数据库连接信息加密后存储在作业或转换配置文件中,同时确保在运行作业或转换时可以正确解密并使用这些敏感信息。
kettle开源数据仓库解决方案
Kettle是一款开源的数据集成和ETL工具,也可以称为Pentaho Data Integration。
它提供了一种可靠的、灵活的、可扩展的方式来导入、转换和加载数据。
以下是使用Kettle构建开源数据仓库的一般解决方案:
1. 数据采集:使用Kettle连接到各种数据源,如关系型数据库、文件、API等,从中提取数据并加载到数据仓库中。
2. 数据清洗和转换:使用Kettle的图形化界面设计工具来进行数据清洗和转换操作。
可以进行诸如数据过滤、字段映射、数据合并、数据转换等操作,以确保数据的一致性和质量。
3. 数据加载和集成:使用Kettle将清洗和转换后的数据加载到数据仓库中。
Kettle支持多种目标存储系统,如关系型数据库、Hadoop等。
4. 数据仓库管理:Kettle提供了一套管理工具,用于监视和管理数据仓库的运行状态。
可以使用这些工具进行数据仓库的备份、恢复和性能优化。
5. 数据报表和分析:Kettle集成了Pentaho Reporting和Pentaho Analysis,可以通过这些工具创建各种数据报表和分析模型。
6. 可视化数据探索:Kettle还集成了Pentaho Data Integration和Pentaho Analysis的功能,可以通过可视化方式探索数据并进行数据挖掘和分析。
总的来说,Kettle提供了一套完整的解决方案,用于构建开源数据仓库。
通过其强大的数据集成和转换功能,可以实现数据的采集、清洗、加载和分析,从而提高数据的一致性和质量,为企业决策提供可靠的数据支持。
调用kettle作业调用Kettle作业Kettle是一款开源的ETL(Extract-Transform-Load)工具,可以用于数据抽取、转换和加载。
在大数据处理和数据仓库等领域,Kettle被广泛应用于数据集成和数据处理的工作中。
本文将介绍如何调用Kettle作业,以及如何在作业中实现数据转换和加载的功能。
一、Kettle作业的基本概念和组成Kettle作业是由一系列步骤组成的,每个步骤都是一个独立的任务单元,可以按照顺序执行。
常见的Kettle作业步骤包括数据输入、数据转换、数据输出等。
在Kettle中,作业的执行结果可以被保存为日志文件或输出到数据库等位置。
二、调用Kettle作业的方法要调用Kettle作业,可以通过以下几种方法实现:1. 使用Kettle图形界面:Kettle提供了一个图形界面工具,可以通过拖拽和连接组件的方式来创建作业。
在图形界面中,可以直接运行和调试作业。
但是,这种方式需要手动操作,不适合自动化调用。
2. 使用Kettle命令行工具:Kettle提供了一个命令行工具,可以通过命令行参数来指定要执行的作业文件和作业参数。
通过命令行工具,可以将Kettle作业集成到脚本或其他程序中,实现自动化调用。
3. 使用Kettle API:Kettle还提供了一个Java API,可以通过编写Java代码来调用Kettle作业。
通过API,可以实现更精细的控制和定制化需求。
三、实现数据转换和加载的方法Kettle作为一个强大的ETL工具,可以实现多种数据转换和加载的需求。
下面介绍几个常见的数据处理场景和相应的实现方法:1. 数据清洗:Kettle提供了多种数据清洗的步骤,如过滤、去重、替换等。
可以根据具体的需求选择合适的步骤进行配置。
2. 数据转换:Kettle支持多种数据格式的转换,如文本文件到数据库、数据库到文本文件、XML到数据库等。
可以通过选择合适的输入和输出步骤,以及相应的映射关系,实现数据的转换。
Kettle进阶之一通过接口API链接取到本地数据库有时候,一般并不是本地数据取数到本地,客户一般是链接形式给我们,从链接API接口取数到我们数仓(数据库),以下讲解实战操作操作步骤1、运行软件,进入主界面。
点击左上角的文件→ 新建→ 转换新建一个转换,并保存,转换的后缀名为ktr。
2、点击面板左侧的主对象树,选择DB连接右键,选择新建或新建数据库连接向导分别创建对数据库A和数据库B的连接。
注意:如果在点击测试连接时出现下图所示错误,检查我们的安装目录下的lib中是否有对应数据库连接的jar包文件(如mysql的jar 文件:mysql-connector-java-5.1.6-bin.jar),下载对应的jar,放到lib文件夹下,重启kettle软件,即可。
点击面板左侧的核心对象,选择输入文件夹下的表输入并把它拖到右侧的编辑区中3. 首先会有客户的一个API文档说明(客户不同,内容不同,大致相同)4、将链接复制到浏览器,会得到一堆JSON值以及参数5、客户链接一般不止一个,为了方便取数,我们将URL地址全部手动维护到需要转入的(数仓)数据库,6、双击表输入,输入SQL查询第一个URL地址,必须点击预览,否则后面会没有数据7、拉入一个HTTP client,目的1:取我们手工维护URL表的URL字段(我的是字段叫URL)。
2:获取链接地址的返回结果字段(result)8、双击HTP client,选择从字段中获取URL,URL字段名这个东西就是图7 说明的你手工维护表字段名。
下面的j结果字段名就是客户所给的信息里面的result请求结果参数,Fields不用填9、这个字段意思是编码格式,如果客户数据库编码GBK,你的是UTF8,如果这里不改,内容会乱码10、开始获取链接地址里面的字段11、双击JSON Input,填写内容=文件:源定义在一个字段里✔。
从字段获取源:result。
如果之前内容合适,下拉会自动展现11.1 开始输入字段参数,继续选择字段,里面有名称为API里面的字段名,路径固定格式$.data.[*].字段名(说明:data为返回参数,客户提供,一般就是data,[*]意思就是里面内容比较对,我们以数组方式获取,如果没有可以不选)12、根据我的图顺序走,数据库连接>>目标表(意思就是数据要引入到哪个表,表点击下面sql自动创建)>>获取字段(会通过JSON input获取到字段,然后调试左边不需要的字段)>>一定要运行sql(可以删除创建引入表所需要的东西)13、点击sql>>执行>>关闭14、到这里,我们从API取数已经建立完成了,点击“校验这个转换”,Kettle会校验并给出简单的报告。
kettle的基本介绍Kettle主要内容:⼀.ETL介绍⼆.Kettle介绍三.调⽤Kettle API⼀、ETL介绍1. ETL是什么?1).ETL分别是“Extract”、“ Transform” 、“Load”三个单词的⾸字母缩写也即数据抽取、转换、装载的过程,但我们⽇常往往简称其为数据抽取。
ETL包含了三⽅⾯:Extract(抽取):将数据从各种原始的业务系统中读取出来,这是所有⼯作的前提。
⼀般抽取过程需要连接到不同的数据源,以便为随后的步骤提供数据。
这⼀部分看上去简单⽽琐碎,实际上它是 ETL 解决⽅案的成功实施的⼀个主要障碍。
Transform(转换):按照预先设计好的规则将抽取得数据进⾏转换,使本来异构的数据格式能统⼀起来。
任何对数据的处理过程都是转换。
这些处理过程通常包括(但不限于)下⾯⼀些操作:移动数据根据规则验证数据数据内容和的修改将多个数据源的数据集成根据处理后的数据计算派⽣值和聚集值Load(装载):将转换完的数据按计划增量或全部导⼊到数据仓库中。
也就是说将数据加载到⽬标系统的所有操作。
2).ETL是(Extract)、清洗(Cleaning)、转换(Transform)、装载(Load)的过程。
是构建的重要⼀环,⽤户从抽取出所需的数据,经过,最终按照预先定义好的数据仓库模型,将数据加载到数据仓库中去。
3).ETL是BI/DW( Business Intelligence/Data Warehouse , 商务智能/数据仓库)的核⼼和灵魂,按照统⼀的规则集成并提⾼数据的价值,是负责完成数据从数据源向⽬标数据仓库转化的过程,是实施数据仓库的重要步骤。
DW(Data Warehouse)即数据仓库:这个概念是由被誉为“数据仓库之⽗”的WilliamH.Inmon博⼠提出的:数据仓库是⼀个⾯向主题的、集成的、随时间变化的、信息相对稳定的数据集合,它⽤于对企业管理和决策提供⽀持。
kettle set参数【实用版】目录1.Kettle 简介2.Kettle 参数设置的方法3.Kettle 参数设置的注意事项正文1.Kettle 简介Kettle 是一款开源的 ETL 工具,可以用于数据提取、转换和加载。
它支持多种数据源,如数据库、文件系统、API 等,并且提供了丰富的数据转换功能,如数据过滤、排序、聚合等。
Kettle 还提供了可视化的界面,使得用户可以轻松地完成 ETL 任务的设计和执行。
2.Kettle 参数设置的方法在 Kettle 中,参数设置可以通过以下几种方法完成:(1)手动设置:用户可以在 Kettle 的图形界面中,通过点击相应的按钮或菜单,手动设置参数。
例如,用户可以在“查询设计”窗口中,通过点击“添加条件”按钮,手动添加查询条件。
(2)使用变量:Kettle 支持使用变量来设置参数。
用户可以在Kettle 中定义变量,然后在需要使用参数的地方,使用变量来代替具体的值。
例如,用户可以在“查询设计”窗口中,使用变量“${date}”来表示查询的时间范围。
(3)使用参数文件:Kettle 支持使用参数文件来设置参数。
用户可以在参数文件中定义参数,然后在 Kettle 中引用参数文件。
例如,用户可以在参数文件中定义一个名为“date”的参数,然后在 Kettle 中使用“${date}”来引用该参数。
3.Kettle 参数设置的注意事项在设置 Kettle 参数时,需要注意以下几点:(1)参数的格式:Kettle 支持多种数据类型,如字符串、数字、日期等。
在设置参数时,需要确保参数的格式与 Kettle 支持的数据类型匹配。
(2)参数的范围:在设置参数时,需要确保参数的范围合理。
例如,在设置查询条件时,需要确保查询条件的范围不会导致数据丢失或重复。
(3)参数的默认值:在设置参数时,可以考虑设置默认值。
这样,在执行 ETL 任务时,如果参数未被设置,Kettle 会使用默认值。
ETL工具——kettle使用说明1简介ETL(Extract-Transform-Load的缩写,即数据抽取、转换、装载的过程),Kettle是一款国外开源的etl工具,纯java编写,数据抽取高效稳定。
2运行环境:OS:Window、Linux、Unix均可Jdk1.4以上3开始使用:Kettle可以在/网站下载。
下载kettle压缩包,因kettle为绿色软件,解压缩到任意本地路径即可。
(本文着重介绍kettle3.2.0稳定版)Spoon是一个图形用户界面,在不同平台上运行Spoon需要不同的脚本:Spoon.bat:在windows平台运行Spoon(或直接点击Kettle.exe)。
Spoon.sh:在Linux、Apple OSX、Solaris平台运行Spoon。
登陆一般选择没有资源库:Kettle中有两种脚本文件,transformation和job,transformation完成针对数据的基础转换,job则完成整个工作流的控制。
4转换(Transformation)新建一个转换kettle默认transformation文件保存后后缀名为ktr新建数据库连接(此链接也可在用到的节点处配置)填写数据源配置内容点击Test测试连接成功:核心对象切换到核心对象,菜单列出的是Transformation中可以调用的环节列表,可以通过鼠标拖动的方式对环节进行添加。
并且可通过shift+鼠标拖动,实现环节之间的连接。
常用节点介绍(红色节点后面逐一演示)类别环节名称功能说明输入文本文件输入从本地文本文件输入数据表输入从数据库表中输入数据获取系统信息读取系统信息输入数据输出文本文件输出将处理结果输出到文本文件表输出将处理结果输出到数据库表插入/更新根据处理结果对数据库表机型插入更新,如果数据库中不存在相关记录则插入,否则为更新。
会根据查询条件中字段进行判断更新根据处理结果对数据库进行更新,若需要更新的数据在数据库表中无记录,则会报错停止删除根据处理结果对数据库记录进行删除,若需要删除的数据在数据库表中无记录,则会报错停止查询数据库查询根据设定的查询条件,对目标表进行查询,返回需要的结果字段流查询将目标表读取到内存,通过查询条件对内存中数据集进行查询调用DB存储过程调用数据库存储过程转换字段选择选择需要的字段,过滤掉不要的字段,也可做数据库字段对应过滤记录根据条件对记录进行分类排序记录将数据根据某以条件,进行排序空操作无操作增加常量增加需要的常量字段脚本Modified Java扩展功能,编写JavaScript脚本,对数据进行相应处理Script Value映射映射(子转换)数据映射作业Sat Variables设置环境变量Get Variables获取环境变量表输入双击拖动到工作面板上的表输入结点,选择(或者新建)所需要的数据库连接,点击获取SQL查询语句或自行编辑SQL。
Kattle API 实战前言:为什么要用Kettle和KETTLE JA V A API?Kettle是什么?kettle:是一个开源ETL工具。
kettle提供了基于java的图形化界面,使用很方便,kettle的ETL工具集合也比较多,常用的ETL工具都包含了。
为什么使用KETTLE JA V A API:就像kettle文档所说:KETTLE JA VA API : Program your own Kettle transformation,kettle提供了基于JA V A的脚步编写功能,可以灵活地自定义ETL过程,使自行定制、批量处理等成为可能,这才是一个程序员需要做的工作,而不仅是象使用word一样操作kettle用户界面。
KETTLE JA VA API 实战操作记录:一、搭建环境:到http://www.kettle.be网站下载kettle的源码包,加压缩,例如解压缩到d:\kettle目录二、打开eclipse,新建一个项目,要使用jdk1.5.0,因为kettle的要使用System.getenv(),只有在jdk1.5.0才被支持。
提起getenv(),好像有一段几起几落的记录,曾一度被抛弃,现在又被jdk1.5支持了.三、建一个class : TransBuilder.java,可以把d:\kettle\ extra\TransBuilder.java的内容原样拷贝到你的TransBuilder.java里。
四、根据需要编辑源码。
并需要对原程序进行如下修改,在头部增加:import org.eclipse.swt.dnd.Transfer;//这个包被遗漏了,原始位置kettle根目录\libswt\win32\swt.jar//add by chq() on 2006.07.20(后来发现,不必加这个引用,因为编译时不需要)五、编译准备,在eclipse中增加jar包,主要包括(主要依据extra\TransBuilder.bat):\lib\kettle.jar\libext\CacheDB.jar\libext\SQLBaseJDBC.jar\libext\activation.jar\libext\db2jcc.jar\libext\db2jcc_license_c.jar\libext\edtftpj-1.4.5.jar\libext\firebirdsql-full.jar\libext\firebirdsql.jar\libext\gis-shape.jar\libext\hsqldb.jar\libext\ifxjdbc.jar\libext\javadbf.jar\libext\jconn2.jar\libext\js.jar\libext\jt400.jar\libext\jtds-1.1.jar\libext\jxl.jar\libext\ktable.jar\libext\log4j-1.2.8.jar\libext\mail.jar\libext\mysql-connector-java-3.1.7-bin.jar\libext\ojdbc14.jar\libext\orai18n.jar\libext\pg74.215.jdbc3.jar\libext\edbc.jar(注意:下面这个包被遗漏了,要加上。
原始位置kettle根目录\libswt\win32\swt.jar)\libswt\win32\swt.jar六、编译成功后,准备运行为使程序不必登陆就可以运行,需要设置环境署文件:kettle.properties,位置在用户目录里,一般在\Documents and Settings\用户\.kettle\,主要内容如下:KETTLE_REPOSITORY=kettle@m80KETTLE_USER=adminKETTLE_PASSWORD=passwd七、好了,现在可以运行一下了,看看数据是不是已经拷贝到目标表了。
以下是运行时的控制台信息输出:下面是自动生成的Transformation :以下为修改后的程序源码:-------------------------------------------------------------------------------- package name.chq.test;import java.io.DataOutputStream;import java.io.File;import java.io.FileOutputStream;import be.ibridge.kettle.core.Const;import be.ibridge.kettle.core.LogWriter;import be.ibridge.kettle.core.NotePadMeta;import be.ibridge.kettle.core.database.Database;import be.ibridge.kettle.core.database.DatabaseMeta;import be.ibridge.kettle.core.exception.KettleException;import be.ibridge.kettle.core.util.EnvUtil;import be.ibridge.kettle.trans.StepLoader;import be.ibridge.kettle.trans.Trans;import be.ibridge.kettle.trans.TransHopMeta;import be.ibridge.kettle.trans.TransMeta;import be.ibridge.kettle.trans.step.StepMeta;import be.ibridge.kettle.trans.step.StepMetaInterface;import be.ibridge.kettle.trans.step.selectvalues.SelectValuesMeta;import be.ibridge.kettle.trans.step.tableinput.TableInputMeta;import be.ibridge.kettle.trans.step.tableoutput.TableOutputMeta;//这个包被遗漏了,原始位置kettle根目录\libswt\win32\swt.jar//add by chq([link=][/link]) on 2006.07.20 //import org.eclipse.swt.dnd.Transfer;/*** Class created to demonstrate the creation of transformations on-the-fly. ** @author Matt**/public class TransBuilder{public static final String[] databasesXML = {"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +"<connection>" +"<name>target</name>" +"<server>192.168.17.35</server>" +"<type>ORACLE</type>" +"<access>Native</access>" +"<database>test1</database>" +"<port>1521</port>" +"<username>testuser</username>" +"<password>pwd</password>" +"<servername/>" +"<data_tablespace/>" +"<index_tablespace/>" +"<attributes>" +"<attribute><code>EXTRA_OPTION_MYSQL.defaultFetchSize</code><attribute>500</attribut e></attribute>" +"<attribute><code>EXTRA_OPTION_eCursorFetch</code><attribute>true</attribute ></attribute>" +"<attribute><code>PORT_NUMBER</code><attribute>1521</attribute></attribute>" +"</attributes>" +"</connection>" ,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +"<connection>" +"<name>source</name>" +"<server>192.168.16.12</server>" +"<type>ORACLE</type>" +"<access>Native</access>" +"<database>test2</database>" +"<port>1521</port>" +"<username>testuser</username>" +"<password>pwd2</password>" +"<servername/>" +"<data_tablespace/>" +"<index_tablespace/>" +"<attributes>" +"<attribute><code>EXTRA_OPTION_MYSQL.defaultFetchSize</code><attribute>500</attribut e></attribute>" +"<attribute><code>EXTRA_OPTION_eCursorFetch</code><attribute>true</attribute ></attribute>" +"<attribute><code>PORT_NUMBER</code><attribute>1521</attribute></attribute>" +"</attributes>" +"</connection>"};/*** Creates a new Transformation using input parameters such as the tablename to read from.* @param transformationName The name of the transformation* @param sourceDatabaseName The name of the database to read from* @param sourceTableName The name of the table to read from* @param sourceFields The field names we want to read from the source table* @param targetDatabaseName The name of the target database* @param targetTableName The name of the target table we want to write to* @param targetFields The names of the fields in the target table (same number of fields as sourceFields)* @return A new transformation* @throws KettleException In the rare case something goes wrong*/public static final TransMeta buildCopyTable(String transformationName,String sourceDatabaseName, String sourceTableName,String[] sourceFields, String targetDatabaseName, String targetTableName,String[] targetFields)throws KettleException{LogWriter log = LogWriter.getInstance();EnvUtil.environmentInit();try{//// Create a new transformation...//TransMeta transMeta = new TransMeta();transMeta.setName(transformationName);// Add the database connectionsfor (int i=0;i<databasesXML.length;i++){DatabaseMeta databaseMeta = new DatabaseMeta(databasesXML[i]);transMeta.addDatabase(databaseMeta);}DatabaseMeta sourceDBInfo = transMeta.findDatabase(sourceDatabaseName);DatabaseMeta targetDBInfo = transMeta.findDatabase(targetDatabaseName);//// Add a note//String note = "Reads information from table [" + sourceTableName+ "] on database ["+ sourceDBInfo + "]" + Const.CR;note += "After that, it writes the information to table [" + targetTableName + "] on database ["+ targetDBInfo + "]";NotePadMeta ni = new NotePadMeta(note, 150, 10, -1, -1);transMeta.addNote(ni);//// create the source step...//String fromstepname = "read from [" + sourceTableName + "]";TableInputMeta tii = new TableInputMeta();tii.setDatabaseMeta(sourceDBInfo);String selectSQL = "SELECT "+Const.CR;for (int i=0;i<sourceFields.length;i++){/* modi by chq(): use * to replace the fields,经分析,以下语句可以处理‘*‘*/if (i>0)selectSQL+=", ";else selectSQL+=" ";selectSQL+=sourceFields[i]+Const.CR;}selectSQL+="FROM "+sourceTableName;tii.setSQL(selectSQL);StepLoader steploader = StepLoader.getInstance();String fromstepid = steploader.getStepPluginID(tii);StepMeta fromstep = new StepMeta(log, fromstepid, fromstepname, (StepMetaInterface) tii);fromstep.setLocation(150, 100);fromstep.setDraw(true);fromstep.setDescription("Reads information from table [" + sourceTableName+ "] on database [" + sourceDBInfo + "]");transMeta.addStep(fromstep);//// add logic to rename fields// Use metadata logic in SelectValues, use SelectValueInfo...///* 不必改名或映射add by chq() on 2006.07.20 SelectValuesMeta svi = new SelectValuesMeta();svi.allocate(0, 0, sourceFields.length);for (int i = 0; i < sourceFields.length; i++){svi.getMetaName()[i] = sourceFields[i];svi.getMetaRename()[i] = targetFields[i];}String selstepname = "Rename field names";String selstepid = steploader.getStepPluginID(svi);StepMeta selstep = new StepMeta(log, selstepid, selstepname, (StepMetaInterface) svi);selstep.setLocation(350, 100);selstep.setDraw(true);selstep.setDescription("Rename field names");transMeta.addStep(selstep);TransHopMeta shi = new TransHopMeta(fromstep, selstep);transMeta.addTransHop(shi);fromstep = selstep; //设定了新的起点by chq([link=][/link]) on 2006.07.20*///// Create the target step...////// Add the TableOutputMeta step...//String tostepname = "write to [" + targetTableName + "]";TableOutputMeta toi = new TableOutputMeta();toi.setDatabase(targetDBInfo);toi.setTablename(targetTableName);toi.setCommitSize(200);toi.setTruncateTable(true);String tostepid = steploader.getStepPluginID(toi);StepMeta tostep = new StepMeta(log, tostepid, tostepname, (StepMetaInterface) toi);tostep.setLocation(550, 100);tostep.setDraw(true);tostep.setDescription("Write information to table [" + targetTableName + "] on database [" + targetDBInfo + "]");transMeta.addStep(tostep);//// Add a hop between the two steps...//TransHopMeta hi = new TransHopMeta(fromstep, tostep);transMeta.addTransHop(hi);// OK, if we're still here: overwrite the current transformation...return transMeta;}catch (Exception e){throw new KettleException("An unexpected error occurred creating the new transformation", e);}}/*** 1) create a new transformation* 2) save the transformation as XML file* 3) generate the SQL for the target table* 4) Execute the transformation* 5) drop the target table to make this program repeatable** @param args*/public static void main(String[] args) throws Exception{EnvUtil.environmentInit();// Init the logging...LogWriter log = LogWriter.getInstance("TransBuilder.log", true, LogWriter.LOG_LEVEL_DETAILED);// Load the Kettle steps & pluginsStepLoader stloader = StepLoader.getInstance();if (!stloader.read()){log.logError("TransBuilder", "Error loading Kettle steps & plugins... stopping now!");return;}// The parameters we want, optionally this can be String fileName = "NewTrans.xml";String transformationName = "Test Transformation"; String sourceDatabaseName = "source";String sourceTableName = "testuser.source_table"; String sourceFields[] = {"*"};String targetDatabaseName = "target";String targetTableName = "testuser.target_table"; String targetFields[] = {"*"};// Generate the transformation.TransMeta transMeta = TransBuilder.buildCopyTable(transformationName,sourceDatabaseName,sourceTableName,sourceFields,targetDatabaseName,targetTableName,targetFields);// Save it as a file:String xml = transMeta.getXML();DataOutputStream dos = new DataOutputStream(new FileOutputStream(newFile(fileName)));dos.write(xml.getBytes("UTF-8"));dos.close();System.out.println("Saved transformation to file: "+fileName);// OK, What's the SQL we need to execute to generate the target table?String sql = transMeta.getSQLStatementsString();// Execute the SQL on the target table:Database targetDatabase = new Database(transMeta.findDatabase(targetDatabaseName));targetDatabase.connect();targetDatabase.execStatements(sql);// Now execute the transformation...Trans trans = new Trans(log, transMeta);trans.execute(null);trans.waitUntilFinished();// For testing/repeatability, we drop the target table again/* modi by chq([link=][/link]) on 2006.07.20 不必删表//targetDatabase.execStatement("drop table "+targetTableName);targetDatabase.disconnect();}文章三:Kattle JA V A API/display/EAI/Pentaho+Data+Integration+-+Java+API+ExamplesKETTLE JA VA API /downloads/api.phpProgram your own Kettle transformationThe example described below performs the following actions:create a new transformationsave the transformation as XML filegenerate the SQL for the target tableExecute the transformationdrop the target table to make this program repeatableThe complete source code for the example is distributed in the distribution zip file. You can findthis file in the downloads section. (Kettle version 2.1.3 or higher)After unzipping this file, you can find the source code in the 揟ransBuilder.java� file in the 揺xtra� directory.The Kettle Java API for Kettle is found here: Kettle Java API// Generate the transformation.TransMeta transMeta = TransBuilder.buildCopyTable(transformationName,sourceDatabaseName,sourceTableName,sourceFields,targetDatabaseName,targetTableName,targetFields);// Save it as a file:String xml = transMeta.getXML();DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(fileName))); dos.write(xml.getBytes("UTF-8"));dos.close();System.out.println("Saved transformation to file: "+fileName);// OK, What's the SQL we need to execute to generate the target table?String sql = transMeta.getSQLStatementsString();// Execute the SQL on the target table:Database targetDatabase = new Database(transMeta.findDatabase(targetDatabaseName)); targetDatabase.connect();targetDatabase.execStatements(sql);// Now execute the transformation...Trans trans = new Trans(log, transMeta);trans.execute(null);trans.waitUntilFinished();// For testing/repeatability, we drop the target table againtargetDatabase.execStatement("drop table "+targetTableName);targetDatabase.disconnect();Below is the source code for the method that creates the transformation:* Creates a new Transformation using input parameters such as the tablename to read from.* @param transformationName The name of the transformation* @param sourceDatabaseName The name of the database to read from* @param sourceTableName The name of the table to read from* @param sourceFields The field names we want to read from the source table* @param targetDatabaseName The name of the target database* @param targetTableName The name of the target table we want to write to* @param targetFields The names of the fields in the target table (same number of fields as sourceFields)* @return A new transformation metadata object* @throws KettleException In the rare case something goes wrong*/public static final TransMeta buildCopyTable(String transformationName,String sourceDatabaseName,String sourceTableName,String[] sourceFields,String targetDatabaseName,String targetTableName,String[] targetFields) throws KettleException{LogWriter log = LogWriter.getInstance();try{//// Create a new transformation...//TransMeta transMeta = new TransMeta();transMeta.setName(transformationName);// Add the database connectionsfor (int i=0;i<databasesXML.length;i++){DatabaseMeta databaseMeta = new DatabaseMeta(databasesXML[i]);transMeta.addDatabase(databaseMeta);}DatabaseMeta sourceDBInfo = transMeta.findDatabase(sourceDatabaseName); DatabaseMeta targetDBInfo = transMeta.findDatabase(targetDatabaseName);//// Add a note//String note = "Reads information from table [" + sourceTableName+ "] on database [" + sourceDBInfo + "]" + Const.CR;note += "After that, it writes the information to table [" + targetTableName + "] on database [" + targetDBInfo + "]";NotePadMeta ni = new NotePadMeta(note, 150, 10, -1, -1);transMeta.addNote(ni);//// create the source step...//String fromstepname = "read from [" + sourceTableName + "]";TableInputMeta tii = new TableInputMeta();tii.setDatabaseMeta(sourceDBInfo);String selectSQL = "SELECT "+Const.CR;for (int i=0;i<sourceFields.length;i++){if (i>0) selectSQL+=", "; else selectSQL+=" ";selectSQL+=sourceFields[i]+Const.CR;}selectSQL+="FROM "+sourceTableName;tii.setSQL(selectSQL);StepLoader steploader = StepLoader.getInstance();String fromstepid = steploader.getStepPluginID(tii);StepMeta fromstep = new StepMeta(log, fromstepid, fromstepname, (StepMetaInterface) tii); fromstep.setLocation(150, 100);fromstep.setDraw(true);fromstep.setDescription("Reads information from table [" + sourceTableName + "] on database [" + sourceDBInfo + "]");transMeta.addStep(fromstep);//// add logic to rename fields// Use metadata logic in SelectValues, use SelectValueInfo...//SelectValuesMeta svi = new SelectValuesMeta();svi.allocate(0, 0, sourceFields.length);for (int i = 0; i < sourceFields.length; i++){svi.getMetaName()[i] = sourceFields[i];svi.getMetaRename()[i] = targetFields[i];}String selstepname = "Rename field names";String selstepid = steploader.getStepPluginID(svi);StepMeta selstep = new StepMeta(log, selstepid, selstepname, (StepMetaInterface) svi); selstep.setLocation(350, 100);selstep.setDraw(true);selstep.setDescription("Rename field names");transMeta.addStep(selstep);TransHopMeta shi = new TransHopMeta(fromstep, selstep);transMeta.addTransHop(shi);fromstep = selstep;//// Create the target step...////// Add the TableOutputMeta step...//String tostepname = "write to [" + targetTableName + "]";TableOutputMeta toi = new TableOutputMeta();toi.setDatabase(targetDBInfo);toi.setTablename(targetTableName);toi.setCommitSize(200);toi.setTruncateTable(true);String tostepid = steploader.getStepPluginID(toi);StepMeta tostep = new StepMeta(log, tostepid, tostepname, (StepMetaInterface) toi);tostep.setLocation(550, 100);tostep.setDraw(true);tostep.setDescription("Write information to table [" + targetTableName + "] on database [" + targetDBInfo + "]");transMeta.addStep(tostep);//// Add a hop between the two steps...//TransHopMeta hi = new TransHopMeta(fromstep, tostep);transMeta.addTransHop(hi);// The transformation is complete, return it...return transMeta;}catch (Exception e){throw new KettleException("An unexpected error occurred creating the new transformation", e); }}本文来自CSDN博客,转载请标明出处:/cissyring/archive/2008/05/29/2493865.aspx。