kettle 创建java项目_Java项目中使用Kettle,在java中使用kettle的API,此处以CSV为例...

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.pentaho.di.core.Const;

import org.pentaho.di.core.KettleEnvironment;

import org.pentaho.di.core.RowMetaAndData;

import org.pentaho.di.core.exception.KettleException;

import org.pentaho.di.core.exception.KettleStepException;

import org.pentaho.di.core.row.RowMetaInterface;

import org.pentaho.di.core.row.ValueMetaInterface;

import org.pentaho.di.core.vfs.KettleVFS;

import org.pentaho.di.core.xml.XMLHandler;

import org.pentaho.di.trans.Trans;

import org.pentaho.di.trans.TransHopMeta;

import org.pentaho.di.trans.TransMeta;

import org.pentaho.di.trans.step.RowListener;

import org.pentaho.di.trans.step.StepInterface;

import org.pentaho.di.trans.step.StepMeta;

import org.pentaho.di.trans.steps.csvinput.CsvInputMeta;

import org.pentaho.di.trans.steps.dummytrans.DummyTransMeta;

import org.pentaho.di.trans.steps.textfileinput.TextFileInputField;

/**

* kettle 创建动态转换

*

* 共包含两个步骤,一个是读取CSV文件,一个是空操作

*

*

*/

public class CsvFileReader {

private static String STEP_READ_A_FILE = "Read a file";

private static String STEP_DUMMY = "Dummy";

private String fileName;

private TextFileInputField[] inputFields;

private List rows;

public CsvFileReader(String filename,TextFileInputField[] inputFields) {

this.fileName = filename;

this.inputFields = inputFields;

}

public void read() throws KettleException {

KettleEnvironment.init();

//transformation

TransMeta transMeta = new TransMeta();

transMeta.setName("sample04");//定义转换名称

CsvInputMeta inputMeta = new CsvInputMeta();

inputMeta.setDefault();

inputMeta.setFilename(fileName);

inputMeta.setInputFields(inputFields);

StepMeta inputStep = new StepMeta(STEP_READ_A_FILE,inputMeta);

inputStep.setLocation(50, 50);

inputStep.setDraw(true);

transMeta.addStep(inputStep);

DummyTransMeta dummyMeta = new DummyTransMeta();

StepMeta dummyStep = new StepMeta(STEP_DUMMY,dummyMeta);

dummyStep.setLocation(150,50);

dummyStep.setDraw(true);

transMeta.addStep(dummyStep);

TransHopMeta hop = new TransHopMeta(inputStep, dummyStep);

transMeta.addTransHop(hop);

//可以将转换输出成文件

//String xml = XMLHandler.getXMLHeader()+transMeta.getXML();

//DataOutputStream dos = new DataOutputStream(KettleVFS.getOutputStream("F:\\\\csv.ktr", false));

//try {

//dos.write(xml.getBytes(Const.XML_ENCODING));

//dos.close();

//} catch (UnsupportedEncodingException e) {

TODO Auto-generated catch block

//e.printStackTrace();

//} catch (IOException e) {

TODO Auto-generated catch block

//e.printStackTrace();

//}

//end

Trans trans = new Trans(transMeta);

trans.prepareExecution(null);

rows = new ArrayList();

RowListener rowLinstener = new RowListener() {

@Override

public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {

// TODO Auto-generated method stub

rows.add(new RowMetaAndData(rowMeta, row));

}

@Override

public void rowReadEvent(RowMetaInterface arg0, Object[] arg1) throws KettleStepException {

// TODO Auto-generated method stub

}

@Override

public void errorRowWrittenEvent(RowMetaInterface arg0, Object[] arg1) throws KettleStepException {

// TODO Auto-generated method stub

}

};

StepInterface setpInterface = trans.findRunThread(STEP_DUMMY);

setpInterface.addRowListener(rowLinstener);

trans.startThreads();

trans.waitUntilFinished();

if(trans.getErrors() > 0) {

System.out.println(">>>>>>>>>> ERROR");

}else {

System.out.println(">>>>>>>>>> Row size "+rows.size());

}

}

public static void main(String[] args) {

String file = "F:\\csv.csv";

TextFileInputField no1 = new TextFileInputField("id", -1, 8);

no1.setTrimType(ValueMetaInterface.TRIM_TYPE_BOTH);

no1.setFormat("#");

no1.setType(ValueMetaInterface.TYPE_INTEGER);

TextFileInputField no2 = new TextFileInputField("name", -1, 50);

no2.setTrimType(ValueMetaInterface.TRIM_TYPE_BOTH);

no2.setFormat("#");

no2.setType(ValueMetaInterface.TYPE_STRING);

TextFileInputField[] inputs = new TextFileInputField[] {no1,no2};

CsvFileReader reader = new CsvFileReader(file, inputs);

try {

reader.read();

} catch (KettleException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值