Search in sources :

Example 1 with Selector

use of org.pentaho.cassandra.util.Selector 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)

Aggregations

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