Generate UUID for Cassandra in Python
Asked Answered
A

2

10

Heh, I'm using

cf.insert(uuid.uuid1().bytes_le, {'column1': 'val1'}) (pycassa)

to create a TimeUUID for Cassandra, but getting the error

InvalidRequestException: 
InvalidRequestException(why='UUIDs must be exactly 16 bytes')

It doesn't work with

uuid.uuid1()
uuid.uuid1().bytes
str(uuid.uuid1())

either.

What's the best way to create a valid TimeUUID to use with the CompareWith="TimeUUIDType" flag?

Thanks,
Henrik

Attorn answered 13/7, 2010 at 18:34 Comment(0)
S
4

You must ensure your column family schema accepts UUID as key. Your code will work with a column family created as (using cassandra-cli):

create column family MyColumnFamily
  with column_type = 'Standard'
  and comparator = 'AsciiType'
  and default_validation_class = 'BytesType'
  and key_validation_class = 'TimeUUIDType';

To add values to this CF:

import pycassa
pool = pycassa.ConnectionPool('Keyspace1')
cf = pycassa.ColumnFamily(pool, 'MyColumnFamily')
cf.insert(uuid.uuid1(), {'column1': 'val1'})
Sociality answered 3/1, 2012 at 17:17 Comment(0)
L
9

Looks like you are using the uuid as the row key and not the column name.

The 'compare_with: TimeUUIDType' attribute specifies that the column names will be compared with using the TimeUUIDType, i.e it tells Cassandra how to sort the columns for slicing operations

Have you considered using any of the high level python clients? E.g. Tradedgy, Lazy Boy, Telephus or Pycassa

Lob answered 13/7, 2010 at 18:44 Comment(3)
I should've mentioned that I'm usig pycassa, but it seems that I have to create the timeuuids myself.Attorn
I don't understand how this answers the question. I am having the same problem, would it be possible for you to give some more details?Rizas
you know the difference between keys and column names?Lob
S
4

You must ensure your column family schema accepts UUID as key. Your code will work with a column family created as (using cassandra-cli):

create column family MyColumnFamily
  with column_type = 'Standard'
  and comparator = 'AsciiType'
  and default_validation_class = 'BytesType'
  and key_validation_class = 'TimeUUIDType';

To add values to this CF:

import pycassa
pool = pycassa.ConnectionPool('Keyspace1')
cf = pycassa.ColumnFamily(pool, 'MyColumnFamily')
cf.insert(uuid.uuid1(), {'column1': 'val1'})
Sociality answered 3/1, 2012 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.