Search in sources :

Example 1 with Keyspace

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

the class CassandraInputDialog 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());
        String cqlText = transMeta.environmentSubstitute(m_cqlText.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, "CassandraInputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e.getLocalizedMessage(), // $NON-NLS-1$
            e);
            new ErrorDialog(shell, BaseMessages.getString(PKG, // $NON-NLS-1$
            "CassandraInputDialog.Error.ProblemGettingSchemaInfo.Title"), // $NON-NLS-1$
            BaseMessages.getString(PKG, "CassandraInputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e.getLocalizedMessage(), // $NON-NLS-1$
            e);
            return;
        }
        String table = CassandraUtils.getTableNameFromCQLSelectQuery(cqlText);
        if (Utils.isEmpty(table)) {
            // $NON-NLS-1$
            throw new Exception(BaseMessages.getString(PKG, "CassandraInput.Error.NoFromClauseInQuery"));
        }
        if (!kSpace.tableExists(table)) {
            throw new Exception(BaseMessages.getString(PKG, "CassandraInput.Error.NonExistentTable", CassandraUtils.removeQuotes(table), // $NON-NLS-1$
            keyspaceS));
        }
        String schemaDescription = kSpace.getTableMetaData(table).describe();
        ShowMessageDialog smd = new ShowMessageDialog(shell, SWT.ICON_INFORMATION | SWT.OK, "Schema info", schemaDescription, // $NON-NLS-1$
        true);
        smd.open();
    } catch (Exception e1) {
        logError(// $NON-NLS-1$
        BaseMessages.getString(PKG, "CassandraInputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e1.getMessage(), // $NON-NLS-1$
        e1);
        new ErrorDialog(shell, // $NON-NLS-1$
        BaseMessages.getString(PKG, "CassandraInputDialog.Error.ProblemGettingSchemaInfo.Title"), // $NON-NLS-1$
        BaseMessages.getString(PKG, "CassandraInputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + e1.getMessage(), // $NON-NLS-1$
        e1);
    } finally {
        if (conn != null) {
            try {
                conn.closeConnection();
            } catch (Exception e) {
                log.logError(e.getLocalizedMessage(), e);
            // TODO popup another error dialog
            }
        }
    }
}
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)

Example 2 with Keyspace

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

the class CassandraInputMeta method getFields.

