Sach Sach - 3 months ago 30
Node.js Question

Insert BigInt in cassandra from node js

I have a very simple table in cassandra.

Name: test

Columns :

  • id ( bigint, primary key)
  • name (text)


I am trying to insert into values into this using nodejs (using cassandra-driver)

client.execute("insert into test (id, name) values (?, ?)", [123, "dskddjls"], function (err, response) {
if (err) {
console.log("An error occured, ", err)
//...
}
else {
console.log("Added to table, ", response)
//...
}
})


The insertion completes successfully but when I check my cassandra database, it appears as if I have garbage values for big int column.

enter image description here

Any explanation for why this is happening ?

Answer

You need to specify the type explicitly as the third argument to execute function:

client.execute("insert into test (id, name) values (?, ?)", [123, "dskddjls"], {hints: ["bigint", null]}, function (err, response) {
   ...
})

The reason is that for some fields cassandra driver can't guess the type (like bigint or timestamp), so you need to hint it. For string or regular numbers it will work without a hint.

Comments