DATACASS-32 - Pulled in existing work from Matthew Adams on the

CassandraTemplate
This commit is contained in:
dwebb
2013-11-19 16:24:01 -05:00
parent 444f49bed1
commit 42a40bb2b0
5 changed files with 205 additions and 0 deletions

View File

@@ -15,6 +15,10 @@
*/
package org.springframework.cassandra.core;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
@@ -36,6 +40,22 @@ public interface CassandraOperations {
RuntimeException potentiallyConvertRuntimeException(RuntimeException ex);
<T> T query(String cql, ResultSetExtractor<T> rse) throws DataAccessException;
void query(String cql, RowCallbackHandler rch) throws DataAccessException;
<T> List<T> query(String cql, RowMapper<T> rowMapper) throws DataAccessException;
<T> T queryForObject(String cql, RowMapper<T> rowMapper) throws DataAccessException;
<T> T queryForObject(String cql, Class<T> requiredType) throws DataAccessException;
Map<String, Object> queryForMap(String cql) throws DataAccessException;
<T> List<T> queryForList(String cql, Class<T> elementType) throws DataAccessException;
List<Map<String, Object>> queryForList(String cql) throws DataAccessException;
/**
* Execute query and return Cassandra ResultSet
*

View File

@@ -15,14 +15,23 @@
*/
package org.springframework.cassandra.core;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.cassandra.support.CassandraAccessor;
import org.springframework.dao.DataAccessException;
import org.springframework.data.cassandra.core.CassandraDataTemplate;
import org.springframework.util.Assert;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;
/**
* The CassandraTemplate is a Spring convenience wrapper for low level and explicit operations on the Cassandra
@@ -142,4 +151,149 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe
}
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.ResultSetExtractor)
*/
public <T> T query(String cql, ResultSetExtractor<T> rse) throws DataAccessException {
try {
ResultSet rs = getSession().execute(cql);
return rse.extractData(rs);
} catch (DriverException dx) {
throw getExceptionTranslator().translateExceptionIfPossible(dx);
}
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowCallbackHandler)
*/
public void query(String cql, RowCallbackHandler rch) throws DataAccessException {
try {
ResultSet rs = getSession().execute(cql);
for (Row row : rs.all()) {
rch.processRow(row);
}
} catch (DriverException dx) {
throw getExceptionTranslator().translateExceptionIfPossible(dx);
}
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowMapper)
*/
public <T> List<T> query(String cql, RowMapper<T> rowMapper) throws DataAccessException {
try {
ResultSet rs = getSession().execute(cql);
int i = 0;
List<T> mappedRows = new ArrayList<T>();
for (Row row : rs.all()) {
mappedRows.add(rowMapper.mapRow(row, i++));
}
return mappedRows;
} catch (DriverException dx) {
throw getExceptionTranslator().translateExceptionIfPossible(dx);
}
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#queryForObject(java.lang.String, org.springframework.cassandra.core.RowMapper)
*/
public <T> T queryForObject(String cql, RowMapper<T> rowMapper) throws DataAccessException {
try {
ResultSet rs = getSession().execute(cql);
List<Row> rows = rs.all();
Assert.notNull(rows, "null row list returned from query");
Assert.isTrue(rows.size() == 1, "row list has " + rows.size() + " rows instead of one");
return rowMapper.mapRow(rows.get(0), 0);
} catch (DriverException dx) {
throw getExceptionTranslator().translateExceptionIfPossible(dx);
}
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#queryForObject(java.lang.String, java.lang.Class)
*/
@SuppressWarnings("unchecked")
public <T> T queryForObject(String cql, Class<T> requiredType) throws DataAccessException {
ResultSet rs = getSession().execute(cql);
if (rs == null) {
return null;
}
Row row = rs.one();
if (row == null) {
return null;
}
return (T) firstColumnToObject(row);
}
/**
* @param row
* @return
*/
protected Object firstColumnToObject(Row row) {
ColumnDefinitions cols = row.getColumnDefinitions();
if (cols.size() == 0) {
return null;
}
return cols.getType(0).deserialize(row.getBytesUnsafe(0));
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#queryForMap(java.lang.String)
*/
public Map<String, Object> queryForMap(String cql) throws DataAccessException {
ResultSet rs = getSession().execute(cql);
if (rs == null) {
return null;
}
return toMap(rs.one());
}
/**
* @param row
* @return
*/
protected Map<String, Object> toMap(Row row) {
if (row == null) {
return null;
}
ColumnDefinitions cols = row.getColumnDefinitions();
Map<String, Object> map = new HashMap<String, Object>(cols.size());
for (Definition def : cols.asList()) {
String name = def.getName();
map.put(name, def.getType().deserialize(row.getBytesUnsafe(name)));
}
return map;
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#queryForList(java.lang.String, java.lang.Class)
*/
@SuppressWarnings("unchecked")
public <T> List<T> queryForList(String cql, Class<T> elementType) throws DataAccessException {
ResultSet rs = getSession().execute(cql);
List<Row> rows = rs.all();
List<T> list = new ArrayList<T>(rows.size());
for (Row row : rows) {
list.add((T) firstColumnToObject(row));
}
return list;
}
/* (non-Javadoc)
* @see org.springframework.cassandra.core.CassandraOperations#queryForList(java.lang.String)
*/
public List<Map<String, Object>> queryForList(String cql) throws DataAccessException {
ResultSet rs = getSession().execute(cql);
List<Row> rows = rs.all();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(rows.size());
for (Row row : rows) {
list.add(toMap(row));
}
return list;
}
}

View File

@@ -0,0 +1,11 @@
package org.springframework.cassandra.core;
import org.springframework.dao.DataAccessException;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.exceptions.DriverException;
public interface ResultSetExtractor<T> {
T extractData(ResultSet rs) throws DriverException, DataAccessException;
}

View File

@@ -0,0 +1,10 @@
package org.springframework.cassandra.core;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
public interface RowCallbackHandler {
void processRow(Row row) throws DriverException;
}

View File

@@ -0,0 +1,10 @@
package org.springframework.cassandra.core;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
public interface RowMapper<T> {
T mapRow(Row row, int rowNum) throws DriverException;
}