Search in sources :

Example 61 with ResultSet

use of com.datastax.driver.core.ResultSet in project ats-framework by Axway.

the class CassandraDbProvider method getColumnInfo.

/**
     * Returns a map with column name as key and column date type as value.
     *
     * The value might be as simple as "Boolean" or more complex like
     *  - "Set|Boolean"
     *  - "List|String"
     *  - "Map|String|Integer"
     *  these are cases when the data type is a container of primitive data types.
     *
     * @param tableName
     * @return
     * @throws DbException
     */
public Map<String, String> getColumnInfo(String tableName) throws DbException {
    connect();
    ResultSet results = session.execute("SELECT * FROM " + this.dbName + "." + tableName + " LIMIT 1");
    Map<String, String> columnInfo = new HashMap<String, String>();
    for (Definition columnDefinition : results.getColumnDefinitions()) {
        DataType dataType = columnDefinition.getType();
        String dataTypeName = dataType.asJavaClass().getSimpleName();
        if ("Set".equalsIgnoreCase(dataTypeName)) {
            dataTypeName = dataTypeName + "|" + dataType.getTypeArguments().get(0);
        } else if ("List".equalsIgnoreCase(dataTypeName)) {
            dataTypeName = dataTypeName + "|" + dataType.getTypeArguments().get(0);
        } else if ("Map".equalsIgnoreCase(dataTypeName)) {
            dataTypeName = dataTypeName + "|" + dataType.getTypeArguments().get(0) + "|" + dataType.getTypeArguments().get(1);
        }
        columnInfo.put(columnDefinition.getName(), dataTypeName);
    }
    return columnInfo;
}
Also used : HashMap(java.util.HashMap) ResultSet(com.datastax.driver.core.ResultSet) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) DataType(com.datastax.driver.core.DataType)

Example 62 with ResultSet

use of com.datastax.driver.core.ResultSet in project ats-framework by Axway.

the class CassandraDbProvider method select.

@Override
public DbRecordValuesList[] select(DbQuery dbQuery, DbReturnModes dbReturnMode) throws DbException {
    connect();
    ArrayList<DbRecordValuesList> dbRecords = new ArrayList<DbRecordValuesList>();
    String sqlQuery = dbQuery.getQuery();
    if (allowFiltering) {
        sqlQuery += " ALLOW FILTERING";
    }
    if (log.isDebugEnabled()) {
        log.debug(sqlQuery);
    }
    ResultSet results = session.execute(sqlQuery);
    int currentRow = 0;
    Iterator<Row> it = results.iterator();
    while (it.hasNext()) {
        Row row = it.next();
        currentRow++;
        if (log.isDebugEnabled()) {
            log.debug("Result row number: " + currentRow);
        }
        DbRecordValuesList recordList = new DbRecordValuesList();
        for (Definition columnDefinition : row.getColumnDefinitions()) {
            DbColumn dbColumn = new DbColumn(columnDefinition.getTable(), columnDefinition.getName());
            dbColumn.setColumnType(columnDefinition.getType().getName().toString());
            Object value = extractObjectFromResultSet(row, columnDefinition);
            DbRecordValue recordValue = new DbRecordValue(dbColumn, value);
            recordList.add(recordValue);
        }
        dbRecords.add(recordList);
    }
    return dbRecords.toArray(new DbRecordValuesList[] {});
}
Also used : DbColumn(com.axway.ats.core.dbaccess.DbColumn) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) Row(com.datastax.driver.core.Row)

Example 63 with ResultSet

use of com.datastax.driver.core.ResultSet in project GNS by MobilityFirst.

the class CassandraRecords method retrieveJSONObject.

