Spearfisher
Spearfisher

Reputation: 8783

Using user-defined types in Cassandra with nodejs

I am trying to use user-defined types in Cassandra with nodejs. Following the documentation, I successfully manage to do it in cqlsh (https://www.datastax.com/documentation/cql/3.1/cql/cql_using/cqlUseUDT.html). I manage to insert items in cqlsh but not ussing the cassandra-driver module.

I understand they are some intricacies do to the use of javascript (http://www.datastax.com/documentation/developer/nodejs-driver/1.0/nodejs-driver/reference/nodejs2Cql3Datatypes.html) and I have tried the different options offered but I couldnt not get it to work.

Here are my trials and their resulting error message:

var insertQuery = 'INSERT INTO mykeyspace.users (id, name) VALUES (?, ?)'

client.execute(insertQuery, ['62c36092-82a1-3a00-93r1-46196ee77204', { firstname: 'paul', lastname: 'jacques'}], {hints: ['uuid', 'map<text, text>']}, function(err) { console.log(err);})

{ name: 'ResponseError', message: 'Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=9 cap=9]', info: 'Represents an error message from the server', code: 8704, query: 'INSERT INTO mykeyspace.users (id, name) VALUES (?, ?)' }

client.execute(insertQuery, ['62c36092-82a1-3a00-93r1-46196ee77204', { firstname: 'paul', lastname: 'jacques'}], { prepare: true }, function(err) { console.log(err);})

    { [TypeError: Not a valid blob, expected Buffer obtained { firstname: 'paul', lastname: 'jacques' }]
  query: 'INSERT INTO mykeyspace.users (id, name) VALUES (?, ?)' }

Then what is the correct syntax to get it to work?

NB. I am using cassandra 2.1.1

Upvotes: 2

Views: 1236

Answers (1)

jorgebg
jorgebg

Reputation: 6600

The DataStax Node.js driver does not support User defined types yet, so the driver won't be able to serialize the data accordingly.

You can use json notation in CQL to access the individual fields. For example, queries like the following will work:

SELECT addr.street, addr.city FROM location WHERE id=?;

or

UPDATE SET addr['street'] = ?, addr['city'] = ? WHERE id = ?

Upvotes: 2

Related Questions