Search in sources :

Example 36 with GoraException

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);
    }
}
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 37 with GoraException

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);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SimpleStatement(com.datastax.driver.core.SimpleStatement) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) GoraException(org.apache.gora.util.GoraException)

Example 38 with GoraException

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);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) Map(java.util.Map) GoraException(org.apache.gora.util.GoraException)

Example 39 with GoraException

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);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException)

Example 40 with GoraException

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);
    }
}
Also used : PartitionWSQueryImpl(org.apache.gora.query.ws.impl.PartitionWSQueryImpl) GoraException(org.apache.gora.util.GoraException) ArrayList(java.util.ArrayList) PartitionQuery(org.apache.gora.query.PartitionQuery) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException)

Aggregations

GoraException (org.apache.gora.util.GoraException)174 IOException (java.io.IOException)119 ArrayList (java.util.ArrayList)30 Schema (org.apache.avro.Schema)25 SQLException (java.sql.SQLException)17 HashMap (java.util.HashMap)17 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 PreparedStatement (java.sql.PreparedStatement)11 Map (java.util.Map)11 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)9 SolrServerException (org.apache.solr.client.solrj.SolrServerException)9 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)8 AccumuloException (org.apache.accumulo.core.client.AccumuloException)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)8 Field (org.apache.avro.Schema.Field)8 KuduException (org.apache.kudu.client.KuduException)8 ResultSet (com.datastax.driver.core.ResultSet)7 SimpleStatement (com.datastax.driver.core.SimpleStatement)7 SAXBuilder (org.jdom.input.SAXBuilder)7 InputStream (java.io.InputStream)6