use of org.apache.gora.cassandra.query.CassandraResultSet in project gora by apache.
the class NativeSerializer method execute.
/**
* {@inheritDoc}
*
* @param dataStore
* @param query
* @return
*/
@Override
public org.apache.gora.query.Result execute(DataStore dataStore, Query query) throws GoraException {
try {
List<Object> objectArrayList = new ArrayList<>();
String[] fields = query.getFields();
if (fields != null) {
fields = (String[]) ArrayUtils.addAll(fields, mapping.getAllKeys());
} else {
fields = mapping.getAllFieldsIncludingKeys();
}
CassandraResultSet<K, T> cassandraResult = new CassandraResultSet<>(dataStore, query);
String cqlQuery = CassandraQueryFactory.getExecuteQuery(mapping, query, objectArrayList, fields);
ResultSet results;
if (objectArrayList.size() == 0) {
results = client.getSession().execute(cqlQuery);
} else {
results = client.getSession().execute(cqlQuery, objectArrayList.toArray());
}
Result<T> objects = mapper.map(results);
Iterator iterator = objects.iterator();
while (iterator.hasNext()) {
T result = (T) iterator.next();
K key = getKey(result);
cassandraResult.addResultElement(key, result);
}
return cassandraResult;
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.cassandra.query.CassandraResultSet in project gora by apache.
the class AvroSerializer method execute.
/**
* {@inheritDoc}
*
* @param dataStore
* @param query
* @return
*/
@Override
public Result execute(DataStore dataStore, Query query) throws GoraException {
try {
List<Object> objectArrayList = new ArrayList<>();
String[] fields = query.getFields();
if (fields != null) {
fields = (String[]) ArrayUtils.addAll(fields, mapping.getAllKeys());
} else {
fields = mapping.getAllFieldsIncludingKeys();
}
CassandraResultSet<K, T> cassandraResult = new CassandraResultSet<>(dataStore, query);
String cqlQuery = CassandraQueryFactory.getExecuteQuery(mapping, query, objectArrayList, fields);
ResultSet results;
SimpleStatement statement;
if (objectArrayList.size() == 0) {
statement = new SimpleStatement(cqlQuery);
} else {
statement = new SimpleStatement(cqlQuery, objectArrayList.toArray());
}
if (readConsistencyLevel != null) {
statement.setConsistencyLevel(ConsistencyLevel.valueOf(readConsistencyLevel));
}
results = client.getSession().execute(statement);
Iterator<Row> iterator = results.iterator();
ColumnDefinitions definitions = results.getColumnDefinitions();
T obj;
K keyObject;
CassandraKey cassandraKey = mapping.getCassandraKey();
while (iterator.hasNext()) {
AbstractGettableData row = (AbstractGettableData) iterator.next();
obj = cassandraDataStore.newPersistent();
keyObject = cassandraDataStore.newKey();
populateValuesToPersistent(row, definitions, obj, fields);
if (cassandraKey != null) {
populateValuesToPersistent(row, definitions, (PersistentBase) keyObject, cassandraKey.getFieldNames());
} else {
Field key = mapping.getInlinedDefinedPartitionKey();
keyObject = (K) getValue(row, definitions.getType(key.getColumnName()), key.getColumnName(), null);
}
cassandraResult.addResultElement(keyObject, obj);
}
return cassandraResult;
} catch (Exception e) {
throw new GoraException(e);
}
}
Aggregations