sunny
sunny

Reputation: 33

cassandra insert and update

my cassandra database has a columns called "createdClientId" and "updateClientId". my model class has a fields called id(primary_key), createdClientId and updateClientId. My request is a post request which has a client-id as a header and that client id needs to be value of columns "createdClientId" and "updateClientId" at the time of insertion and while updating it should update only "updateClientId"

my dao looks like this

public void getdata(Model model, String clientId) {
        model.setcreatedClientId(clientId);
        model.setupdateClientId(clientId);
        String insert = SimpleJson.writeValueAsString(model);
        // insert query
String query = PropertyManager.getProperty("prop") + insert + "' IF NOT EXISTS;";
ResultSet rs = cassandraTemplate.getCqlOperations().queryForResultSet(query);
        if (rs != null) {
            LOGGER.log(DEBUG, "Result set is not null");
            if (rs.wasApplied()) {
                response.setMessage(RESPONSE_MESSAGE + " Created successfully");
            } else if (!rs.wasApplied()) {
                model.setUpdatedBy(clientId);
                // update query
                query = PropertyManager.getProperty("prop") + insert + "'";
                cassandraTemplate.getCqlOperations().queryForResultSet(query);
                response.setMessage(RESPONSE_MESSAGE + " Updated successfully");
            }
        }
    }

now when i am inserting data it works fine(ie both createdClientId & updateClientId fields are saving with same values of client-id) but when i update it should only update updateClientId instead it is updating the createdClientId also which it should not.

Upvotes: 3

Views: 606

Answers (1)

Mohamed Ibrahim Elsayed
Mohamed Ibrahim Elsayed

Reputation: 2964

The reason why createdClientId is also updated every time is that this line
model.setupdateClientId(clientId); is executed every time, then you create insert string
String insert = SimpleJson.writeValueAsString(model); and using this string in both cases, so createdClientId get updated.
One solution is to separate your data into two separate Cassandra tables: one that persists createdClientId piece of data for only one time and another table to support your frequent updates that holds updatedClientId (both with clientId as primary key) and use 'IF NOT EXISTS' with first table queries.
The drawback here is that you have to make two requests to Cassandra for each of your post requests but the one for the first table should be executed only once if you use 'IF NOT EXISTS'

Upvotes: 1

Related Questions