Java は kettle API を呼び出して変換テーブルの入出力を作成します

最近 Kettle API を使用して何かをしたので、このコラムに記録します。

図に示すように、ケトルを使用してサンプル テーブル入力 -> テーブル出力を完了します。

画像の説明を追加してください

サンプル分析:

図に示すように、接続ソース (入力オブジェクトと出力オブジェクトが同じソースを使用する)、テーブル入力オブジェクト、テーブル出力オブジェクト、およびホップ オブジェクトが必要です。

コード:
使用される Jar パッケージ: 特定の JAR パッケージについては、ここを参照してコンパイルして取得してください https://blog.csdn.net/weixin_43889494/article/details/132211256?spm=1001.2014.3001.5501
ここに画像の説明を挿入します

1. ケトル環境を初期化する (非常に重要)

KettleEnvironment.init();

画像の説明を追加してください

2. データソース ここでのデータソースは従来のjdbc URL、ドライバー、アカウント、パスワードの形式を使用しており、国内のリレーショナルデータベースを使用する場合は一般的にこれを使用できます。

static final String SOURCE_DB_NAME = "源数据库";
static final String SOURCE_DB_USER = "";
static final String SOURCE_DB_PASS = "Y@1234";
static final String SOURCE_DB_URL = "jdbc:mysql://192.168.10.105:/mysql;
static final String SOURCE_DB_DRIVER = "com.mysql.cj.jdbc.Driver";
DatabaseMeta sourceMeta = new DatabaseMeta();
sourceMeta.setAccessType(DatabaseMeta.getAccessType("Native"));
sourceMeta.setDatabaseType("GENERIC");
sourceMeta.setUsername(SOURCE_DB_USER);
sourceMeta.setPassword(SOURCE_DB_PASS);
sourceMeta.setName(SOURCE_DB_NAME);
Properties sourceProperties = new Properties();
sourceProperties.setProperty("CUSTOM_URL", SOURCE_DB_URL);
//引入驱动依赖或jar包即可
sourceProperties.setProperty("CUSTOM_DRIVER_CLASS", SOURCE_DB_DRIVER);
sourceProperties.setProperty("FORCE_IDENTIFIERS_TO_LOWERCASE", "N");
sourceProperties.setProperty("FORCE_IDENTIFIERS_TO_UPPERCASE", "N");
sourceMeta.setAttributes(sourceProperties);
//连接测试
String testInfo = sourceMeta.testConnection();

3. テーブル入力 テーブル出力

画像の説明を追加してください

// 插件在画布的位置
Point point = new Point(100, 100);
//表输入
TableInputMeta tableInputMeta = new TableInputMeta();
tableInputMeta.setDatabaseMeta(sourceMeta);
tableInputMeta.setSQL("select `id`,`tel`,`email`,`bank_id`,`name`,`h_no`,`h_ano`,`shuihao`,`com`,`com_no`,`phone`,`aomen`,`ip`,`idcard`,`addr`,`date`,`c_no`,`rmb`,`num_2`,`vin`,`zc`,`addnew`,`wz`,`completely`,`single`,`sh`,`sw`,`jg`,`mac`,`hg_no`,`huix`,`ydsbm`,`ga_no`,`eg_name`,`eg_addr`,`ftmz`,`tid` from `aliyu_kettle_tran_source`.`sensitive_table1`");
// 获取注册插件的id,这里的id类似于策略模式,进行任务分发
String tableInputPluginId = PluginRegistry.getInstance().getPluginId(StepPluginType.class, tableInputMeta);
StepMeta tableInputStepMeta = new StepMeta(tableInputPluginId, TABLE_INPUT_NMAE, tableInputMeta);
tableInputStepMeta.setDraw(true);
tableInputStepMeta.setLocation(point);
//表输出
TableOutputMeta tableOutputMeta = new TableOutputMeta();
tableOutputMeta.setDatabaseMeta(sourceMeta);
tableOutputMeta.setSchemaName("aliyu_kettle_tran_target");
tableOutputMeta.setTableName("sensitive_table1");
tableOutputMeta.setCommitSize(1);
tableOutputMeta.setTruncateTable(false);
tableOutputMeta.setIgnoreErrors(true);
tableOutputMeta.setUseBatchUpdate(true);
//指定数据库字段,流里的字段和数据库字段做对比
tableOutputMeta.setSpecifyFields(true);
tableOutputMeta.setTableNameInTable(false);
//数据库中的字段,这里会拼接insert sql
tableOutputMeta.setFieldDatabase("`id`,`tel`,`email`,`bank_id`,`name`,`h_no`,`h_ano`,`shuihao`,`com`,`com_no`,`phone`,`aomen`,`ip`,`idcard`,`addr`,`date`,`c_no`,`rmb`,`num_2`,`vin`,`zc`,`addnew`,`wz`,`completely`,`single`,`sh`,`sw`,`jg`,`mac`,`hg_no`,`huix`,`ydsbm`,`ga_no`,`eg_name`,`eg_addr`,`ftmz`,`tid`".split(","));
//上一个步骤,数据流中的字段
tableOutputMeta.setFieldStream("id,tel,email,bank_id,name,h_no,h_ano,shuihao,com,com_no,phone,aomen,ip,idcard,addr,date,c_no,rmb,num_2,vin,zc,addnew,wz,completely,single,sh,sw,jg,mac,hg_no,huix,ydsbm,ga_no,eg_name,eg_addr,ftmz,tid".split(","));
String tableOutputPluginId = PluginRegistry.getInstance().getPluginId(StepPluginType.class, tableOutputMeta);
StepMeta tableOutputStepMeta = new StepMeta(tableOutputPluginId, TABLE_OUTPUT_NMAE, tableOutputMeta);
tableOutputStepMeta.setDraw(true);
tableOutputStepMeta.setLocation(point);
  1. 変換を実行する
TransMeta transMeta = new TransMeta();
transMeta.setName("trans");
transMeta.addDatabase(sourceMeta);
transMeta.addStep(tableInputStepMeta);
transMeta.addStep(tableOutputStepMeta);
//建立连接 hop 表输入->表输出
transMeta.addTransHop(new TransHopMeta(tableInputStepMeta, tableOutputStepMeta));
//打印ktr脚本,可以放在kettle工具执行
FileOut.writeFile("yous file path", transMeta);
Trans trans = new Trans(transMeta);
trans.execute(null);
trans.waitUntilFinished();
  1. ツール
public class FileOut {
    
    
    public static void writeFile(String path, TransMeta transMeta, DefaultAcquisitionTransXml<AbstractMeta> defaultData){
    
    
        try{
    
    
            Files.write(Paths.get(path),defaultData.getAddXmlStep(transMeta).getBytes());
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
    }
    public static void writeFile(String path, TransMeta transMeta){
    
    
        try {
    
    
            writeFile(path,transMeta,(tm)-> DefaultAcquisitionTransXml.XML_HEAD+"\n"+tm.getXML());
            System.out.printf("转换元 %s 打印成功(覆盖性打印)", transMeta.getName());
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
    }
}
@FunctionalInterface
public interface DefaultAcquisitionTransXml<T extends AbstractMeta> {
    
    
     String XML_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
     String getAddXmlStep(T transMeta) throws KettleException;
}

おすすめ

転載: blog.csdn.net/weixin_43889494/article/details/132168551