Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get exact cql from statement using java api from datastax

My code directly executes the bound statement prepared without any exact query. Then how to get the cql it is trying to perform in cassandra database?

For example:

public <T> void save(T entity) {
    if (entity != null) {
        Statement statement = getEntityMapper(entity).saveQuery(entity);
        statement.setConsistencyLevel(consistencyLevelWrite);
        mappingManager.getSession().execute(statement);
    }
}

I am trying to get something like INSERT INTO "keyspace"."tableName"("column1","column2") VALUES (value1,value2)

like image 689
user3870168 Avatar asked Mar 13 '23 02:03

user3870168


1 Answers

My most generic answer is to enable the query logger. It will show executed queries in your application logs.

If you need something more specific and want to manipulate the query string in your own code, you can take inspiration from the implementation: QueryLogger.java. In this particular case, you can get the "generic" query string (with placeholders) by casting to BoundStatement and then invoking .preparedStatement().getQueryString() on it; then inspect the bound statement for the values of the placeholders. As you'll see in the code, QueryLogger handles a lot of corner cases (e.g. truncating large parameters).

like image 122
Olivier Michallat Avatar answered Mar 16 '23 07:03

Olivier Michallat