java - How to use bind variable inside collection literal in cassandra - InvalidQueryException
问题描述
I'm preparing a cassandra query in java using com.datastax.driver.core.PreparedStatement
My schema is very simple - a key
which is of type text
and value
of type set<text>
.
I am preparing the statement like so:
mySession.prepare("UPDATE a.table USING TTL :ttl SET value = value + {:value} WHERE key = :key ");
[This is done in order to add a value to the set and have separate ttl
for each value]
I got this error when evaluating the above statement:
com.datastax.driver.core.exceptions.InvalidQueryException: Invalid set literal for value: bind variables are not supported inside collection literals
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:49)
at com.datastax.driver.core.DriverThrowables.propagateCause(DriverThrowables.java:35)
at com.datastax.driver.core.AbstractSession.prepare(AbstractSession.java:86)
How can I overcome this exception and bind my variable to a collection of type set<text>
. Alternatively, is there some better/prefered way of accomplishing my goal of dynamically (with different TTL) adding items to a set
in cassandra from java?
解决方案
You can do as follows:
PreparedStatement st = session.prepare("UPDATE test2 USING TTL :ttl SET value = value + :value WHERE name = :name ");
session.execute(st.bind()
.setInt("ttl",1)
.setSet("value", ImmutableSet.of("add this value"))
.setString("name","key1"));
推荐阅读
- r - 从邻接矩阵重建二元数据帧的最有效过程是什么?
- sql-server - 出现“函数过程需要未提供的参数”错误,但提供了所有参数
- bash - git apply 错误没有这样的文件或目录
- javascript - 在打开图层中单击图层时的回调
- git - 有没有办法对项目中的每个文件进行“git stash”(不仅仅是更改)?
- reactjs - ShallowWrapper is empty when running test
- arrays - 诉诸阵列
- azure-devops - 上传到 Azure Devops 的所有附件的最大大小是多少?
- java - 如何将这些按钮添加到我的主要活动中?
- python - 计算熊猫数据框两列中每个值组合的出现次数并制作条形图