编写kettle当中的java脚本获取多个数据库中表的数据

1.不同数据库中的表,保证这些表的结构相同

2.表中数据和生成文本数据


3.自定义常量设置和java代码


import java.sql.*;
import org.pentaho.di.core.database.*;




public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
    Object[] r = getRow();
    if (r == null) {
        setOutputDone();
        return false;
    }


//获取数据库名和表名
    String dbName = getInputRowMeta().getString(r, "conname", null );
    String tablename = getInputRowMeta().getString(r, "tablename", null );
    if (dbName==null||tablename==null) {
      throw new KettleException("Unable to find field with name "+tablename+" in the input row.");
    }
    logBasic("table---"+tablename);


//数据库连接
    Database database=null;
    DatabaseMeta databaseMeta=null;
    try  {
          databaseMeta = getTransMeta().findDatabase(dbName);
         if (databaseMeta==null) {
         logError("A connection with name "+dbName+" could not be found!");
         setErrors(1);
         return false;
    }
      database = new Database(getTrans(), databaseMeta);
      database.connect();
       logBasic("success!");
    } catch(Exception e) {
      logError("Connecting to database "+dbName+" failed.", e);
      setErrors(1);
      return false;
    }


//查询表数据
   String sql="select id,name from "+tablename;
   ResultSet resultSet;
     try {
      resultSet = database.openQuery(sql);
      Object[] idxRow = database.getRow(resultSet);
      RowMetaInterface idxRowMeta =null;
      if(idxRow!=null){
           idxRowMeta=database.getReturnRowMeta();
      }
      int i=0;
      while(idxRow!=null){
          r = createOutputRow(r, data.outputRowMeta.size());
          int index = getInputRowMeta().size();
        // Add the index name
        //
        r[index++] = idxRowMeta.getString(idxRow, "id", null);

        // Add the column name
        
        r[index++] = idxRowMeta.getString(idxRow, "name", null);
        putRow(data.outputRowMeta, r);
          idxRow = database.getRow(resultSet);
          i++;
     }
     logBasic("idxRow--length"+i);
    
     }
   catch(Exception e) {
     throw new KettleException(e);
   }

//释放连接
   if (database!=null) {
      database.disconnect();
      database.closeQuery(resultSet);
    }

    return true;
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值