@Override
public void getFields(RowMetaInterface rowMeta, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space) throws KettleStepException {
    m_specificCols = null;
    m_rowLimit = -1;
    m_colLimit = -1;
    // start afresh - eats the input
    rowMeta.clear();
    if (Utils.isEmpty(m_cassandraKeyspace)) {
        // no keyspace!
        return;
    }
    String tableName = null;
    if (!Utils.isEmpty(m_cqlSelectQuery)) {
        String subQ = space.environmentSubstitute(m_cqlSelectQuery);
        if (!subQ.toLowerCase().startsWith("select")) {
            // $NON-NLS-1$
            // not a select statement!
            // $NON-NLS-1$
            logError(BaseMessages.getString(PKG, "CassandraInput.Error.NoSelectInQuery"));
            return;
        }
        if (subQ.indexOf(';') < 0) {
            // query must end with a ';' or it will wait for more!
            // $NON-NLS-1$
            logError(BaseMessages.getString(PKG, "CassandraInput.Error.QueryTermination"));
            return;
        }
        // is there a LIMIT clause?
        if (subQ.toLowerCase().indexOf("limit") > 0) {
            // $NON-NLS-1$
            String limitS = // $NON-NLS-1$
            subQ.toLowerCase().substring(subQ.toLowerCase().indexOf("limit") + 5, subQ.length()).trim();
            // $NON-NLS-1$ //$NON-NLS-2$
            limitS = limitS.replaceAll(";", "");
            try {
                m_rowLimit = Integer.parseInt(limitS);
            } catch (NumberFormatException ex) {
                logError(BaseMessages.getString(PKG, "CassandraInput.Error.UnableToParseLimitClause", // $NON-NLS-1$
                m_cqlSelectQuery));
                m_rowLimit = 10000;
            }
        }
        // strip off where clause (if any)
        if (subQ.toLowerCase().lastIndexOf("where") > 0) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            subQ = subQ.substring(0, subQ.toLowerCase().lastIndexOf("where"));
        }
        // first determine the source table
        // look for a FROM that is surrounded by space
        // $NON-NLS-1$
        int fromIndex = subQ.toLowerCase().indexOf("from");
        String tempS = subQ.toLowerCase();
        int offset = fromIndex;
        while (fromIndex > 0 && tempS.charAt(fromIndex - 1) != ' ' && (fromIndex + 4 < tempS.length()) && tempS.charAt(fromIndex + 4) != ' ') {
            tempS = tempS.substring(fromIndex + 4, tempS.length());
            // $NON-NLS-1$
            fromIndex = tempS.indexOf("from");
            offset += (4 + fromIndex);
        }
        fromIndex = offset;
        if (fromIndex < 0) {
            // $NON-NLS-1$
            logError(BaseMessages.getString(PKG, "CassandraInput.Error.MustSpecifyATable"));
            // no from clause
            return;
        }
        tableName = subQ.substring(fromIndex + 4, subQ.length()).trim();
        if (tableName.indexOf(' ') > 0) {
            tableName = tableName.substring(0, tableName.indexOf(' '));
        } else {
            // $NON-NLS-1$ //$NON-NLS-2$
            tableName = tableName.replace(";", "");
        }
        if (tableName.length() == 0) {
            // no table specified
            return;
        }
        // is there a FIRST clause?
        if (subQ.toLowerCase().indexOf("first ") > 0) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String firstS = subQ.substring(subQ.toLowerCase().indexOf("first") + 5, subQ.length()).trim();
            // Strip FIRST part from query
            subQ = firstS.substring(firstS.indexOf(' ') + 1, firstS.length());
            firstS = firstS.substring(0, firstS.indexOf(' '));
            try {
                m_colLimit = Integer.parseInt(firstS);
            } catch (NumberFormatException ex) {
                logError(BaseMessages.getString(PKG, "CassandraInput.Error.UnableToParseFirstClause", // $NON-NLS-1$
                m_cqlSelectQuery));
                return;
            }
        } else {
            // $NON-NLS-1$
            subQ = subQ.substring(subQ.toLowerCase().indexOf("select") + 6, subQ.length());
        }
        // Reset FROM index
        // $NON-NLS-1$
        fromIndex = subQ.toLowerCase().indexOf("from");
        // now determine if its a select */FIRST or specific set of columns
        Selector[] cols = null;
        if (subQ.indexOf("*") >= 0 && subQ.toLowerCase().indexOf("count(*)") == -1) {
            // $NON-NLS-1$
            // nothing special to do here
            m_isSelectStarQuery = true;
        } else {
            m_isSelectStarQuery = false;
            // String colsS = subQ.substring(subQ.indexOf('\''), fromIndex);
            String colsS = subQ.substring(0, fromIndex);
            // Parse select expression to get selectors: columns and functions
            // $NON-NLS-1$
            cols = CQLUtils.getColumnsInSelect(colsS, true);
        }
        // try and connect to get meta data
        String hostS = space.environmentSubstitute(m_cassandraHost);
        String portS = space.environmentSubstitute(m_cassandraPort);
        String userS = m_username;
        String passS = m_password;
        if (!Utils.isEmpty(userS) && !Utils.isEmpty(passS)) {
            userS = space.environmentSubstitute(m_username);
            passS = space.environmentSubstitute(m_password);
        }
        String keyspaceS = space.environmentSubstitute(m_cassandraKeyspace);
        Connection conn = null;
        Keyspace kSpace;
        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 = CassandraInputData.getCassandraConnection(hostS, Integer.parseInt(portS), userS, passS);
         * conn.setKeyspace(keyspaceS);
         */
            kSpace = conn.getKeyspace(keyspaceS);
        } catch (Exception ex) {
            ex.printStackTrace();
            logError(ex.getMessage(), ex);
            return;
        }
        try {
            /*
         * CassandraColumnMetaData colMeta = new CassandraColumnMetaData(conn, tableName);
         */
            ITableMetaData colMeta = kSpace.getTableMetaData(tableName);
            if (cols == null) {
                // select * - use all the columns that are defined in the schema
                List<ValueMetaInterface> vms = colMeta.getValueMetasForSchema();
                for (ValueMetaInterface vm : vms) {
                    rowMeta.addValueMeta(vm);
                }
            } else {
                m_specificCols = new ArrayList<String>();
                for (Selector col : cols) {
                    if (!col.isFunction() && !colMeta.columnExistsInSchema(col.getColumnName())) {
                        // this one isn't known about in about in the schema - we can
                        // output it
                        // as long as its values satisfy the default validator...
                        logBasic(// $NON-NLS-1$
                        BaseMessages.getString(PKG, "CassandraInput.Info.DefaultColumnValidator", col));
                    }
                    ValueMetaInterface vm = colMeta.getValueMeta(col);
                    rowMeta.addValueMeta(vm);
                }
            }
        } catch (Exception ex) {
            logBasic(BaseMessages.getString(PKG, "CassandraInput.Info.UnableToRetrieveColumnMetaData", tableName), // $NON-NLS-1$
            ex);
            return;
        } finally {
            if (conn != null) {
                try {
                    conn.closeConnection();
                } catch (Exception e) {
                    throw new KettleStepException(e);
                }
            }
        }
    }
}
Also used : KettleStepException(org.pentaho.di.core.exception.KettleStepException) HashMap(java.util.HashMap) Connection(org.pentaho.cassandra.spi.Connection) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Keyspace(org.pentaho.cassandra.spi.Keyspace) ITableMetaData(org.pentaho.cassandra.spi.ITableMetaData) Selector(org.pentaho.cassandra.util.Selector)

