Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

erpriyeshnaik_190137 avatar image
erpriyeshnaik_190137 asked ·

How do I generate a timeuuid type to do an INSERT with the Python driver?

Hi Folks,

This is my first query on the forum so please correct if my understanding are wrong.

I am executing exercise Application Driver Connection from DS201.

Table is as follows:

cqlsh:killrvideo> SELECT * FROM videos_by_tag ;
 tag | added_date | video_id | title
-----------+---------------------------------+--------------------------------------+------------------------------
 datastax | 2013-10-16 00:00:00.000000+0000 | 4845ed97-14bd-11e5-8a40-8338255b7e33 | DataStax Studio

Now I want to do one task "Python code to insert a new video into the database" as per lab.

I tried this code and getting error:

session.execute(
    "INSERT INTO videos_by_tag (tag, added_date, video_id, title)"
    "VALUES ('cassandra', '2013-01-10', uuid(), 'Cassandra Is My Friend')"
)
Traceback (most recent call last):
 File "<stdin>", line 3, in <module>
 File "cassandra/cluster.py", line 2618, in cassandra.cluster.Session.execute
 File "cassandra/cluster.py", line 4877, in cassandra.cluster.ResponseFuture.result
cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Type error: cannot assign result of function system.uuid (type uuid) to video_id (type timeuuid)"

I tried below but failed:

1. UUIDs.timeBased()

cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Unknown function uuids.timebased called"

2. cassandra.util.uuid_from_time

cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:109 no viable alternative at input '.' (...)VALUES ('cassandra', '2013-01-10', [cassandra].util...)">

For time being I have hardcoded the value.

session.execute(
    "INSERT INTO videos_by_tag (tag, added_date, video_id, title)"
    "VALUES ('cassandra', '2013-01-10', 245e8024-14bd-11e5-9743-8238357b7e32, 'Cassandra Is My Friend')"
)

Success in DB:

cassandra | 2013-01-10 00:00:00.000000+0000 | 245e8024-14bd-11e5-9743-8238357b7e32 | Cassandra Is My Friend

But I want to know this ?

cqlpython driver
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered ·

Try using the built-in CQL function now(). The coordinator of the request generates a value of type timeuuid when the INSERT statement is executed.

For example:

INSERT INTO ... ( ... ) VALUES ( ... , NOW(), ... )

You can get a list of other UUID functions and examples in the CQL UUID and timeuuid functions page. Cheers!

1 comment Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

This worked !

0 Likes 0 · ·
bettina.swynnerton avatar image
bettina.swynnerton answered ·

Hi,

you were on the right track there to use cassandra.util.uuid_from_time from the driver utils, this gives you a time uuid.

But it looks like you passed the value incorrectly into the query statement, which resulted in the CQL syntax error.

You can try something like this:

time1 = datetime.datetime.now()

session.execute(
    """
    INSERT INTO killrvideo.videos_by_tag (tag, added_date, video_id, title) 
    VALUES (%s, %s, %s, %s)
    """, 
    ('cassandra', '2013-01-10', cassandra.util.uuid_from_time(time1), 'Cassandra Is My Friend')
)

Have a look at the driver docs, too:

https://docs.datastax.com/en/developer/python-driver/3.24/getting_started/#passing-parameters-to-cql-queries

Hope this helps!

1 comment Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Hi @bettina.swynnerton,

Thank you for your reply.

cassandra.util.uuid_from_time(time1)

I tried this function but it still giving similar error.

File "testPyDriver.py", line 25, in <module>

....
cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:109 no viable alternative at input '.' (...)VALUES ('cassandra', '2013-01-10', [cassandra].util...)">

PS: Unable to convert this to Comment

0 Likes 0 · ·