My actual problem is that I need to generate unique ids per user. I detail below how I attempted to do it and what problem my solution gave me.
I'm using cassandra-driver to insert into this table of my database:
CREATE TABLE myspace.unpublished_question (
user_id uuid PRIMARY KEY,
account_created timestamp,
first_name text,
surname text
)
To avoid collisions I thought I would have cassandra auto-generate the uuid's for the primary key column, so I'm inserting like this:
const cassandra = require('cassandra-driver');
const cassandraClient = new cassandra.Client({contactPoints: ['localhost'], keyspace: 'myspace'});
const query = 'insert into user (user_id, account_created, first_name, surname) values (uuid(), ?, ?, ?)';
const params = [new Date(), 'John', 'Smith'];
cassandraClient.execute(query, params, {prepare: true}, (err, results) => {
console.log('Callback arguments:', err, JSON.stringify(results));
});
But it only logs this:
Callback: null {"info":{"queriedHost":"127.0.0.1:9042","triedHosts":{},"achievedConsistency":10},"columns":null,"pageState":null}
There's no way for me to retrieve the uuid
that cassandra has just generated for this entry.
How can I find out what this uuid was ?
I'd ideally like to be able to do something like:
cassandraClient.execute(query, params, {prepare: true}, (err, results) => {
const uuid = X;
console.log('User has been given uuid:', uuid);
});
The insert operation does not provide the inserted data, so there is no way to obtain it from the server on the same operation.
You will need to generate the TimeUuid
client side:
const id = TimeUuid.now();
client.execute(query, [ id, 'John', 'Smith' ], { prepare: true }, (err, result) => {
// do something with the id
});
See the Node.js documentation on TimeUuid
for more information: http://docs.datastax.com/en/developer/nodejs-driver/latest/features/datatypes/uuids/#time-uuid