Search in sources :

Example 6 with Connection

use of org.pentaho.cassandra.spi.Connection in project pentaho-cassandra-plugin by pentaho.

the class CassandraOutputDialog method popupSchemaInfo.

protected void popupSchemaInfo() {
    Connection conn = null;
    Keyspace kSpace = null;
    try {
        String hostS = transMeta.environmentSubstitute(m_hostText.getText());
        String portS = transMeta.environmentSubstitute(m_portText.getText());
        String userS = m_userText.getText();
        String passS = m_passText.getText();
        if (!Utils.isEmpty(userS) && !Utils.isEmpty(passS)) {
            userS = transMeta.environmentSubstitute(userS);
            passS = transMeta.environmentSubstitute(passS);
        }
        String keyspaceS = transMeta.environmentSubstitute(m_keyspaceText.getText());
        try {
            Map<String, String> opts = new HashMap<String, String>();
            opts.put(CassandraUtils.CQLOptions.CQLVERSION_OPTION, CassandraUtils.CQLOptions.CQL3_STRING);
            conn = CassandraUtils.getCassandraConnection(hostS, Integer.parseInt(portS), userS, passS, ConnectionFactory.Driver.BINARY_CQL3_PROTOCOL, opts);
            conn.setHosts(hostS);
            conn.setDefaultPort(Integer.parseInt(portS));
            conn.setUsername(userS);
            conn.setPassword(passS);
            kSpace = conn.getKeyspace(keyspaceS);
        } catch (Exception e) {
            logError(// $NON-NLS-1$
            BaseMessages.getString(PKG, "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e.getLocalizedMessage(), // $NON-NLS-1$
            e);
            new ErrorDialog(shell, BaseMessages.getString(PKG, // $NON-NLS-1$
            "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Title"), // $NON-NLS-1$
            BaseMessages.getString(PKG, "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e.getLocalizedMessage(), // $NON-NLS-1$
            e);
            return;
        }
        String table = transMeta.environmentSubstitute(m_tableCombo.getText());
        if (Utils.isEmpty(table)) {
            // $NON-NLS-1$
            throw new Exception("No table name specified!");
        }
        table = CassandraUtils.cql3MixedCaseQuote(table);
        // if (!CassandraColumnMetaData.tableExists(conn, table)) {
        if (!kSpace.tableExists(table)) {
            throw new Exception(// $NON-NLS-1$ //$NON-NLS-2$
            "The table '" + table + "' does not " + "seem to exist in the keyspace '" + // $NON-NLS-1$
            keyspaceS);
        }
        ITableMetaData cassMeta = kSpace.getTableMetaData(table);
        // CassandraColumnMetaData cassMeta = new CassandraColumnMetaData(conn,
        // table);
        String schemaDescription = cassMeta.describe();
        ShowMessageDialog smd = // $NON-NLS-1$
        new ShowMessageDialog(shell, SWT.ICON_INFORMATION | SWT.OK, "Schema info", schemaDescription, true);
        smd.open();
    } catch (Exception e1) {
        logError(// $NON-NLS-1$
        BaseMessages.getString(PKG, "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e1.getMessage(), // $NON-NLS-1$
        e1);
        new ErrorDialog(shell, BaseMessages.getString(PKG, // $NON-NLS-1$
        "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Title"), // $NON-NLS-1$
        BaseMessages.getString(PKG, "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e1.getMessage(), // $NON-NLS-1$
        e1);
    } finally {
        if (conn != null) {
            try {
                conn.closeConnection();
            } catch (Exception e) {
                // TODO popup another error dialog
                e.printStackTrace();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Keyspace(org.pentaho.cassandra.spi.Keyspace) ShowMessageDialog(org.pentaho.di.ui.core.dialog.ShowMessageDialog) Connection(org.pentaho.cassandra.spi.Connection) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ITableMetaData(org.pentaho.cassandra.spi.ITableMetaData) KettleException(org.pentaho.di.core.exception.KettleException)

Example 7 with Connection

use of org.pentaho.cassandra.spi.Connection in project pentaho-cassandra-plugin by pentaho.

the class CassandraUtils method getCassandraConnection.

/**
 * Get a connection to cassandra
 *
 * @param host
 *          the hostname of a cassandra node
 * @param port
 *          the port that cassandra is listening on
 * @param username
 *          the username for (optional) authentication
 * @param password
 *          the password for (optional) authentication
 * @param driver
 *          the driver to use
 * @param opts
 *          the additional options to the driver
 * @return a connection to cassandra
 * @throws Exception
 *           if a problem occurs during connection
 */
public static Connection getCassandraConnection(String host, int port, String username, String password, ConnectionFactory.Driver driver, Map<String, String> opts) throws Exception {
    Connection conn = ConnectionFactory.getFactory().getConnection(driver);
    conn.setHosts(host);
    conn.setDefaultPort(port);
    conn.setUsername(username);
    conn.setPassword(password);
    conn.setAdditionalOptions(opts);
    return conn;
}
Also used : Connection(org.pentaho.cassandra.spi.Connection)

Aggregations

Connection (org.pentaho.cassandra.spi.Connection)7 Keyspace (org.pentaho.cassandra.spi.Keyspace)5 KettleException (org.pentaho.di.core.exception.KettleException)5 HashMap (java.util.HashMap)4 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)3 ITableMetaData (org.pentaho.cassandra.spi.ITableMetaData)2 ShowMessageDialog (org.pentaho.di.ui.core.dialog.ShowMessageDialog)2 Selector (org.pentaho.cassandra.util.Selector)1 KettleStepException (org.pentaho.di.core.exception.KettleStepException)1 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1