Skip to content
Jorge Bay Gondra edited this page Jan 21, 2014 · 7 revisions

Cassandra data types support

node-cassandra-cql provides support for all Cassandra types.

Depending on the Cassandra data type, the Driver will retrieve the values in an specific Javascript type. For example:

  • Bigints are decoded as Long.
  • List / Set datatypes are decoded to Javascript Arrays.
  • Map datatype are decoded to Javascript objects with keys as properties.
  • Timestamp are decoded to Date objects.

In the same way, the Driver will encode Javascript types into Cassandra data types. The driver will try to guess the target data type based on the parameter type:

  • Javascript string will be encoded as text.
  • Javascript Date will be encoded as timestamp.
  • Javascript Number will be encoded as int.
  • Long will be encoded bigint.
  • Javascript Arrays will be encoded as list.
  • Node.js Buffers will be encoded as blob.

Consumers can provide a hint to the driver specifying the target data type. For example:

client.executeAsPrepared(query, [{value: value, hint: 'set<varchar>'], callback);

Samples: Writing to Cassandra

//String and Date: no hint required
client.executeAsPrepared(
  'INSERT INTO sampletable (string_column, date_column) VALUES (?, ?)',
  ['text1', new Date()], 
  callback);

//Integer Number and Long: no hint required
client.executeAsPrepared(
  'INSERT INTO sampletable (int_column, long_column) VALUES (?, ?)',
  [123, types.Long.fromString('9223372036854775807')], 
  callback);

//UUID and list: no hint required
client.executeAsPrepared(
  'INSERT INTO sampletable (uuid_column, list_column) VALUES (?, ?)',
  [types.uuid(), ['one, 'two']], 
  callback);

//Buffers: no hint
client.executeAsPrepared(
  'INSERT INTO sampletable (uuid_column, blob_column) VALUES (?, ?)',
  [types.uuid(), new Buffer(128)], 
  callback);

Providing hint

You can specify the target datatype by creating a param object with the properties hint and value. All Cassandra CQL native types are defined in the object types.dataTypes.

client.executeAsPrepared(
  'INSERT INTO sampletable (int_column, float_column) VALUES (?, ?)',
  [100, {value: 1.1, hint: types.dataType.float}], 
  callback);
//Also you can specify the hint as a string 
client.executeAsPrepared(
  'INSERT INTO sampletable (int_column, float_column) VALUES (?, ?)',
  [100, {value: 1.1, hint: 'float'}], 
  callback);
Clone this wiki locally