Search code examples
pythoncassandrathrift

How do I insert a SuperColumn with python/thrift?


Using the python/thrift interface I am trying to insert a SuperColumn just like the Comments example in WTF is a Supercolumn..

I've gotten as far as to create the SuperColumn and figured out that I should use batch_mutate to insert it. But I don't know how to create the Mutation and set the key and SuperColumn type

keyspace = "Keyspace1"

col1 = Column(name = "commenter", value = "J Doe", timestamp = time.time())
col2 = Column(name = "email", value = "[email protected]", timestamp = time.time())

sc = SuperColumn(name = str(uuid.uuidl()), [col1, col2])

# i am guessing the missing code goes here

mutation = Mutation(column_or_supercolumn = sc?)
client.batch_mutate(keyspace, mutation, ConsistencyLevel.ZERO)

Solution

  • I would use pycassa or something to make life easier, but something like:

    keyspace = "Keyspace1"
    tableName = "Super1"
    key = "jdoe"
    col1 = Column(name = "commenter", value = "J Doe", timestamp = time.time())
    col2 = Column(name = "email", value = "[email protected]", timestamp = time.time())
    
    newData = [Mutation(ColumnOrSuperColumn(None,
                                 SuperColumn(str(uuid.uuidl()),
                                            [col1, col2])))]
    dataMap = {key : {tableName : newData}}
    
    client.batch_mutate(keyspace=keyspace,
                        mutation_map=dataMap,
                        consistency_level=ConsistencyLevel.ZERO)