kettle api 执行转换
import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.Date; import be.ibridge.kettle.core.Const; import be.ibridge.kettle.core.LogWriter; import be.ibridge.kettle.core.NotePadMeta; import b
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
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.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;
/**
*
*
Title:
* 本文描述了以下操作:
1) 建立一个新的转换(transformation)
2) 把转换(transformation)存储为XML文件
3) 生成需要在目标表运行的SQL语句
4) 执行转换(transformation)
5) 删除目标表,可以使测试程序可以反复执行(这一点可根据需要修改)。
*
Description: TODO 类的功能描述
*
Copyright: Copyright (c) 2003
* @author 洪亮
* @version 1.0
*
------------------------------------------------------------
*
修改历史
*
序号 日期 时间 修 改 人 修 改 原 因
*
1 2006-9-20 下午05:59:06 洪亮 创建
*
*/
public class TransBuilderME
{
public static final String[] databasesXML = {
"" +
"
"
"
"
"
"
"
"
"
"
"" +
"
"
"
"
"
"
"
"
"
"
};
/**
* 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();
try
{
//
// Create a new transformation...
//传输元信息
TransMeta transMeta = new TransMeta();
transMeta.setName(transformationName);//传输名称
// Add the database connections
for (int i=0;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 + "]";//from步骤名称
TableInputMeta tii = new TableInputMeta();//表输入元数据信息
tii.setDatabaseMeta(sourceDBInfo);//为表输入 指定 数据库
String selectSQL = "SELECT "+Const.CR;//拼接查询sql语句
for (int i=0;i
if (i>0) selectSQL+=", "; else selectSQL+=" ";
selectSQL+=sourceFields[i]+Const.CR;
}
selectSQL+="FROM "+sourceTableName;
tii.setSQL(selectSQL);//设置查询sql语句
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 {
//设置源字段和目标字段
svi.getMetaName()[i] = sourceFields[i];
svi.getMetaRename()[i] = targetFields[i];
}
String selstepname = "Rename field names";
//获取步骤插件ID
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);
//传输连接元数据信息(连接from和select)
TransHopMeta shi = new TransHopMeta(fromstep, selstep);
transMeta.addTransHop(shi);//添加到传输元对象
fromstep = selstep;//然后设置from步骤为select步骤
//
// 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(3000);//设置批量提交数
toi.setTruncateTable(true);//是否清除原有数据
//获取步骤ID
String tostepid = steploader.getStepPluginID(toi);
//创建to步骤
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...
//
//创建连接 from--to
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
{
long start = new Date().getTime();
// Init the logging...
LogWriter log = LogWriter.getInstance("TransBuilder.log", true, LogWriter.LOG_LEVEL_DETAILED);
// Load the Kettle steps & plugins
StepLoader 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 = "emp_collect";
String sourceFields[] = {
"empno",
"ename",
"job",
"mgr",
"comm",
"sal",
"deptno",
"birthday"
};
String targetDatabaseName = "target";
String targetTableName = "emp_kettle01";
String targetFields[] = {
"empno01",
"ename01",
"job01",
"mgr01",
"comm",
"sal",
"deptno",
"birthday"
};
// Generate the transformation.
//创建转换元对象
TransMeta transMeta = TransBuilderME.buildCopyTable(
transformationName,
sourceDatabaseName,
sourceTableName,
sourceFields,
targetDatabaseName,
targetTableName,
targetFields
);
// transMeta = new TransMeta();
// Save it as a file:
//传输元对象 中获得XML,并输出
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?
//获得sql语句,创建表语句
String sql = transMeta.getSQLStatementsString();
// Execute the SQL on the target table:
//创建表
Database targetDatabase = new Database(transMeta.findDatabase(targetDatabaseName));
targetDatabase.connect();//连接数据库
targetDatabase.execStatements(sql);//执行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
// targetDatabase.execStatement("drop table "+targetTableName);
targetDatabase.disconnect();//断开数据库连接
long end = new Date().getTime();
System.out.println("运行时间:" + (end - start) / 1000 + "秒");
long min = (end - start) / 1000 / 60;
long second = (end - start) / 1000 % 60;
System.out.println("运行时间:" + min + "分钟" + second + "秒");
}
}

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

全角英文字母轉換為半角形式的實用技巧在現代生活中,我們經常會接觸到英文字母,在使用電腦、手機等設備時也經常需要輸入英文字母。然而,有時候我們會遇到全角英文字母的情況,而我們需要使用的是半角形式。那麼,如何將全角英文字母轉換為半角形式呢?以下就為大家介紹一些實用的技巧。首先,全角英文字母和數字是指在輸入法中佔據一個全角位置的字符,而半角英文字母和數字則是佔據一

這篇文章將詳細介紹如何將PHP中的月份轉換為英文月份的方法,同時給出具體的程式碼範例。在PHP開發中,有時候我們需要將數字表示的月份轉換為英文的月份,這在一些日期處理或資料展示的場景下非常實用。以下將從實作原理、具體程式碼範例和注意事項等方面進行詳解。一、實作原理在PHP中,可以透過使用DateTime類別和format方法來實現將數位月份轉換為英文月份。 Date

qq音樂讓大家盡情享受觀影解悶,每天都可以使用這個軟體,輕鬆滿足自己的使用,優質海量的歌曲,任由大家暢聽,也可以下載保存起來,下次聽的時候,不需要網絡,而在這裡下載的歌曲不是MP3格式的,無法在其他平台使用,會員歌曲過期後也沒有辦法再聽了,所以很多小伙伴們,都想要將歌曲轉換成MP3格式的,在這裡小編為你們提供方法,幫助大家都可以使用起來! 1、開啟電腦qq音樂,點選右上角【主選單】按鈕,點選【音訊轉碼】,選擇【新增歌曲】選項,新增需要轉換的歌曲; 2、新增歌曲完畢,點選選擇轉換為【mp3

全角英文字母變成半角字母的方法在日常生活和工作中,有時候我們會遇到需要將全角英文字母轉換為半角字母的情況,例如在輸入電腦密碼、編輯文件或設計排版時。全角英文字母和數字是指寬度與中文字符相同的字符,而半角英文字母則是指寬度較窄的字符。在實際操作中,我們需要掌握一些簡單的方法,將全角英文字母轉換為半角字母,以便更方便地處理文字和數字。一、全角英文字母與半角英

PHP教學:如何將int型別轉換為字串在PHP中,將整型資料轉換為字串是常見的操作。本教學將介紹如何使用PHP內建的函數將int型別轉換為字串,同時提供具體的程式碼範例。使用強制型別轉換:在PHP中,可以使用強制型別轉換的方式將整型資料轉換為字串。這種方法非常簡單,只需要在整型資料前加上(string)即可將其轉換為字串。下面是一個簡單的範例程式碼

PHP中的ASCII數值轉換是程式設計中常會遇到的問題。 ASCII(AmericanStandardCodeforInformationInterchange)是一種用於將字元轉換為數字的標準編碼系統。在PHP中,我們經常需要透過ASCII碼來實現字元和數字之間的轉換。本文將介紹如何在PHP中進行ASCII數值轉換,並給予具體的程式碼範例。一、將字符

Oracle是一家全球知名的資料庫管理系統供應商,其API(ApplicationProgrammingInterface,應用程式介面)是一種強大的工具,可協助開發人員輕鬆地與Oracle資料庫互動和整合。在本文中,我們將深入探討OracleAPI的使用指南,向讀者展示如何在開發過程中利用資料介面技術,同時提供具體的程式碼範例。 1.Oracle

EXE轉PHP:實現功能擴展的有效策略隨著互聯網的發展,越來越多的應用程式開始向web化遷移,以實現更大範圍的用戶訪問和更便捷的操作。在這個過程中,將原本以EXE(執行檔)方式運作的功能轉換為PHP腳本的需求也逐漸增加。本文將探討如何將EXE轉換為PHP來實現功能擴展,同時給出具體的程式碼範例。為什麼將EXE轉換為PHP跨平台性:PHP是一種跨平台的語言
