1

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);
});
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • [Does it help ?](https://stackoverflow.com/questions/26586686/obtaining-id-of-inserted-row-cassandra-net) – Orelsanpls Nov 30 '17 at 10:21
  • 1
    @GrégoryNEUT that just suggests passing in the uuid myself. Of course this is possible, but how do I ensure that I won't cause a conflict? – theonlygusti Nov 30 '17 at 10:54
  • takes a look in [here](https://stackoverflow.com/questions/16084573/cassandra-generate-a-unique-id). This is a good question you are asking – Orelsanpls Nov 30 '17 at 10:55
  • @GrégoryNEUT so I change column type to `timeuuid` and then in JavaScript I pass `new Date()`? – theonlygusti Nov 30 '17 at 10:56
  • What happens in that case if two inserts are run at exactly the same time – theonlygusti Nov 30 '17 at 10:56
  • In the SO post @Richard says `There is a collision probability, but the collision probability (assuming uncorrelated random number sources, which it will be if you generate in Java) is extremely low - if you created 1 billion a second for 100 years the probability of one collision is about 50%. (See http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates for more details.)` – Orelsanpls Nov 30 '17 at 12:57
  • If it happens you i'll have an error from the database. You can catch it, and call it again (it will generate a new timeuuid) and it's be good. – Orelsanpls Nov 30 '17 at 12:58

1 Answers1

0

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

jorgebg
  • 6,560
  • 1
  • 22
  • 31