How to get exact cql from statement using java api from datastax
Asked Answered
J

1

5

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)

Jurist answered 2/3, 2016 at 9:54 Comment(2)
You might want to provide more details, like code showing how you build and execute the bound statement. Also what do you mean by "without any exact query"?Bonneau
Just added an exampleJurist
B
8

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).

Bonneau answered 2/3, 2016 at 13:54 Comment(2)
Thanks a lot for the information. Unfortunately the message logged by QueryLogger is not what we want. Most importantly, event I set withMaxQueryStringLength(Integer.MAX_VALUE), it still truncated our INSERT query. I guess the only approach left is to write our own query logger.Jurist
query logger hotlink is dead. Can you please fix?Corbicula

© 2022 - 2024 — McMap. All rights reserved.