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;
}
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();
}
}
}
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;
}
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;
}
Aggregations