0

Cassandra has org.apache.cassandra.cql3.QueryHandler interface which provide apis to handle external queries from client.

Below api which handles prepared statment:

public ResultMessage processPrepared(CQLStatement statement, QueryState state, QueryOptions options) throws RequestExecutionException, RequestValidationException;

I want to log queryString and value passed to it, in case CQLStatement,QueryState and QueryOptions is given . How can i get it?

I Believe a person who has worked on cassandra code can help me out in this.

Laxmikant
  • 1,551
  • 1
  • 13
  • 30
  • https://stackoverflow.com/a/40684084/2320144 – Ashraful Islam Nov 19 '17 at 15:35
  • Hi, QueryLogger can help while writing client side code ..I am planning to put some logger at cassadra server side..so i believe this wont help here – Laxmikant Nov 19 '17 at 15:38
  • I don't think you can get query string from CQLStatement, But you can try this https://stackoverflow.com/a/46622652/2320144 – Ashraful Islam Nov 19 '17 at 15:55
  • Query string is converted into Statement there is no way to get back the query string. I have check cassandra code, they log the query string in trace level i.e `Tracing.trace("Parsing {}", queryStr);` – Ashraful Islam Nov 19 '17 at 16:00
  • It seems you are right. I have spent lots of time to some how get back the query from the statement but could not get successful yet. – Laxmikant Nov 19 '17 at 17:15

2 Answers2

1

This would be very difficult in 2.1. With newer versions where for logging they needed this they just recreate it as well as possible. You can see how in the ReadCommand implementations, theres a name() or toCQLString() used in things like slow query logging. You could backport this and the 2 implementations of appendCQLWhereClause for ability to do similar and then build one for modification statement.

in getPrepared() you can get the rawCQLStatement from the ParsedStatement.Prepared and stash it in the thread local.

You may want to alternatively consider using a custom implementation of tracing (example) or using triggers and building a mutation logger.

Chris Lohfink
  • 16,150
  • 1
  • 29
  • 38
1

Do the following:

  • create a class that would implement the QueryHandler interface and make Cassandra aware of it
  • in that class you can maintain a list of the queries (add to this list when prepare method is being called) and the current query that you will get from the list when getPrepared it's called; you can get it from the list using the MD5Digest id
  • when processPrepared is called you can replace the ? in the query string with the values in the QueryOptions options.getValues().

HTH

Horia
  • 2,942
  • 7
  • 14
  • Thanks for the reply . Per has shared your pull with me :) ..It is helpful..I am working on it will share my changes as well – Laxmikant Nov 20 '17 at 13:49