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;
}
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[] {});
}
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;
}
}
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);
}
Aggregations