USER在pentaho数据集成中定义了java类步骤

时间:2014-03-25 06:31:12

标签: java mysql jdbc pentaho

  • 您好,

    我在USER DEFINED JAVA CLASS中使用以下代码:

     //STEP 1. Import required packages import java.sql.*;  
    import  org.pentaho.di.core.database.*;
      public class JDBCExample  {    
    // JDBC driver name and database URL      
      static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
          static final String DB_URL ="jdbc:mysql://localhost:1111/mysql";
    
     //  Database credentials    
           static final String USER = "USER"; 
           static final String PASS = "PASS";
         public static void main(String[] args) {    
         Connection conn = null;    
         Statement stmt = null;    try{
        //STEP 2: Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");
    
    
     //STEP 3: Open a connection
     System.out.println("Connecting to a selected database...");
     conn = DriverManager.getConnection(DB_URL, USER, PASS);
     System.out.println("Connected database successfully...");
    
     //STEP 4: Execute a query
     System.out.println("Creating statement...");
     stmt = conn.createStatement();
    
     String sql = "select id,sorname,src_databasetype,src_databasename from table";
     ResultSet rs = stmt.executeQuery(sql);
     //STEP 5: Extract data from result set
     while(rs.next()){
        //Retrieve by column name
        int id  = rs.getInt("id");
        String sorname = rs.getString("sorname");
        String src_databasetype = rs.getString("src_databasetype");
        String src_databasename = rs.getString("src_databasename");
    
        //Display values
        System.out.print("ID: " + id);
        System.out.print(", sorname: " + sorname);
        System.out.print(", src_databasetype: " + src_databasetype);
        System.out.println(", src_databasename: " + src_databasename);
     }
     rs.close();    }catch(SQLException se){
     //Handle errors for JDBC
     se.printStackTrace();    }catch(Exception e){
     //Handle errors for Class.forName
     e.printStackTrace();    }finally{
     //finally block used to close resources
     try{
        if(stmt!=null)
           conn.close();
     }catch(SQLException se){
     }// do nothing
     try{
        if(conn!=null)
           conn.close();
     }catch(SQLException se){
        se.printStackTrace();
     }//end finally try    }//end try    System.out.println("Goodbye!"); }//end main }//end JDBCExample 
    

    通过命令提示符运行代码,其工作正常

    但是单独运行步骤(在PDI中)我收到错误: 非抽象类"处理器"必须实现方法" boolean org.pentaho.di.trans.steps.userdefinedjavaclass.TransformClassBase.processRow(org.pentaho.di.trans.step.StepMetaInterface, org.pentaho.di.trans.step.StepDataInterface)抛出 org.pentaho.di.core.exception.KettleException"

2 个答案:

答案 0 :(得分:2)

对于UDJC,我认为不是将代码放在main方法中,而是需要将它放在processRow()中。

而不是使用 - public static void main(String [] args) 使用 - public boolean processRow(StepMetaInterface smi,StepDataInterface sdi)抛出KettleException

我仍然怀疑它是否还会继续工作,因为我不明白你在尝试用这些代码做什么。

答案 1 :(得分:0)

尝试如下在processRow方法中编写代码:

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException{


    Object[] r = getRow();

    if (r == null) {
      setOutputDone();
      return false;
    }
    Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());

    //String row = getString(r)+",";
    //setValue(outputRow, row)
    putRow(data.outputRowMeta, outputRow);

    return true;

}