Example 3 with Keyspace

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

the class CassandraOutput method initialize.

protected void initialize(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    m_meta = (CassandraOutputMeta) smi;
    m_data = (CassandraOutputData) sdi;
    first = false;
    m_rowsSeen = 0;
    // Get the connection to Cassandra
    String hostS = environmentSubstitute(m_meta.getCassandraHost());
    String portS = environmentSubstitute(m_meta.getCassandraPort());
    String userS = m_meta.getUsername();
    String passS = m_meta.getPassword();
    String batchTimeoutS = environmentSubstitute(m_meta.getCQLBatchInsertTimeout());
    String batchSplitFactor = environmentSubstitute(m_meta.getCQLSubBatchSize());
    String schemaHostS = environmentSubstitute(m_meta.getSchemaHost());
    String schemaPortS = environmentSubstitute(m_meta.getSchemaPort());
    if (Utils.isEmpty(schemaHostS)) {
        schemaHostS = hostS;
    }
    if (Utils.isEmpty(schemaPortS)) {
        schemaPortS = portS;
    }
    if (!Utils.isEmpty(userS) && !Utils.isEmpty(passS)) {
        userS = environmentSubstitute(userS);
        passS = environmentSubstitute(passS);
    }
    m_keyspaceName = environmentSubstitute(m_meta.getCassandraKeyspace());
    m_tableName = CassandraUtils.cql3MixedCaseQuote(environmentSubstitute(m_meta.getTableName()));
    m_consistencyLevel = environmentSubstitute(m_meta.getConsistency());
    String keyField = environmentSubstitute(m_meta.getKeyField());
    try {
        if (!Utils.isEmpty(batchTimeoutS)) {
            try {
                m_cqlBatchInsertTimeout = Integer.parseInt(batchTimeoutS);
                if (m_cqlBatchInsertTimeout < 500) {
                    // $NON-NLS-1$
                    logBasic(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Message.MinimumTimeout"));
                    m_cqlBatchInsertTimeout = 500;
                }
            } catch (NumberFormatException e) {
                // $NON-NLS-1$
                logError(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Error.CantParseTimeout"));
                m_cqlBatchInsertTimeout = 10000;
            }
        }
        if (!Utils.isEmpty(batchSplitFactor)) {
            try {
                m_batchSplitFactor = Integer.parseInt(batchSplitFactor);
            } catch (NumberFormatException e) {
                // $NON-NLS-1$
                logError(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Error.CantParseSubBatchSize"));
            }
        }
        if (Utils.isEmpty(hostS) || Utils.isEmpty(portS) || Utils.isEmpty(m_keyspaceName)) {
            throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
            "CassandraOutput.Error.MissingConnectionDetails"));
        }
        if (Utils.isEmpty(m_tableName)) {
            throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
            "CassandraOutput.Error.NoTableSpecified"));
        }
        if (Utils.isEmpty(keyField)) {
            throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
            "CassandraOutput.Error.NoIncomingKeySpecified"));
        }
        // check that the specified key field is present in the incoming data
        // $NON-NLS-1$
        String[] kparts = keyField.split(",");
        m_keyIndexes = new ArrayList<Integer>();
        for (String kpart : kparts) {
            int index = getInputRowMeta().indexOfValue(kpart.trim());
            if (index < 0) {
                throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Error.CantFindKeyField", // $NON-NLS-1$
                keyField));
            }
            m_keyIndexes.add(index);
        }
        logBasic(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
        "CassandraOutput.Message.ConnectingForSchemaOperations", // $NON-NLS-1$
        schemaHostS, schemaPortS, m_keyspaceName));
        Connection connection = null;
        // open up a connection to perform any schema changes
        try {
            connection = openConnection(true);
            Keyspace keyspace = connection.getKeyspace(m_keyspaceName);
            // Try to execute any apriori CQL commands?
            if (!Utils.isEmpty(m_meta.getAprioriCQL())) {
                String aprioriCQL = environmentSubstitute(m_meta.getAprioriCQL());
                List<String> statements = CassandraUtils.splitCQLStatements(aprioriCQL);
                logBasic(// $NON-NLS-1$
                BaseMessages.getString(// $NON-NLS-1$
                CassandraOutputMeta.PKG, // $NON-NLS-1$
                "CassandraOutput.Message.ExecutingAprioriCQL", m_tableName, aprioriCQL));
                // $NON-NLS-1$ //$NON-NLS-2$
                String compression = m_meta.getUseCompression() ? "gzip" : "";
                for (String cqlS : statements) {
                    try {
                        keyspace.executeCQL(cqlS, compression, m_consistencyLevel, log);
                    } catch (Exception e) {
                        if (m_meta.getDontComplainAboutAprioriCQLFailing()) {
                            // just log and continue
                            // $NON-NLS-1$
                            logBasic("WARNING: " + e.toString());
                        } else {
                            throw e;
                        }
                    }
                }
            }
            if (!keyspace.tableExists(m_tableName)) {
                if (m_meta.getCreateTable()) {
                    // create the table
                    boolean result = keyspace.createTable(m_tableName, getInputRowMeta(), m_keyIndexes, environmentSubstitute(m_meta.getCreateTableWithClause()), log);
                    if (!result) {
                        throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
                        "CassandraOutput.Error.NeedAtLeastOneFieldAppartFromKey"));
                    }
                } else {
                    throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
                    "CassandraOutput.Error.TableDoesNotExist", m_tableName, m_keyspaceName));
                }
            }
            if (m_meta.getUpdateCassandraMeta()) {
                // Update cassandra meta data for unknown incoming fields?
                keyspace.updateTableCQL3(m_tableName, getInputRowMeta(), m_keyIndexes, log);
            }
            // get the table meta data
            logBasic(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Message.GettingMetaData", // $NON-NLS-1$
            m_tableName));
            m_cassandraMeta = keyspace.getTableMetaData(m_tableName);
            // output (downstream) is the same as input
            m_data.setOutputRowMeta(getInputRowMeta());
            String batchSize = environmentSubstitute(m_meta.getBatchSize());
            if (!Utils.isEmpty(batchSize)) {
                try {
                    m_batchSize = Integer.parseInt(batchSize);
                } catch (NumberFormatException e) {
                    // $NON-NLS-1$
                    logError(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Error.CantParseBatchSize"));
                    m_batchSize = 100;
                }
            } else {
                throw new KettleException(BaseMessages.getString(CassandraOutputMeta.PKG, // $NON-NLS-1$
                "CassandraOutput.Error.NoBatchSizeSet"));
            }
            // Truncate (remove all data from) table first?
            if (m_meta.getTruncateTable()) {
                keyspace.truncateTable(m_tableName, log);
            }
        } finally {
            if (connection != null) {
                closeConnection(connection);
                connection = null;
            }
        }
        m_consistency = environmentSubstitute(m_meta.getConsistency());
        m_batchInsertCQL = CassandraUtils.newCQLBatch(m_batchSize, m_meta.getUseUnloggedBatch());
        m_batch = new ArrayList<Object[]>();
        // now open the main connection to use
        openConnection(false);
    } catch (Exception ex) {
        // $NON-NLS-1$
        logError(BaseMessages.getString(CassandraOutputMeta.PKG, "CassandraOutput.Error.InitializationProblem"), ex);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Connection(org.pentaho.cassandra.spi.Connection) KettleException(org.pentaho.di.core.exception.KettleException) Keyspace(org.pentaho.cassandra.spi.Keyspace)

Example 4 with Keyspace

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

the class CassandraOutputDialog method setupTablesCombo.

protected void setupTablesCombo() {
    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);
            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;
        }
        List<String> tables = kSpace.getTableNamesCQL3();
        m_tableCombo.removeAll();
        for (String famName : tables) {
            m_tableCombo.add(famName);
        }
    } catch (Exception ex) {
        logError(// $NON-NLS-1$
        BaseMessages.getString(PKG, "CassandraOutputDialog.Error.ProblemGettingSchemaInfo.Message") + ":\n\n" + ex.getMessage(), // $NON-NLS-1$
        ex);
        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" + ex.getMessage(), // $NON-NLS-1$
        ex);
    } 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) Connection(org.pentaho.cassandra.spi.Connection) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) KettleException(org.pentaho.di.core.exception.KettleException)

Example 5 with Keyspace

use of org.pentaho.cassandra.spi.Keyspace 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)

Aggregations

Connection (org.pentaho.cassandra.spi.Connection)5 Keyspace (org.pentaho.cassandra.spi.Keyspace)5 HashMap (java.util.HashMap)4 KettleException (org.pentaho.di.core.exception.KettleException)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