Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print end Query in Cassandra nodejs-driver?

I am looking for the way to print end Query in Cassandra nodejs-driver, i.e. CQL String after binding all parameters so that I can debug and see what values are actually going to the Cassandra engine. How to do this?

var query = ' select * from shahid.tbl_raw_data where yymmddhh = ? limit ? ';
var params = [last_yymmddhh_value, settings.records_per_batch];

client.execute(query, params, {prepare: true}, function(err, result)
{           
      console.log( result );
});

I want to get string like this

select * from shahid.tbl_raw_data where yymmddhh = '15010101' limit 100

Note: This is simple example, I am actually working on very complex query in loop, so I don't want to do console.log(last_yymmddhh_value) to see the binding values.

like image 280
Mohd Shahid Avatar asked Mar 13 '23 12:03

Mohd Shahid


2 Answers

The query that actually gets executed by the driver is different then the full query text as the evaluation is done server side.

What actually gets sent to the server is a unique ID identifying the prepared statement and the parameters values that you are providing (see: EXECUTE). However, ultimately the query text you provided is what is being evaluated.

The java-driver has a Query Logger concept that logs executed queries and their latencies. You can configure it in such a way that it logs only slow queries or all queries. It also logs parameters that were used in prepared statements, i.e.:

DEBUG [cluster1] [/127.0.0.1:9042] Query completed normally, took 9 ms: select * from shahid.tbl_raw_data where yymmddhh = ? limit ? [15010101, 100];

There isn't such a construct for the nodejs-driver yet, but alex-rokabilis' answer will work in most cases, with the only exceptions being where you use named parameters or you actually have '?' in column names (which is valid, but probably not something people practically do). I've logged NODEJS-220 to request implementing such a feature.

like image 22
Andy Tolbert Avatar answered Mar 24 '23 15:03

Andy Tolbert


You can try this

function dispQuery(query, params) {
   var params_temp = Array.from(params);
   return query.replace(/\?/g, function() {
      return params_temp.shift()
   })
}

For example in your code:

client.execute(query, params, {prepare: true}, function(err, result)
{           
  console.log("Query ::%s",dispQuery(query,params));
  console.log("Result ::",result);
});
like image 58
Alex Michailidis Avatar answered Mar 24 '23 15:03

Alex Michailidis