Cassandra - CqlEngine - using the collection

I want to know how I can work with a collection in cqlengine. I can insert a value into the list, but only one value, so I cannot add any value to my list. I want to do this: In CQL3:

UPDATE users SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo'; 

In CqlEngine:

 connection.setup(['127.0.0.1:9160']) TestModel.create(id=1,field1 = [2]) 

this code will add 2 to my list, but when I insert a new value, it is replaced with the old value in the list.

The only help in Cqlengine: https://cqlengine.readthedocs.org/en/latest/topics/columns.html#collection-type-columns

And I want to know how I can read the cqlengine collection field. Is this a dictionary in my django project? how can i use it? !!

Please, help. Thanks

+4
source share
1 answer

Looking at your example, this is a list.

For a table based on Cassandra CQL documentation:

 CREATE TABLE plays ( id text PRIMARY KEY, game text, players int, scores list<int> ) 

You must declare the model as follows:

 class Plays(Model): id = columns.Text(primary_key=True) game = columns.Text() players = columns.Integer() scores = columns.List(columns.Integer()) 

You can create a new entry similar to this (omitting the connection code):

 Plays.create(id = '123-afde', game = 'quake', players = 3, scores = [1, 2, 3]) 

Then, to update the list of ratings, follow these steps:

 play = Plays.objects.filter(id = '123-afde').get() play.scores.append(20) # <- this will add a new entry at the end of the list play.save() # <- this will propagate the update to Cassandra - don't forget it 

Now, if you request data with the CQL client, you should see the new values:

  id | game | players | scores ----------+-------+---------+--------------- 123-afde | quake | 3 | [1, 2, 3, 20] 

To get the values ​​in python, you can simply use the array index:

 print "Length is %(len)s and 3rd element is %(val)d" %\ { "len" : len(play.scores), "val": play.scores[2] } 
+2
source

Source: https://habr.com/ru/post/1497382/


All Articles