use of org.apache.gora.cassandra.query.CassandraQuery 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 org.apache.gora.cassandra.query.CassandraQuery 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);
}
use of org.apache.gora.cassandra.query.CassandraQuery in project gora by apache.
the class TestCassandraStoreWithNativeSerialization method testUpdateByQuery.
/**
* In this test case, update by quert method behavior of the data store is testing.
* @throws GoraException
*/
@Test
public void testUpdateByQuery() throws GoraException {
userDataStore.truncateSchema();
UUID id1 = UUID.randomUUID();
User user1 = new User(id1, "user1", Date.from(Instant.now()));
userDataStore.put(id1, user1);
UUID id2 = UUID.randomUUID();
User user2 = new User(id2, "user2", Date.from(Instant.now()));
userDataStore.put(id2, user2);
Query<UUID, User> query1 = userDataStore.newQuery();
if (query1 instanceof CassandraQuery) {
((CassandraQuery) query1).addUpdateField("name", "madhawa");
}
query1.setKey(id1);
if (userDataStore instanceof CassandraStore) {
userDataStore.updateByQuery(query1);
}
User user = userDataStore.get(id1);
Assert.assertEquals(user.getName(), "madhawa");
}
use of org.apache.gora.cassandra.query.CassandraQuery in project gora by apache.
the class TestCassandraStoreWithCassandraKey method testUpdateByQuery.
@Test
public void testUpdateByQuery() throws GoraException {
cassandraRecordDataStore.truncateSchema();
// insert data
CassandraRecord record1 = new CassandraRecord();
CassandraRecord record2 = new CassandraRecord();
CassandraRecord record3 = new CassandraRecord();
CassandraRecord record4 = new CassandraRecord();
record1.setDataLong(719411002L);
record1.setDataString(new Utf8("Madawa"));
record1.setDataInt(100);
record2.setDataLong(712778588L);
record2.setDataString(new Utf8("Kasun"));
record2.setDataInt(101);
record3.setDataLong(716069539L);
record3.setDataString(new Utf8("Charith"));
record3.setDataInt(102);
record4.setDataLong(112956051L);
record4.setDataString(new Utf8("Bhanuka"));
record4.setDataInt(103);
CassandraKey key1 = new CassandraKey();
key1.setTimestamp(200L);
key1.setUrl("www.apache.org");
CassandraKey key2 = new CassandraKey();
key2.setTimestamp(205L);
key2.setUrl("www.apache.org");
CassandraKey key3 = new CassandraKey();
key3.setTimestamp(210L);
key3.setUrl("www.apache.org");
CassandraKey key4 = new CassandraKey();
key4.setTimestamp(215L);
key4.setUrl("www.apache.org");
cassandraRecordDataStore.put(key1, record1);
cassandraRecordDataStore.put(key2, record2);
cassandraRecordDataStore.put(key3, record3);
cassandraRecordDataStore.put(key4, record4);
CassandraQuery<CassandraKey, CassandraRecord> query = new CassandraQuery<>(cassandraRecordDataStore);
query.setKey(key1);
query.addUpdateField("dataString", new Utf8("test123"));
cassandraRecordDataStore.updateByQuery(query);
CassandraRecord result = cassandraRecordDataStore.get(key1);
Assert.assertEquals(new Utf8("test123"), result.getDataString());
}
Aggregations