Search in sources :

Example 1 with AbstractGettableData

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

Example 2 with AbstractGettableData

use of com.datastax.driver.core.AbstractGettableData 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)

Example 3 with AbstractGettableData

use of com.datastax.driver.core.AbstractGettableData in project gora by apache.

the class AvroSerializer method get.

/**
 * {@inheritDoc}
 *
 * @param key
 * @param fields
 * @return
 */
@Override
public Persistent get(Object key, String[] fields) throws GoraException {
    try {
        if (fields == null) {
            fields = getFields();
        }
        ArrayList<String> cassandraKeys = new ArrayList<>();
        ArrayList<Object> cassandraValues = new ArrayList<>();
        AvroCassandraUtils.processKeys(mapping, key, cassandraKeys, cassandraValues);
        String cqlQuery = CassandraQueryFactory.getSelectObjectWithFieldsQuery(mapping, fields, cassandraKeys);
        SimpleStatement statement = new SimpleStatement(cqlQuery, cassandraValues.toArray());
        if (readConsistencyLevel != null) {
            statement.setConsistencyLevel(ConsistencyLevel.valueOf(readConsistencyLevel));
        }
        ResultSet resultSet = this.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, fields);
        }
        return obj;
    } catch (GoraException e) {
        throw e;
    } 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) GoraException(org.apache.gora.util.GoraException) GoraException(org.apache.gora.util.GoraException) ResultSet(com.datastax.driver.core.ResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet) Row(com.datastax.driver.core.Row)

Aggregations

AbstractGettableData (com.datastax.driver.core.AbstractGettableData)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)3 ResultSet (com.datastax.driver.core.ResultSet)3 Row (com.datastax.driver.core.Row)3 SimpleStatement (com.datastax.driver.core.SimpleStatement)3 ArrayList (java.util.ArrayList)3 CassandraResultSet (org.apache.gora.cassandra.query.CassandraResultSet)3 GoraException (org.apache.gora.util.GoraException)3 CassandraKey (org.apache.gora.cassandra.bean.CassandraKey)1 Field (org.apache.gora.cassandra.bean.Field)1