Hitachi Vantara Pentaho Community Wiki
Child pages
  • Connect to a database with DatabaseMeta
Skip to end of metadata
Go to start of metadata

Todo:

Describe how to create a database connection and use it to access a database.

So far:

  1. Create a DatabaseMeta and fill it with connection information
  2. Create a new Database using the DatabaseMeta
  3. Connect with the database
  4. Query away
  5. Shut it all down
public static void main(String[] args) {
    try {
      KettleEnvironment.init();

      DatabaseMeta dbm = new DatabaseMeta();

      dbm.setDatabaseInterface(DatabaseMeta.getDatabaseInterface("Hadoop Hive"));

      dbm.setName("DB Conn");

      dbm.setHostname("server.domain.com");
      dbm.setDBName("default");
      dbm.setDBPort("10000");

      Database db = new Database(null, dbm);

      db.connect();

      ResultSet rs = db.openQuery("select * from account");

      if(rs != null) {
        System.out.println("");
        System.out.println("");

        int colWidth = 20;

        int colCount = rs.getMetaData().getColumnCount();

        for(int i = 1; i < colCount; i++) {
          System.out.print(HiveTest.fill(rs.getMetaData().getColumnName(i), colWidth));
        }
        System.out.println("");

        while(rs.next()) {
          // Do something with your data
        }

        db.closeQuery(rs);
      }
      db.disconnect();

    } catch(Throwable e) {
      e.printStackTrace();
    }
  }