Search in sources :

Example 1 with InsertInto

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

the class Updater method updateRow.

/**
 * Since we have no way of getting the id of a record, and we cannot get the primary key list of a table,
 * even we can it is not extensible. So we the result sql sentense would be like
 * UPDATE dbName.tableName SET afterUpdateValues WHERE beforeUpdateValues.
 */
private Boolean updateRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    log.info("Updater.updateRow() get called ");
    int count = 0;
    InsertInto insert = QueryBuilder.insertInto(dbName, tableName);
    RegularInsert regularInsert = null;
    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) {
            regularInsert = insert.value(fieldName, buildTerm(fieldType, fieldValue));
        } else {
            regularInsert = regularInsert.value(fieldName, buildTerm(fieldType, fieldValue));
        }
    }
    SimpleStatement stmt;
    boolean finishUpdate = false;
    log.info("trying to execute sql query,{}", regularInsert.asCql());
    try {
        while (!cqlSession.isClosed() && !finishUpdate) {
            stmt = regularInsert.build();
            ResultSet result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                log.info("update table success, executed cql query {}", regularInsert.asCql());
                return true;
            }
            finishUpdate = true;
        }
    } catch (Exception e) {
        log.error("update table error,{}", e);
    }
    return false;
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) FieldType(io.openmessaging.connector.api.data.FieldType) Field(io.openmessaging.connector.api.data.Field) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) 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 InsertInto (com.datastax.oss.driver.api.querybuilder.insert.InsertInto)1 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)1 Field (io.openmessaging.connector.api.data.Field)1 FieldType (io.openmessaging.connector.api.data.FieldType)1 Map (java.util.Map)1