use of com.datastax.driver.core.querybuilder.Update in project camel by apache.
the class CassandraComponentProducerTest method testRequestMessageStatement.
/**
* Test with incoming message containing a header with RegularStatement.
*/
@Test
public void testRequestMessageStatement() throws Exception {
if (!canTest()) {
return;
}
Update.Where update = update("camel_user").with(set("first_name", bindMarker())).and(set("last_name", bindMarker())).where(eq("login", bindMarker()));
Object response = producerTemplate.requestBodyAndHeader(new Object[] { "Claus 2", "Ibsen 2", "c_ibsen" }, CassandraConstants.CQL_QUERY, update);
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Claus 2", row.getString("first_name"));
assertEquals("Ibsen 2", row.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.querybuilder.Update in project camel by apache.
the class CassandraComponentProducerUnpreparedTest method testRequestMessageStatement.
/**
* Test with incoming message containing a header with RegularStatement.
*/
@Test
public void testRequestMessageStatement() throws Exception {
Update.Where update = update("camel_user").with(set("first_name", "Claus 2")).and(set("last_name", "Ibsen 2")).where(eq("login", "c_ibsen"));
Object response = producerTemplate.requestBodyAndHeader(null, CassandraConstants.CQL_QUERY, update);
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Claus 2", row.getString("first_name"));
assertEquals("Ibsen 2", row.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.querybuilder.Update in project YCSB by brianfrankcooper.
the class CassandraCQLClient method update.
/**
* Update a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key, overwriting any existing values with the same field name.
*
* @param table
* The name of the table
* @param key
* The record key of the record to write.
* @param values
* A HashMap of field/value pairs to update in the record
* @return Zero on success, a non-zero error code on error
*/
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
try {
Set<String> fields = values.keySet();
PreparedStatement stmt = updateStmts.get(fields);
// Prepare statement on demand
if (stmt == null) {
Update updateStmt = QueryBuilder.update(table);
// Add fields
for (String field : fields) {
updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker()));
}
// Add key
updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker()));
stmt = session.prepare(updateStmt);
stmt.setConsistencyLevel(writeConsistencyLevel);
if (trace) {
stmt.enableTracing();
}
PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt);
if (prevStmt != null) {
stmt = prevStmt;
}
}
if (logger.isDebugEnabled()) {
logger.debug(stmt.getQueryString());
logger.debug("key = {}", key);
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
logger.debug("{} = {}", entry.getKey(), entry.getValue());
}
}
// Add fields
ColumnDefinitions vars = stmt.getVariables();
BoundStatement boundStmt = stmt.bind();
for (int i = 0; i < vars.size() - 1; i++) {
boundStmt.setString(i, values.get(vars.getName(i)).toString());
}
// Add key
boundStmt.setString(vars.size() - 1, key);
session.execute(boundStmt);
return Status.OK;
} catch (Exception e) {
logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e);
}
return Status.ERROR;
}
use of com.datastax.driver.core.querybuilder.Update in project gora by apache.
the class CassandraQueryFactory method getUpdateByQueryForNative.
/**
* This method returns the CQL Query for UpdateByQuery method
* refer : http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlUpdate.html
*
* @param mapping Cassandra mapping {@link CassandraMapping}
* @param cassandraQuery Cassandra Query {@link CassandraQuery}
* @param objects field Objects list
* @return CQL Query
*/
static String getUpdateByQueryForNative(CassandraMapping mapping, Query cassandraQuery, List<Object> objects) {
Update update = QueryBuilder.update(mapping.getKeySpace().getName(), mapping.getCoreName());
Update.Assignments updateAssignments = null;
if (cassandraQuery instanceof CassandraQuery) {
String[] columnNames = getColumnNames(mapping, Arrays.asList(cassandraQuery.getFields()));
for (String column : columnNames) {
updateAssignments = update.with(QueryBuilder.set(column, "?"));
objects.add(((CassandraQuery) cassandraQuery).getUpdateFieldValue(mapping.getFieldFromColumnName(column).getFieldName()));
}
} else {
throw new RuntimeException("Please use Cassandra Query object to invoke, UpdateByQuery method.");
}
return processQuery(cassandraQuery, updateAssignments, mapping, objects);
}
use of com.datastax.driver.core.querybuilder.Update in project gora by apache.
the class CassandraQueryFactory method getUpdateByQueryForAvro.
/**
* This method returns the CQL Query for UpdateByQuery method
* refer : http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlUpdate.html
*
* @param mapping Cassandra mapping {@link CassandraMapping}
* @param cassandraQuery Cassandra Query {@link CassandraQuery}
* @param objects field Objects list
* @return CQL Query
*/
static String getUpdateByQueryForAvro(CassandraMapping mapping, Query cassandraQuery, List<Object> objects, Schema schema) {
Update update = QueryBuilder.update(mapping.getKeySpace().getName(), mapping.getCoreName());
Update.Assignments updateAssignments = null;
if (cassandraQuery instanceof CassandraQuery) {
String[] columnNames = getColumnNames(mapping, Arrays.asList(cassandraQuery.getFields()));
for (String column : columnNames) {
updateAssignments = update.with(QueryBuilder.set(column, "?"));
Field field = mapping.getFieldFromColumnName(column);
Object value = ((CassandraQuery) cassandraQuery).getUpdateFieldValue(field.getFieldName());
try {
Schema schemaField = schema.getField(field.getFieldName()).schema();
objects.add(AvroCassandraUtils.getFieldValueFromAvroBean(schemaField, schemaField.getType(), value, field));
} catch (NullPointerException e) {
throw new RuntimeException(field + " field couldn't find in the class " + mapping.getPersistentClass() + ".");
}
}
} else {
throw new RuntimeException("Please use Cassandra Query object to invoke, UpdateByQuery method.");
}
return processQuery(cassandraQuery, updateAssignments, mapping, objects);
}
Aggregations