Search in sources :

Example 6 with CassandraKey

use of org.apache.gora.cassandra.bean.CassandraKey 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);
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) AbstractGettableData(com.datastax.driver.core.AbstractGettableData) SimpleStatement(com.datastax.driver.core.SimpleStatement) ArrayList(java.util.ArrayList) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey) GoraException(org.apache.gora.util.GoraException) Field(org.apache.gora.cassandra.bean.Field) GoraException(org.apache.gora.util.GoraException) ResultSet(com.datastax.driver.core.ResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet) Row(com.datastax.driver.core.Row)

Aggregations

CassandraKey (org.apache.gora.cassandra.bean.CassandraKey)6 Field (org.apache.gora.cassandra.bean.Field)5 ClusterKeyField (org.apache.gora.cassandra.bean.ClusterKeyField)4 PartitionKeyField (org.apache.gora.cassandra.bean.PartitionKeyField)4 ArrayList (java.util.ArrayList)3 AbstractGettableData (com.datastax.driver.core.AbstractGettableData)1 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)1 ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1 Select (com.datastax.driver.core.querybuilder.Select)1 List (java.util.List)1 Schema (org.apache.avro.Schema)1 CassandraResultSet (org.apache.gora.cassandra.query.CassandraResultSet)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1 GoraException (org.apache.gora.util.GoraException)1 Attribute (org.jdom.Attribute)1 Element (org.jdom.Element)1