5

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 ?

Sachin
  • 3,350
  • 2
  • 17
  • 29
  • 1
    Can you try to specify the type explicitly: `client.execute("insert into test (id, name) values (?, ?)", [123, "dskddjls"], {hints: ["bigint", null]}, function (err, data) {.... })` – Vsevolod Goloviznin Aug 24 '16 at 14:42
  • @VsevolodGoloviznin yeah, this works. So do we need to specify types explicitly ? – Sachin Aug 24 '16 at 14:46

2 Answers2

4

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.

Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • The expected JavaScript type for a `bigint` is a `Long`: https://github.com/datastax/nodejs-driver/tree/master/doc/features/datatypes – jorgebg Aug 25 '16 at 08:29
1

In the CQL datatypes to JavaScript types documentation you can see that expected JavaScript type for bigint is Long. The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, used by the driver to represent Cassandra int, float and double (default).

In your case, if you want to insert a value that you have in your application as a number, you should use the Long.fromNumber() method:

const query = "insert into test (id, name) values (?, ?)";
client.execute(query, [ Long.fromNumber(123), "my name" ], callback);

Also, for accurate mapping between a JavaScript type and a Cassandra type (along with other benefits) you should prepare your queries. In your case, if you set the prepare flag, the driver will identify that the expected value is a Long and do the conversion from Number:

client.execute(query, [ 123, "my name" ], { prepare: true }, callback);
jorgebg
  • 6,560
  • 1
  • 22
  • 31