1

I am using this driver as a bridge between cassandra and my node js app. Everything seems to work fine till now except following issue:

Issue I have a column of type varchar, when i am inserting a string which has dash (-) in it then cassandra throws errorString didn't validate..

I am using batch statement which is like following

var queryset_insert_user = {
    query: query_insert_user,
    params: query_insert_user_params,
    hints:[ dataTypes.varchar, dataTypes.varchar, dataTypes.varchar, dataTypes.varchar,
    dataTypes.varchar, dataTypes.varchar, dataTypes.varchar]
}

where varchar is

var dataTypes = {};
dataTypes.varchar = 0x000d;  //couldn't find how to get this from API itself so just copied value from types.js of cassandra driver.

Batch statement is

var batchQueries = [queryset_insert_iidMetadata, queryset_insert_user];
client.batch(batchQueries,__queryOptions(), function(err, result){
       // getting err here...
});

Input parameters are:

query2 params are = ["4fde84c173232d25641db25ba1b0","+0012255446633",["1415957771074"],"CGFnzVSuGwkOrVI","NEW","+001","53a985bd-bc28-3768-a1ea-e366409cb996"]

Note I have attempted to use hints when i saw this issue.

How to solve this error?

jorgebg
  • 6,560
  • 1
  • 22
  • 31
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72

2 Answers2

3

After adding the option prepare: true, the string containing dash can be inserted into cassandra nomally.

Here is the code from cassandra node driver repo:

// Use query markers (?) and parameters
const query = 'UPDATE users SET birth = ? WHERE key=?'; 
const params = [ new Date(1942, 10, 1), 'jimi-hendrix' ];
// Set the prepare flag in the query options
client.execute(query, params, { prepare: true })
  .then(result => console.log('Row updated on the cluster'));
Employee
  • 3,109
  • 5
  • 31
  • 50
Iceberg
  • 2,744
  • 19
  • 19
2

In the DataStax Node.js driver for Cassandra, the parameter hints are passed in the query options:

// Queries with the parameters
const queries = [ queryset_insert_iidMetadata, queryset_insert_user ];
// Parameter hints are passed with the rest of the queryOptions
client.batch(queries, {hints: [hintForIidMeta, hintForUser]}, callback);

In any case, the recommended way is to use prepared statements, that way the parameter mapping would be accurate:

await client.batch(queries, { prepare: true })
console.log('Data updated on cluster');
jorgebg
  • 6,560
  • 1
  • 22
  • 31