Search in sources :

Example 1 with MapiSocket

use of nl.cwi.monetdb.mcl.net.MapiSocket in project pentaho-kettle by pentaho.

the class MonetDBBulkLoader method getMonetDBConnection.

protected static MapiSocket getMonetDBConnection(String host, int port, String user, String password, String db, LogChannelInterface log) throws Exception {
    MapiSocket mserver = new MapiSocket();
    mserver.setDatabase(db);
    mserver.setLanguage("sql");
    List<?> warnings = mserver.connect(host, port, user, password);
    if (warnings != null) {
        for (Object warning : warnings) {
            if (log != null) {
                log.logBasic("MonetDB connection warning: " + warning);
            }
        }
    } else {
        if (log != null) {
            log.logDebug("Successful MapiSocket connection to MonetDB established.");
        }
    }
    return mserver;
}
Also used : MapiSocket(nl.cwi.monetdb.mcl.net.MapiSocket)

Example 2 with MapiSocket

use of nl.cwi.monetdb.mcl.net.MapiSocket in project pentaho-kettle by pentaho.

the class MonetDBBulkLoader method executeSql.

/*
   * executeSQL Uses the MonetDB API to create a new server connection and the associated buffered Reader and Writer to
   * execute a single query.
   *
   * @param Query string
   *
   * @param Host URI
   *
   * @param Numerical port
   *
   * @param Username for establishing the connection
   *
   * @param Password for establishing the connection
   *
   * @param database to connect to
   */
protected static void executeSql(String query, String host, int port, String user, String password, String db) throws Exception {
    MapiSocket mserver = null;
    try {
        mserver = getMonetDBConnection(host, port, user, password, db);
        BufferedMCLReader in = mserver.getReader();
        BufferedMCLWriter out = mserver.getWriter();
        String error = in.waitForPrompt();
        if (error != null) {
            throw new Exception("ERROR waiting for input reader: " + error);
        }
        // the leading 's' is essential, since it is a protocol
        // marker that should not be omitted, likewise the
        // trailing semicolon
        out.write('s');
        System.out.println(query);
        out.write(query);
        out.write(';');
        out.newLine();
        out.writeLine("");
        String line = null;
        while ((line = in.readLine()) != null) {
            int type = in.getLineType();
            // read till we get back to the prompt
            if (type == BufferedMCLReader.PROMPT) {
                break;
            }
            switch(type) {
                case BufferedMCLReader.ERROR:
                    System.err.println(line);
                    break;
                case BufferedMCLReader.RESULT:
                    System.out.println(line);
                    break;
                default:
                    // unknown, header, ...
                    break;
            }
        }
    } finally {
        if (mserver != null) {
            mserver.close();
        }
    }
}
Also used : MapiSocket(nl.cwi.monetdb.mcl.net.MapiSocket) BufferedMCLWriter(nl.cwi.monetdb.mcl.io.BufferedMCLWriter) BufferedMCLReader(nl.cwi.monetdb.mcl.io.BufferedMCLReader) KettleException(org.pentaho.di.core.exception.KettleException) MonetDBRowLimitException(org.pentaho.di.trans.steps.monetdbagilemart.MonetDBRowLimitException)

Example 3 with MapiSocket

use of nl.cwi.monetdb.mcl.net.MapiSocket in project pentaho-kettle by pentaho.

the class MonetDBBulkLoader method getMonetDBConnection.

protected MapiSocket getMonetDBConnection() throws Exception {
    if (this.meta == null) {
        throw new KettleException("No metadata available to determine connection information from.");
    }
    DatabaseMeta dm = meta.getDatabaseMeta();
    String hostname = environmentSubstitute(Const.NVL(dm.getHostname(), ""));
    String portnum = environmentSubstitute(Const.NVL(dm.getDatabasePortNumberString(), ""));
    String user = environmentSubstitute(Const.NVL(dm.getUsername(), ""));
    String password = Utils.resolvePassword(variables, Const.NVL(dm.getPassword(), ""));
    String db = environmentSubstitute(Const.NVL(dm.getDatabaseName(), ""));
    MapiSocket mserver = getMonetDBConnection(hostname, Integer.valueOf(portnum), user, password, db, log);
    return mserver;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) MapiSocket(nl.cwi.monetdb.mcl.net.MapiSocket) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) MonetDBDatabaseMeta(org.pentaho.di.core.database.MonetDBDatabaseMeta)

Example 4 with MapiSocket

use of nl.cwi.monetdb.mcl.net.MapiSocket in project pentaho-kettle by pentaho.

the class MonetDBBulkLoader method execute.

public boolean execute(MonetDBBulkLoaderMeta meta, boolean wait) throws KettleException {
    if (log.isDetailed()) {
        logDetailed("Started execute");
    }
    try {
        if (log.isDetailed()) {
            logDetailed("Auto String Length flag: " + meta.isAutoStringWidths());
        }
        DatabaseMeta dm = meta.getDatabaseMeta();
        String user = environmentSubstitute(Const.NVL(dm.getUsername(), ""));
        String password = Utils.resolvePassword(variables, Const.NVL(dm.getPassword(), ""));
        MapiSocket mserver = getMonetDBConnection();
        data.mserver = mserver;
        data.in = mserver.getReader();
        data.out = mserver.getWriter();
        String error = data.in.waitForPrompt();
        if (error != null) {
            throw new KettleException("Error while connecting to MonetDB for bulk loading : " + error);
        }
        data.outputLogger = new StreamLogger(log, mserver.getInputStream(), "OUTPUT");
        // If the truncate table checkbox is checked, we can do the truncate here.
        if (meta.isTruncate()) {
            truncate();
        }
        Database db = null;
        // get table metadata, will be used later for date type identification (DATE, TIMESTAMP, ...)
        try {
            db = new Database(meta.getParent(), dm);
            db.connect(user, password);
            physicalTableRowMeta = db.getTableFields(data.schemaTable);
        } catch (Exception e) {
            // try again, with the unquoted table...
            try {
                physicalTableRowMeta = db.getTableFields(meta.getTableName());
            } catch (Exception e1) {
                logBasic("Could not get metadata for the physical table " + data.schemaTable + ".");
            }
        } finally {
            if (db != null) {
                db.disconnect();
            }
        }
        meta.setCompatibilityDbVersionMode();
    } catch (Exception ex) {
        throw new KettleException(ex);
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) StreamLogger(org.pentaho.di.core.util.StreamLogger) MapiSocket(nl.cwi.monetdb.mcl.net.MapiSocket) Database(org.pentaho.di.core.database.Database) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) MonetDBDatabaseMeta(org.pentaho.di.core.database.MonetDBDatabaseMeta) KettleException(org.pentaho.di.core.exception.KettleException) MonetDBRowLimitException(org.pentaho.di.trans.steps.monetdbagilemart.MonetDBRowLimitException)

Aggregations

MapiSocket (nl.cwi.monetdb.mcl.net.MapiSocket)4 KettleException (org.pentaho.di.core.exception.KettleException)3 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)2 MonetDBDatabaseMeta (org.pentaho.di.core.database.MonetDBDatabaseMeta)2 MonetDBRowLimitException (org.pentaho.di.trans.steps.monetdbagilemart.MonetDBRowLimitException)2 BufferedMCLReader (nl.cwi.monetdb.mcl.io.BufferedMCLReader)1 BufferedMCLWriter (nl.cwi.monetdb.mcl.io.BufferedMCLWriter)1 Database (org.pentaho.di.core.database.Database)1 StreamLogger (org.pentaho.di.core.util.StreamLogger)1