Search in sources :

Example 1 with ITableMetaData

use of org.pentaho.cassandra.spi.ITableMetaData 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 2 with ITableMetaData

use of org.pentaho.cassandra.spi.ITableMetaData 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 3 with ITableMetaData

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

the class DriverKeyspace method updateTableCQL3.

/**
 * Actually an ALTER to add columns, not UPDATE. Purpose of keyIndexes yet to be determined
 */
@Override
public void updateTableCQL3(String tableName, RowMetaInterface rowMeta, List<Integer> keyIndexes, LogChannelInterface log) throws Exception {
    Session session = getSession();
    ITableMetaData table = getTableMetaData(tableName);
    for (ValueMetaInterface valueMeta : rowMeta.getValueMetaList()) {
        if (!table.columnExistsInSchema(valueMeta.getName())) {
            session.execute(SchemaBuilder.alterTable(tableName).alterColumn(valueMeta.getName()).type(getDataType(valueMeta)));
        }
    }
}
Also used : ITableMetaData(org.pentaho.cassandra.spi.ITableMetaData) Session(com.datastax.driver.core.Session) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

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