Search in sources :

Example 16 with SimpleStatement

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

the class AvroSerializer method updateByQuery.

/**
 * {@inheritDoc}
 *
 * @param query
 * @return
 */
@Override
public boolean updateByQuery(Query query) {
    List<Object> objectArrayList = new ArrayList<>();
    String cqlQuery = CassandraQueryFactory.getUpdateByQueryForAvro(mapping, query, objectArrayList, persistentSchema);
    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);
    return results.wasApplied();
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet)

Example 17 with SimpleStatement

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

the class AvroSerializer method put.

/**
 * {@inheritDoc}
 *
 * @param key
 * @param persistent
 */
@Override
public void put(Object key, Persistent persistent) throws GoraException {
    try {
        if (persistent instanceof PersistentBase) {
            if (persistent.isDirty()) {
                PersistentBase persistentBase = (PersistentBase) persistent;
                ArrayList<String> fields = new ArrayList<>();
                ArrayList<Object> values = new ArrayList<>();
                AvroCassandraUtils.processKeys(mapping, key, fields, values);
                for (Schema.Field f : persistentBase.getSchema().getFields()) {
                    String fieldName = f.name();
                    Field field = mapping.getFieldFromFieldName(fieldName);
                    if (field == null) {
                        LOG.debug("Ignoring {} adding field, {} field can't find in {} mapping", new Object[] { fieldName, fieldName, persistentClass });
                        continue;
                    }
                    if (persistent.isDirty(f.pos()) || mapping.getInlinedDefinedPartitionKey().equals(mapping.getFieldFromFieldName(fieldName))) {
                        Object value = persistentBase.get(f.pos());
                        String fieldType = field.getType();
                        if (fieldType.contains("frozen")) {
                            fieldType = fieldType.substring(fieldType.indexOf("<") + 1, fieldType.indexOf(">"));
                            UserType userType = client.getSession().getCluster().getMetadata().getKeyspace(mapping.getKeySpace().getName()).getUserType(fieldType);
                            UDTValue udtValue = userType.newValue();
                            Schema udtSchema = f.schema();
                            if (udtSchema.getType().equals(Schema.Type.UNION)) {
                                for (Schema schema : udtSchema.getTypes()) {
                                    if (schema.getType().equals(Schema.Type.RECORD)) {
                                        udtSchema = schema;
                                        break;
                                    }
                                }
                            }
                            PersistentBase udtObjectBase = (PersistentBase) value;
                            for (Schema.Field udtField : udtSchema.getFields()) {
                                Object udtFieldValue = AvroCassandraUtils.getFieldValueFromAvroBean(udtField.schema(), udtField.schema().getType(), udtObjectBase.get(udtField.name()), field);
                                if (udtField.schema().getType().equals(Schema.Type.MAP)) {
                                    udtValue.setMap(udtField.name(), (Map) udtFieldValue);
                                } else if (udtField.schema().getType().equals(Schema.Type.ARRAY)) {
                                    udtValue.setList(udtField.name(), (List) udtFieldValue);
                                } else {
                                    udtValue.set(udtField.name(), udtFieldValue, (Class) udtFieldValue.getClass());
                                }
                            }
                            value = udtValue;
                        } else {
                            value = AvroCassandraUtils.getFieldValueFromAvroBean(f.schema(), f.schema().getType(), value, field);
                        }
                        values.add(value);
                        fields.add(fieldName);
                    }
                }
                String cqlQuery = CassandraQueryFactory.getInsertDataQuery(mapping, fields);
                SimpleStatement statement = new SimpleStatement(cqlQuery, values.toArray());
                if (writeConsistencyLevel != null) {
                    statement.setConsistencyLevel(ConsistencyLevel.valueOf(writeConsistencyLevel));
                }
                client.getSession().execute(statement);
            } else {
                LOG.info("Ignored putting persistent bean {} in the store as it is neither " + "new, neither dirty.", new Object[] { persistent });
            }
        } else {
            LOG.error("{} Persistent bean isn't extended by {} .", new Object[] { this.persistentClass, PersistentBase.class });
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : UDTValue(com.datastax.driver.core.UDTValue) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) SimpleStatement(com.datastax.driver.core.SimpleStatement) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) GoraException(org.apache.gora.util.GoraException) Field(org.apache.gora.cassandra.bean.Field) GoraException(org.apache.gora.util.GoraException) ArrayList(java.util.ArrayList) List(java.util.List) UserType(com.datastax.driver.core.UserType)

Example 18 with SimpleStatement

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

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

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

the class SimpleCQLStatementMapper method map.

/**
     * {@inheritDoc}.
     */
@Override
public List<Statement> map(Map conf, Session session, ITuple tuple) {
    List<Column> columns = mapper.map(tuple);
    SimpleStatement statement = new SimpleStatement(queryString, Column.getVals(columns));
    if (hasRoutingKeys()) {
        List<ByteBuffer> keys = rkGenerator.getRoutingKeys(tuple);
        if (keys.size() == 1)
            statement.setRoutingKey(keys.get(0));
        else
            statement.setRoutingKey(keys.toArray(new ByteBuffer[keys.size()]));
    }
    return Arrays.asList((Statement) statement);
}
Also used : Column(org.apache.storm.cassandra.query.Column) SimpleStatement(com.datastax.driver.core.SimpleStatement) ByteBuffer(java.nio.ByteBuffer)

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