ResponseError: Expected 4 or 0 bytes int

I am trying to start the cassandra node driver and get into a problem when inserting a record, it seems that the cassandra driver cannot insert float values.

Problem: When passing int value for insertion in db, api gives following error: Debug: hapi, internal, implementation, error ResponseError: Expected 4 or 0 byte int (8) at FrameReader.readError (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/readers.js:291:13) at Parser.parseError (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:185:45) at Parser.parseBody (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:167:19) at Parser._transform (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:101:10) at Parser.Transform._read (_stream_transform.js:179:10) at Parser.Transform._write (_stream_transform.js:167:12) at doWrite (_stream_writable.js:225:10) at writeOrBuffer (_stream_writable.js:215:5) at Parser.Writable.write (_stream_writable.js:182:11) at write (_stream_readable.js:601:24) 

I am trying to execute the following query from code:

 INSERT INTO ragchews.user (uid ,iid ,jid ,jpass ,rateCount ,numOfratedUser ,hndl ,interests ,locX ,locY ,city ) VALUES ('uid_1',{'iid1'},'jid_1','pass_1',25, 10, {'NEX1231'}, {'MUSIC'}, 21.321, 43.235, 'delhi'); 

passed to execute() is

 var params = [uid, iid, jid, jpass, rateCount, numOfratedUser, hndl, interest, locx, locy, city]; 

Where

 var locx = 32.09; var locy = 54.90; 

and the call to execute is as follows:

 var addUserQuery = 'INSERT INTO ragchews.user (uid ,iid ,jid ,jpass ,rateCount ,numOfratedUser ,hndl ,interests ,locX ,locY ,city) VALUES (?,?,?,?,?,?,?,?,?,?,?);'; var addUser = function(user, cb){ console.log(user); client.execute(addUserQuery, user, function(err, result){ if(err){ throw err; } cb(result); }); }; CREATE TABLE ragchews.user( uid varchar, iid set<varchar>, jid varchar, jpass varchar, rateCount int, numOfratedUser int, hndl set<varchar>, interests set<varchar>, locX float, locY float, city varchar, favorite map<varchar, varchar>, PRIMARY KEY(uid) ); 

PS Some notes when trying to understand the problem:

  • Since the problem is with float, so I changed the type of float (locX, locY) to int and re-run the code. The same error persists. Therefore, this is not a problem specifically related to float type CQL.
  • Then I tried to remove all ints from the INSERT query and tried to insert only non-numeric values. This attempt successfully entered a value in db. Therefore, it now looks like this problem may be associated with numeric types .
+5
source share
1 answer

The following words are taken from the cassandra node documentation for driver data type

When encoding data during normal execution with parameters, the driver tries to guess the target type based on the input type. Values โ€‹โ€‹of type Number will be encoded as double (as the value of Number double / IEEE 754).

Consider the following example:

 var key = 1000; client.execute('SELECT * FROM table1 where key = ?', [key], callback); 

If the key column is of type int, execution is not performed. There are two possible ways to avoid this problem:

  • Prepare data (recommended) - prepare a request before execution

    client.execute('SELECT * FROM table1 where key = ?', [key], { prepare : true }, callback);

  • Hint for target types - hint: first parameter is an integer

    client.execute('SELECT * FROM table1 where key = ?', [key], { hints : ['int'] }, callback);

If you are dealing with a batch update, then this question may interest you.

+17
source

Source: https://habr.com/ru/post/1205988/


All Articles