我正在研究使用 spring-data-cassandra 的 CassandraOperations 更新 cassandra 表内的集合的示例。 请分享,因为我发现很难做到正确。而且它无法通过网络获得。
谢谢
更新
我研究了 spring-data-cassandra 的代码。目前他们只提供通过更新操作替换集合的选项。我将寻求扩展它并提供用于集合升级的 API
最佳答案
这是一个使用 CQL3 的独立示例。使用 java cassandra 驱动程序将其移植到 spring 应该不会很复杂。
cqlsh> CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };
cqlsh> CREATE COLUMNFAMILY test.example (name text PRIMARY KEY, emails list<text>);
cqlsh> UPDATE test.example SET emails = ['fake@mail.com'] where name='lyuben';
cqlsh> SELECT * FROM test.example;
name | emails
--------+-------------------
lyuben | ['fake@mail.com']
(1 rows)
cqlsh> UPDATE test.example SET emails = ['not@real.net'] + emails where name='lyuben';
cqlsh> SELECT * FROM test.example;
name | emails
--------+-----------------------------------
lyuben | ['not@real.net', 'fake@mail.com']
(1 rows)
cqlsh> UPDATE test.example SET emails = emails + ['maybe@legit.cc'] where name='lyuben';
cqlsh> SELECT * FROM test.example;
name | emails
--------+-----------------------------------------------------
lyuben | ['not@real.net', 'fake@mail.com', 'maybe@legit.cc']
(1 rows)
DOCS here以下是 other collections 上的一些内容.
唯一需要注意的是,无论将新集合元素附加在语句的开头还是末尾,都会有所不同:
// post-append
['not@real.net'] + emails
['not@real.net', 'fake@mail.com']
// pre-append
emails = emails + ['maybe@legit.cc']
['not@real.net', 'fake@mail.com', 'maybe@legit.cc']
关于java - 如何使用 CassandraOperations 更新集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28358794/