一、Developer Environment:
JDK1.6
MyEclipse6.5
Kettle4.0
二、Code List
1、创建Step日志表kettle_step_log_table(Oracle建表sql)
create table KETTLE_STEP_LOG_TABLE
(
ID_BATCH INTEGER,
CHANNEL_ID VARCHAR2(255),
LOG_DATE DATE,
TRANSNAME VARCHAR2(255),
STEPNAME VARCHAR2(255),
STEP_COPY INTEGER,
LINES_READ INTEGER,
LINES_WRITTEN INTEGER,
LINES_UPDATED INTEGER,
LINES_INPUT INTEGER,
LINES_OUTPUT INTEGER,
LINES_REJECTED INTEGER,
ERRORS INTEGER,
LOG_FIELD CLOB
)
2、配置step日志数据库连接
<connection>
<name>kettle_step_log_table</name>
<server>10.10.3.52</server>
<type>ORACLE</type>
<access>Native</access>
<database>DGWSJ</database>
<port>1521</port>
<username>bsoft</username>
<password>Encrypted 2be98afc86aa7f2e4cb79ce72cd9da9ce</password>
<servername/>
<data_tablespace/>
<index_tablespace/>
<attributes>
<attribute><code>FORCE_IDENTIFIERS_TO_LOWERCASE</code><attribute>N</attribute></attribute>
<attribute><code>FORCE_IDENTIFIERS_TO_UPPERCASE</code><attribute>N</attribute></attribute>
<attribute><code>IS_CLUSTERED</code><attribute>N</attribute></attribute>
<attribute><code>PORT_NUMBER</code><attribute>1521</attribute></attribute>
<attribute><code>QUOTE_ALL_FIELDS</code><attribute>N</attribute></attribute>
<attribute><code>SUPPORTS_BOOLEAN_DATA_TYPE</code><attribute>N</attribute></attribute>
<attribute><code>USE_POOLING</code><attribute>N</attribute></attribute>
</attributes>
</connection>
3、Trans实现代码
package com.ohgrateboy;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.StepLogTable;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.variables.Variables;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
public class ReaderTransFromFileRep {
public static void main(String[] args) throws KettleException {
KettleEnvironment.init();
Trans trans=null;
//定义变量
VariableSpace space = new Variables();
//将step日志数据库配置名加入到变量集中
space.setVariable("kettle_step_log_table","kettle_step_log_table");
space.initializeVariablesFrom(null);
try {
TransMeta transMetadel = new TransMeta("D:/Untitled.xml");
//声明StepLogTable。space-系统变量,tanMetade1-提供数据库连接
StepLogTable stepLogTable = StepLogTable.getDefault(space,transMetadel);
//StepLogTable使用的数据库连接名(上面配置的变量名)。
stepLogTable.setConnectionName("kettle_step_log_table");
//设置Step日志的表名
stepLogTable.setTableName("kettle_step_log_table");
//设置TransMeta的StepLogTable
transMetadel.setStepLogTable(stepLogTable);
// 转换
trans = new Trans(transMetadel);
// 执行转换
trans.execute(null);
// 等待转换执行结束
trans.waitUntilFinished();
//抛出异常
if(trans.getErrors()>0){
throw new Exception("There are errors during transformation exception!(传输过程中发生异常)");
}
} catch (Exception e) {
if(trans!=null){
trans.stopAll();
}
e.printStackTrace();
throw new KettleException(e);
}
}
}
4、控制台运行截图
5、step日志表查询结果截图(部分字段):