Search in sources :

Example 21 with SimpleStatement

use of com.datastax.driver.core.SimpleStatement in project ignite by apache.

the class IgnitePersistentStoreTest method loadCacheTest.

/**
 */
@Test
public void loadCacheTest() {
    Ignition.stopAll(true);
    LOGGER.info("Running loadCache test");
    LOGGER.info("Filling Cassandra table with test data");
    CacheStore store = CacheStoreHelper.createCacheStore("personTypes", new ClassPathResource("org/apache/ignite/tests/persistence/pojo/persistence-settings-3.xml"), CassandraHelper.getAdminDataSrc());
    Collection<CacheEntryImpl<PersonId, Person>> entries = TestsHelper.generatePersonIdsPersonsEntries();
    // noinspection unchecked
    store.writeAll(entries);
    LOGGER.info("Cassandra table filled with test data");
    LOGGER.info("Running loadCache test");
    try (Ignite ignite = Ignition.start("org/apache/ignite/tests/persistence/pojo/ignite-config.xml")) {
        CacheConfiguration<PersonId, Person> ccfg = new CacheConfiguration<>("cache3");
        IgniteCache<PersonId, Person> personCache3 = ignite.getOrCreateCache(ccfg);
        int size = personCache3.size(CachePeekMode.ALL);
        LOGGER.info("Initial cache size " + size);
        LOGGER.info("Loading cache data from Cassandra table");
        String qry = "select * from test1.pojo_test3 limit 3";
        personCache3.loadCache(null, qry);
        size = personCache3.size(CachePeekMode.ALL);
        Assert.assertEquals("Cache data was incorrectly loaded from Cassandra table by '" + qry + "'", 3, size);
        personCache3.clear();
        personCache3.loadCache(null, new SimpleStatement(qry));
        size = personCache3.size(CachePeekMode.ALL);
        Assert.assertEquals("Cache data was incorrectly loaded from Cassandra table by statement", 3, size);
        personCache3.clear();
        personCache3.loadCache(null);
        size = personCache3.size(CachePeekMode.ALL);
        Assert.assertEquals("Cache data was incorrectly loaded from Cassandra. " + "Expected number of records is " + TestsHelper.getBulkOperationSize() + ", but loaded number of records is " + size, TestsHelper.getBulkOperationSize(), size);
        LOGGER.info("Cache data loaded from Cassandra table");
    }
    LOGGER.info("loadCache test passed");
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) ClassPathResource(org.springframework.core.io.ClassPathResource) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) SimplePersonId(org.apache.ignite.tests.pojos.SimplePersonId) PersonId(org.apache.ignite.tests.pojos.PersonId) Ignite(org.apache.ignite.Ignite) CacheStore(org.apache.ignite.cache.store.CacheStore) SimplePerson(org.apache.ignite.tests.pojos.SimplePerson) Person(org.apache.ignite.tests.pojos.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 22 with SimpleStatement

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

the class AvroSerializer method delete.

/**
 * {@inheritDoc}
 *
 * @param key
 * @return
 */
@Override
public boolean delete(Object key) throws GoraException {
    ArrayList<String> cassandraKeys = new ArrayList<>();
    ArrayList<Object> cassandraValues = new ArrayList<>();
    try {
        AvroCassandraUtils.processKeys(mapping, key, cassandraKeys, cassandraValues);
        String cqlQuery = CassandraQueryFactory.getDeleteDataQuery(mapping, cassandraKeys);
        SimpleStatement statement = new SimpleStatement(cqlQuery, cassandraValues.toArray());
        if (writeConsistencyLevel != null) {
            statement.setConsistencyLevel(ConsistencyLevel.valueOf(writeConsistencyLevel));
        }
        ResultSet resultSet = client.getSession().execute(statement);
        return resultSet.wasApplied();
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SimpleStatement(com.datastax.driver.core.SimpleStatement) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet) GoraException(org.apache.gora.util.GoraException)

Example 23 with SimpleStatement

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

the class CassandraSerializer method exists.

/**
 * Check if key exists
 *
 * @param key key value
 * @return true/false
 */
public boolean exists(Object key) throws GoraException {
    try {
        ArrayList<String> cassandraKeys = new ArrayList<>();
        ArrayList<Object> cassandraValues = new ArrayList<>();
        AvroCassandraUtils.processKeys(mapping, key, cassandraKeys, cassandraValues);
        String cqlQuery = CassandraQueryFactory.getCheckExistsQuery(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();
        Row next = iterator.next();
        long count = next.getLong(0);
        return count != 0;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SimpleStatement(com.datastax.driver.core.SimpleStatement) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) GoraException(org.apache.gora.util.GoraException)

Example 24 with SimpleStatement

use of com.datastax.driver.core.SimpleStatement 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 25 with SimpleStatement

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

SimpleStatement (com.datastax.driver.core.SimpleStatement)38 ResultSet (com.datastax.driver.core.ResultSet)17 Row (com.datastax.driver.core.Row)17 Test (org.junit.Test)15 Session (com.datastax.driver.core.Session)13 Statement (com.datastax.driver.core.Statement)10 ArrayList (java.util.ArrayList)10 CassandraResultSet (org.apache.gora.cassandra.query.CassandraResultSet)7 GoraException (org.apache.gora.util.GoraException)7 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)6 ByteBuffer (java.nio.ByteBuffer)5 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)4 AbstractGettableData (com.datastax.driver.core.AbstractGettableData)3 BatchStatement (com.datastax.driver.core.BatchStatement)2 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)2 DriverException (com.datastax.driver.core.exceptions.DriverException)2 UnavailableException (com.datastax.driver.core.exceptions.UnavailableException)2 CQLStatement (org.apache.cassandra.cql3.CQLStatement)2 BatchStatement (org.apache.cassandra.cql3.statements.BatchStatement)2 ICluster (org.apache.cassandra.distributed.api.ICluster)2