//  private String retrieveUserKeysString(String tableName, String guid) throws JSONException {
//    StringBuffer result = new StringBuffer();
//    String values = lookupEntireRecord(tableName, guid, NameRecord.USER_KEYS);
//    String prefix = "";
//    for (String key : JSONUtils.JSONArrayToArrayList(new JSONArray(values))) {
//      result.append(prefix);
//      result.append(CSI(key));
//      prefix = ",";
//    }
//    return result.toString();
//  }
private JSONObject retrieveJSONObject(String tableName, String guid) {
    CollectionSpec spec = getCollectionSpec(tableName);
    if (spec != null) {
        String query = "SELECT * FROM " + CSI(tableName) + " WHERE " + CSI(spec.getPrimaryKey()) + " = '" + guid + "';";
        DatabaseConfig.getLogger().finer("Executing query " + query);
        ResultSet results = session.execute(query);
        Row row = results.one();
        if (row != null) {
            JSONObject json = retrieveJSONObjectFromRow(row);
            DatabaseConfig.getLogger().finest(json.toString());
            return json;
        } else {
            return null;
        }
    } else {
        DatabaseConfig.getLogger().log(Level.SEVERE, "CASSANDRA DB: No table named: {0}", tableName);
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Example 64 with ResultSet

use of com.datastax.driver.core.ResultSet in project ignite by apache.

the class CassandraSessionImpl method execute.

/** {@inheritDoc} */
@Override
public <V> V execute(ExecutionAssistant<V> assistant) {
    int attempt = 0;
    Throwable error = null;
    String errorMsg = "Failed to execute Cassandra CQL statement: " + assistant.getStatement();
    RandomSleeper sleeper = newSleeper();
    incrementSessionRefs();
    try {
        while (attempt < CQL_EXECUTION_ATTEMPTS_COUNT) {
            error = null;
            if (attempt != 0) {
                log.warning("Trying " + (attempt + 1) + " attempt to execute Cassandra CQL statement: " + assistant.getStatement());
            }
            try {
                PreparedStatement preparedSt = prepareStatement(assistant.getTable(), assistant.getStatement(), assistant.getPersistenceSettings(), assistant.tableExistenceRequired());
                if (preparedSt == null)
                    return null;
                Statement statement = tuneStatementExecutionOptions(assistant.bindStatement(preparedSt));
                ResultSet res = session().execute(statement);
                Row row = res == null || !res.iterator().hasNext() ? null : res.iterator().next();
                return row == null ? null : assistant.process(row);
            } catch (Throwable e) {
                error = e;
                if (CassandraHelper.isTableAbsenceError(e)) {
                    if (!assistant.tableExistenceRequired()) {
                        log.warning(errorMsg, e);
                        return null;
                    }
                    handleTableAbsenceError(assistant.getTable(), assistant.getPersistenceSettings());
                } else if (CassandraHelper.isHostsAvailabilityError(e))
                    handleHostsAvailabilityError(e, attempt, errorMsg);
                else if (CassandraHelper.isPreparedStatementClusterError(e))
                    handlePreparedStatementClusterError(e);
                else
                    // For an error which we don't know how to handle, we will not try next attempts and terminate.
                    throw new IgniteException(errorMsg, e);
            }
            if (!CassandraHelper.isTableAbsenceError(error))
                sleeper.sleep();
            attempt++;
        }
    } catch (Throwable e) {
        error = e;
    } finally {
        decrementSessionRefs();
    }
    log.error(errorMsg, error);
    throw new IgniteException(errorMsg, error);
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) IgniteException(org.apache.ignite.IgniteException) ResultSet(com.datastax.driver.core.ResultSet) RandomSleeper(org.apache.ignite.cache.store.cassandra.common.RandomSleeper) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)64 Row (com.datastax.driver.core.Row)35 Test (org.junit.Test)25 BoundStatement (com.datastax.driver.core.BoundStatement)11 Session (com.datastax.driver.core.Session)10 ArrayList (java.util.ArrayList)9 Cluster (com.datastax.driver.core.Cluster)8 Statement (com.datastax.driver.core.Statement)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)5 List (java.util.List)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 BatchStatement (com.datastax.driver.core.BatchStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 Update (com.datastax.driver.core.querybuilder.Update)3 ImmutableList (com.google.common.collect.ImmutableList)3 IgniteException (org.apache.ignite.IgniteException)3 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2