cassandrapycassanosql

How can I specify primary key using pycassa?


I am trying to add a primary key using pycassa. I can do that using CQL3 but I cannot find any document that explains how to do this with Pycassa in the web. I googled too much, didnt find anything.

Thank you


Solution

  • Yes, you can have a large primary key ("composite key"). In your example, id would then be the partition key, and name, last_name, age are clustering keys. All fields combined make the primary key.

    For a very good explanation of the different types (and naming) of keys, check out this SO answer.

    This post gives an example of how to create a composite key:

    system_manager.create_column_family(..., key_validation_class="CompositeType(UTF8Type, Int32Type)")
    

    For your data then, I would guess:

    system_manager.create_column_family(..., key_validation_class="CompositeType(Int32Type, UTF8Type, UTF8Type, Int32Type)")
    

    I have not used pycassa myself, but the pycassa documentation has this example:

    col_fam.insert('row_key', {'col_name': 'col_val'})
    

    So I'll venture a guess for your case (adding a couple of columns for the sake of completeness):

    col_fam.insert((id,name,last_name,age), {'firstname':first_name,'city':city})