Example of how to use timeUUID and autogenerate for Cassandra?

524 views
Skip to first unread message

Brad Gawne

unread,
Jun 19, 2015, 11:33:31 AM6/19/15
to kundera...@googlegroups.com
Hi,

I was wondering if there is a sample somewhere on how to autogenerate a timeUUID for Cassadra.

Also, I noticed from the the code, while i was looking through trunk that the database is called with the now function and  a time UUID is created, but only if the dataType is uuid is it used.
For efficiency, shouldnt you put this code in the case block for datatype? It doesnt seem to need to make a call unless the type is defintely UUID, although perhaps it always is.

Anyway, if you could tell me how to autogenerate timeUUID that would be awesome !

-brad




@Override
    public Object generate(Client<?> client, String dataType)
    {
        CqlResult cqlResult = ((CassandraClientBase) client).execute("Select now() from system.schema_columns",
                ((CassandraClientBase) client).getRawClient("system"));

        CqlRow cqlRow = cqlResult.getRowsIterator().next();
        TimeUUIDType t = TimeUUIDType.instance;
        UUID timeUUID = t.compose(ByteBuffer.wrap(cqlRow.getColumns().get(0).getValue()));

        switch (dataType.toLowerCase())
        {
        case "uuid":
            return timeUUID;

        default:
            return UUID.randomUUID();

        }
    }

Chhavi Gangwal

unread,
Jun 22, 2015, 10:10:18 AM6/22/15
to kundera...@googlegroups.com
Hi Brad,

In order to auto generate timeUUID in Cassandra , you should map timeUUID type id in Cassandra to UUID in your java entity class e.g.:

@Id
@GeneratedValue
private UUID id;

Cassandra : id => timeUUID

Hope that Helps !
Chhavi
Reply all
Reply to author
Forward
0 new messages