我想知道如何使用CQL3中的集合,我可以将值插入列表,但只插入一个值,这样我就不能在我的列表中添加一些值--我想这样做:在CQL3中:
UPDATE users
SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo';在CqlEngine中:
connection.setup(['127.0.0.1:9160'])
TestModel.create(id=1,field1 = [2])此代码将将2添加到我的列表中,但当我插入新值时,它将替换为list中的旧值。
Cqlengine的唯一帮助:https://cqlengine.readthedocs.org/en/latest/topics/columns.html#collection-type-columns
我想知道我是如何通过can读取集合字段的。它是我django项目中的字典吗?我怎么能用呢?!!
请帮帮忙。谢谢
发布于 2013-08-17 12:56:52
看看你的例子,这是一个列表。
给出了一个基于Cassandra CQL文件的表格:
CREATE TABLE plays (
id text PRIMARY KEY,
game text,
players int,
scores list<int>
)你必须像这样声明模型:
class Plays(Model):
id = columns.Text(primary_key=True)
game = columns.Text()
players = columns.Integer()
scores = columns.List(columns.Integer())您可以创建这样的新条目(省略如何连接的代码):
Plays.create(id = '123-afde', game = 'quake', players = 3, scores = [1, 2, 3])然后,要更新分数列表:
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现在,如果您使用CQL客户端查询数据,您应该会看到新的值:
id | game | players | scores
----------+-------+---------+---------------
123-afde | quake | 3 | [1, 2, 3, 20]要获取python中的值,只需使用数组的索引:
print "Length is %(len)s and 3rd element is %(val)d" %\
{ "len" : len(play.scores), "val": play.scores[2] }https://stackoverflow.com/questions/18268201
复制相似问题