use of org.apache.gora.util.GoraException in project gora by apache.
the class AvroSerializer method get.
/**
* {@inheritDoc}
*
* @param key
* @return
*/
@Override
public Persistent get(Object key) throws GoraException {
try {
ArrayList<String> cassandraKeys = new ArrayList<>();
ArrayList<Object> cassandraValues = new ArrayList<>();
AvroCassandraUtils.processKeys(mapping, key, cassandraKeys, cassandraValues);
String cqlQuery = CassandraQueryFactory.getSelectObjectQuery(mapping, cassandraKeys);
SimpleStatement statement = new SimpleStatement(cqlQuery, cassandraValues.toArray());
if (readConsistencyLevel != null) {
statement.setConsistencyLevel(ConsistencyLevel.valueOf(readConsistencyLevel));
}
ResultSet resultSet = client.getSession().execute(statement);
Iterator<Row> iterator = resultSet.iterator();
ColumnDefinitions definitions = resultSet.getColumnDefinitions();
T obj = null;
if (iterator.hasNext()) {
obj = cassandraDataStore.newPersistent();
AbstractGettableData row = (AbstractGettableData) iterator.next();
populateValuesToPersistent(row, definitions, obj, mapping.getFieldNames());
}
return obj;
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class CassandraSerializer method deleteByQuery.
public long deleteByQuery(Query query) throws GoraException {
try {
List<Object> objectArrayList = new ArrayList<>();
if (query.getKey() == null && query.getEndKey() == null && query.getStartKey() == null) {
if (query.getFields() == null) {
client.getSession().execute(CassandraQueryFactory.getTruncateTableQuery(mapping));
} else {
LOG.error("Delete by Query is not supported for the Queries which didn't specify Query keys with fields.");
}
} else {
String cqlQuery = CassandraQueryFactory.getDeleteByQuery(mapping, query, objectArrayList);
ResultSet results;
SimpleStatement statement;
if (objectArrayList.size() == 0) {
statement = new SimpleStatement(cqlQuery);
} else {
statement = new SimpleStatement(cqlQuery, objectArrayList.toArray());
}
if (writeConsistencyLevel != null) {
statement.setConsistencyLevel(ConsistencyLevel.valueOf(writeConsistencyLevel));
}
results = client.getSession().execute(statement);
LOG.debug("Delete by Query was applied : " + results.wasApplied());
}
LOG.info("Delete By Query method doesn't return the deleted element count.");
return 0;
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class CassandraSerializer method createSchema.
public void createSchema() throws GoraException {
try {
LOG.debug("creating Cassandra keyspace {}", mapping.getKeySpace().getName());
this.client.getSession().execute(CassandraQueryFactory.getCreateKeySpaceQuery(mapping));
for (Map.Entry udtType : userDefineTypeMaps.entrySet()) {
LOG.debug("creating Cassandra User Define Type {}", udtType.getKey());
this.client.getSession().execute((String) udtType.getValue());
}
LOG.debug("creating Cassandra column family / table {}", mapping.getCoreName());
this.client.getSession().execute(CassandraQueryFactory.getCreateTableQuery(mapping));
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class CassandraStore method initialize.
/**
* In initializing the cassandra datastore, read the mapping file, creates the basic connection to cassandra cluster,
* according to the gora properties
*
* @param keyClass key class
* @param persistentClass persistent class
* @param properties properties
*/
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
LOG.debug("Initializing Cassandra store");
String serializationType;
try {
this.keyClass = keyClass;
this.persistentClass = persistentClass;
if (this.beanFactory == null) {
this.beanFactory = new BeanFactoryImpl<>(keyClass, persistentClass);
}
String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE);
serializationType = properties.getProperty(CassandraStoreParameters.CASSANDRA_SERIALIZATION_TYPE);
CassandraMappingBuilder mappingBuilder = new CassandraMappingBuilder(this);
mapping = mappingBuilder.readMapping(mappingFile);
CassandraClient cassandraClient = new CassandraClient();
cassandraClient.initialize(properties, mapping);
cassandraSerializer = CassandraSerializer.getSerializer(cassandraClient, serializationType, this, mapping);
} catch (GoraException e) {
throw e;
} catch (Exception e) {
throw new GoraException("Error while initializing Cassandra store: " + e.getMessage(), e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class CassandraStore method getPartitions.
/**
* {@inheritDoc}
*/
@Override
public List<PartitionQuery<K, T>> getPartitions(Query<K, T> query) throws GoraException {
try {
List<PartitionQuery<K, T>> partitions = new ArrayList<>();
PartitionWSQueryImpl<K, T> pqi = new PartitionWSQueryImpl<>(query);
pqi.setDataStore(this);
partitions.add(pqi);
return partitions;
} catch (Exception e) {
throw new GoraException(e);
}
}
Aggregations