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();
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations