Search in sources :

Example 1 with DeleteSelection

use of com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection in project rocketmq-externals by apache.

the class Updater method deleteRow.

private boolean deleteRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    DeleteSelection deleteSelection = QueryBuilder.deleteFrom(dbName, tableName);
    Delete delete = null;
    int count = 0;
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[1];
        if (count == 1) {
            delete = deleteSelection.whereColumn(fieldName).isEqualTo(buildTerm(fieldType, fieldValue));
        } else {
            delete = delete.whereColumn(fieldName).isEqualTo(buildTerm(fieldType, fieldValue));
        }
    }
    boolean finishDelete = false;
    SimpleStatement stmt = delete.build();
    try {
        while (!cqlSession.isClosed() && !finishDelete) {
            ResultSet result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                log.info("delete from table success, executed query {}", delete);
                return true;
            }
            finishDelete = true;
        }
    } catch (Exception e) {
        log.error("delete from table error,{}", e);
    }
    return false;
}
Also used : Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) FieldType(io.openmessaging.connector.api.data.FieldType) Field(io.openmessaging.connector.api.data.Field) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Map(java.util.Map)

Aggregations

ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)1 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)1 Field (io.openmessaging.connector.api.data.Field)1 FieldType (io.openmessaging.connector.api.data.FieldType)1 Map (java.util.Map)1