首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cassandra - CqlEngine -使用集合

Cassandra - CqlEngine -使用集合
EN

Stack Overflow用户
提问于 2013-08-16 07:38:10
回答 1查看 1.1K关注 0票数 4

我想知道如何使用CQL3中的集合,我可以将值插入列表,但只插入一个值,这样我就不能在我的列表中添加一些值--我想这样做:在CQL3中:

代码语言:javascript
复制
UPDATE users
SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo';

在CqlEngine中:

代码语言:javascript
复制
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项目中的字典吗?我怎么能用呢?!!

请帮帮忙。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-17 12:56:52

看看你的例子,这是一个列表。

给出了一个基于Cassandra CQL文件的表格:

代码语言:javascript
复制
CREATE TABLE plays (
    id text PRIMARY KEY,
    game text,
    players int,
    scores list<int>
)

你必须像这样声明模型:

代码语言:javascript
复制
class Plays(Model):
        id = columns.Text(primary_key=True)
        game = columns.Text()
        players = columns.Integer()
        scores = columns.List(columns.Integer())

您可以创建这样的新条目(省略如何连接的代码):

代码语言:javascript
复制
Plays.create(id = '123-afde', game = 'quake', players = 3, scores = [1, 2, 3])

然后,要更新分数列表:

代码语言:javascript
复制
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客户端查询数据,您应该会看到新的值:

代码语言:javascript
复制
 id       | game  | players | scores
----------+-------+---------+---------------
 123-afde | quake |       3 | [1, 2, 3, 20]

要获取python中的值,只需使用数组的索引:

代码语言:javascript
复制
print "Length is %(len)s and 3rd element is %(val)d" %\
 { "len" : len(play.scores), "val": play.scores[2] }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18268201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档